Repository navigation
Commit e41aeea
Fix stack overflow when a descriptor's
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__get__ is itself a descriptor (pyrefly-org#3709)1 parent 42ee12e commit e41aeea
2 files changed
Lines changed: 41 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4768 | 4768 | | |
4769 | 4769 | | |
4770 | 4770 | | |
| 4771 | + | |
| 4772 | + | |
| 4773 | + | |
| 4774 | + | |
| 4775 | + | |
| 4776 | + | |
4771 | 4777 | | |
4772 | 4778 | | |
4773 | 4779 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
912 | 912 | | |
913 | 913 | | |
914 | 914 | | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
0 commit comments