Title: Deadlock when `__len__` re-enters `__length_hint__` via list iterator · Issue #6590 · RustPython/RustPython · GitHub
Open Graph Title: Deadlock when `__len__` re-enters `__length_hint__` via list iterator · Issue #6590 · RustPython/RustPython
X Title: Deadlock when `__len__` re-enters `__length_hint__` via list iterator · Issue #6590 · RustPython/RustPython
Description: What happened? iterator.__length_hint__ holds its internal PyMutex while calling the sequence’s __len__. A user __len__ can call back into the same __length_hint__, which immediately blocks on the already-held mutex and deadlocks the int...
Open Graph Description: What happened? iterator.__length_hint__ holds its internal PyMutex while calling the sequence’s __len__. A user __len__ can call back into the same __length_hint__, which immediately blocks on the ...
X Description: What happened? iterator.__length_hint__ holds its internal PyMutex while calling the sequence’s __len__. A user __len__ can call back into the same __length_hint__, which immediately blocks on the ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/6590
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deadlock when `__len__` re-enters `__length_hint__` via list iterator","articleBody":"### What happened?\n\n`iterator.__length_hint__` holds its internal `PyMutex` while calling the sequence’s `__len__`. A user `__len__` can call back into the same `__length_hint__`, which immediately blocks on the already-held mutex and deadlocks the interpreter instead of raising an error or progressing the iterator.\n\n**Proof of Concept:**\n```python\nit = None\n\nclass Evil:\n def __getitem__(self, index):\n if index == 0:\n return 0\n raise IndexError\n\n def __len__(self):\n it.__length_hint__()\n return 1\n\nobj = Evil()\nit = iter(obj)\nit.__length_hint__()\n```\n\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eAffected Versions\u003c/strong\u003e\u003c/summary\u003e\n\n| RustPython Version | Status | Exit Code |\n|---|---|---|\n| `Python 3.13.0alpha (heads/main-dirty:21300f689, Dec 13 2025, 22:16:49) [RustPython 0.4.0 with rustc 1.90.0-nightly (11ad40bb8 2025-06-28)]` | Deadlock | 124 |\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eVulnerable Code\u003c/strong\u003e\u003c/summary\u003e\n\n```c\n#[pymethod]\nfn __length_hint__(\u0026self, vm: \u0026VirtualMachine) -\u003e PyObjectRef {\n let internal = self.internal.lock(); // holds PyMutex across user callbacks\n if let IterStatus::Active(obj) = \u0026internal.status {\n let seq = PySequence { obj, methods: self.seq_methods };\n seq.length(vm) // invokes user __len__. PoC calls iterator.__length_hint__ again while lock is held\n .map(|x| PyInt::from(x).into_pyobject(vm))\n .unwrap_or_else(|_| vm.ctx.not_implemented())\n } else {\n PyInt::from(0).into_pyobject(vm)\n }\n}\n// Inner __length_hint__ call tries to lock the same mutex again, blocking forever → deadlock.\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRust Output\u003c/strong\u003e\u003c/summary\u003e\n\n```\nProgram hangs forever\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eCPython Output\u003c/strong\u003e\u003c/summary\u003e\n\n```\nTraceback (most recent call last):\n File \"\u003cstring\u003e\", line 15, in \u003cmodule\u003e\n File \"\u003cstring\u003e\", line 10, in __len__\n File \"\u003cstring\u003e\", line 10, in __len__\n File \"\u003cstring\u003e\", line 10, in __len__\n [Previous line repeated 496 more times]\nRecursionError: maximum recursion depth exceeded\n\n```\n\u003c/details\u003e\n\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-30T04:55:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/6590/RustPython/issues/6590"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a79d98d2-f5e1-1b0b-991a-cf5bfd044f0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A8A:16E640:2DC479D:3F4365C:6964F2AC |
| html-safe-nonce | 02bed70bd5e682176a1fc435aae22227ab1ac5597f152f52b76cb748ee847422 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QThBOjE2RTY0MDoyREM0NzlEOjNGNDM2NUM6Njk2NEYyQUMiLCJ2aXNpdG9yX2lkIjoiMjM0NzI2MjI0OTY1NTM5OTA4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eb5bb5d74b8202a0c74579477a71dcb5a7e4d0e590dfe227120af0dd09359112 |
| hovercard-subject-tag | issue:3769052915 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/6590/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7e2e8a28da72e24ea36e0aea935712c508d0236ca263d6eea5a51610d0d4a489/RustPython/RustPython/issues/6590 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7e2e8a28da72e24ea36e0aea935712c508d0236ca263d6eea5a51610d0d4a489/RustPython/RustPython/issues/6590 |
| og:image:alt | What happened? iterator.__length_hint__ holds its internal PyMutex while calling the sequence’s __len__. A user __len__ can call back into the same __length_hint__, which immediately blocks on the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jackfromeast |
| hostname | github.com |
| expected-hostname | github.com |
| None | 53cd8ae9b65b7c4a0ad545ffeb3216839259a92733857ba426906c0655c5419d |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c140758368c0a01881f61b83770bd75cfe81e2c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width