Skip to content

Commit e41aeea

Browse files
mikeleppanemeta-codesync[bot]
authored andcommitted
Fix stack overflow when a descriptor's __get__ is itself a descriptor (pyrefly-org#3709)
Summary: Fixes pyrefly-org#3671 ### What pyrefly aborted with a stack overflow on this (fuzzed) program: ```python class C: def d() -> C: pass d def __get__(): pass C.__get__() ``` Before: `thread '<unknown>' has overflowed its stack` → abort. After: terminates with `Expected a callable, got C` at the call — matching pyright/ty, which never crash. ### Why The decorator `d` (which returns `C`) gives the `__get__` member type `C`. Because `C` defines `__get__`, that member is *itself* classified as a descriptor of `C`. Resolving the descriptor's getter re-entered attribute resolution, which re-applied the descriptor protocol to `__get__`, which resolved to another descriptor of `C`… with no base case, recursing ~10k frames until the stack overflowed. The descriptor protocol is specified to apply at most once per access — the `__get__` you invoke is a bound method, never re-fed through the protocol — and that "once" rule is the missing termination condition. A type checker must terminate regardless of how pathological the input is. ### How `pyrefly/lib/alt/class/class_field.rs` — one guard in `resolve_descriptor_getter`: if the `__get__` member resolves to a `ClassAttribute::Descriptor`, return `None` ("no usable getter") instead of recursing. The read then falls back to the descriptor's instance type. A real `__get__` is always a method (resolves to `ReadWrite`/bound method), so this only fires on the pathological self-referential case. The `Descriptor` attribute branch always routes through the getter, so the single guard also terminates the assignment/`__set__` path — no separate setter guard needed. Nothing else is touched. Pull Request resolved: pyrefly-org#3709 Test Plan: `pyrefly/lib/test/descriptors.rs`: | Test | Protects | |------|----------| | `test_self_referential_descriptor_get_no_crash` | Read path (the issue repro) terminates; call reported `not-callable` | | `test_self_referential_descriptor_set_no_crash` | Assignment to a self-referential descriptor also terminates | Reviewed By: yangdanny97 Differential Revision: D107774088 Pulled By: rchen152 fbshipit-source-id: 7d1e81e0d2174dfaf32a21c5329162ecd5902774
1 parent 42ee12e commit e41aeea

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎pyrefly/lib/alt/class/class_field.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,6 +4768,12 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
47684768
{
47694769
let attr =
47704770
self.as_instance_attribute(&dunder::GET, &getter, &Instance::of_class(&x.cls));
4771+
// `__get__` is bound and called like a method, never re-fed through the
4772+
// descriptor protocol. If it is itself a descriptor, recursing here would
4773+
// loop forever, so report no usable getter.
4774+
if matches!(attr, ClassAttribute::Descriptor(..)) {
4775+
return None;
4776+
}
47714777
Some(
47724778
self.resolve_get_class_attr(attr_name, attr, x.range, errors, None)
47734779
.unwrap_or_else(|e| {

‎pyrefly/lib/test/descriptors.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,38 @@ def f(c: C):
912912
assert_type(c.x, str | None)
913913
"#,
914914
);
915+
916+
testcase!(
917+
// A `__get__` whose type is itself a descriptor must not recurse forever; the
918+
// read falls back to the descriptor's instance type, so the call below is reported
919+
// as not callable rather than overflowing the stack.
920+
test_self_referential_descriptor_get_no_crash,
921+
r#"
922+
class C:
923+
def d() -> C: ...
924+
@d # E: Expected 0 positional arguments, got 1 in function `C.d`
925+
def __get__():
926+
pass
927+
C.__get__() # E: Expected a callable, got `C`
928+
"#,
929+
);
930+
931+
testcase!(
932+
// Assignment resolves a descriptor through its getter too, so the same guard keeps
933+
// the write path from overflowing the stack.
934+
test_self_referential_descriptor_set_no_crash,
935+
r#"
936+
class C:
937+
def d() -> C: ...
938+
@d # E: Expected 0 positional arguments, got 1 in function `C.d`
939+
def __get__():
940+
pass
941+
@d # E: Expected 0 positional arguments, got 1 in function `C.d`
942+
def __set__():
943+
pass
944+
class Host:
945+
x: C = C()
946+
def f(h: Host) -> None:
947+
h.x = 5 # E: Expected a callable, got `C`
948+
"#,
949+
);

0 commit comments

Comments
 (0)