Title: Deadlock when sentinel equality re-enters iterator via `iter(callable, sentinel)` · Issue #6589 · RustPython/RustPython · GitHub
Open Graph Title: Deadlock when sentinel equality re-enters iterator via `iter(callable, sentinel)` · Issue #6589 · RustPython/RustPython
X Title: Deadlock when sentinel equality re-enters iterator via `iter(callable, sentinel)` · Issue #6589 · RustPython/RustPython
Description: What happened? PyCallableIterator::next keeps an upgradable read lock while comparing the callable result to the sentinel with vm.bool_eq. If the sentinel’s __eq__ calls next() on the same iterator (as in the PoC), the re-entrant next bl...
Open Graph Description: What happened? PyCallableIterator::next keeps an upgradable read lock while comparing the callable result to the sentinel with vm.bool_eq. If the sentinel’s __eq__ calls next() on the same iterator...
X Description: What happened? PyCallableIterator::next keeps an upgradable read lock while comparing the callable result to the sentinel with vm.bool_eq. If the sentinel’s __eq__ calls next() on the same iterator...
Opengraph URL: https://github.com/RustPython/RustPython/issues/6589
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deadlock when sentinel equality re-enters iterator via `iter(callable, sentinel)`","articleBody":"### What happened?\n\n`PyCallableIterator::next` keeps an upgradable read lock while comparing the callable result to the sentinel with `vm.bool_eq`. If the sentinel’s `__eq__` calls `next()` on the same iterator (as in the PoC), the re-entrant `next` blocks on the same lock, hanging the interpreter instead of finishing iteration.\n\n**Proof of Concept:**\n```python\nclass Evil:\n def __init__(self):\n self.it = None\n def __eq__(self, other):\n next(self.it)\n return True\n\nevil = Evil()\nitr = iter(lambda: evil, evil)\nevil.it = itr\nnext(itr)\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\nfn next(zelf: \u0026Py\u003cSelf\u003e, vm: \u0026VirtualMachine) -\u003e PyResult\u003cPyIterReturn\u003e {\n let status = zelf.status.upgradable_read(); // holds the lock across user comparisons\n let next = if let IterStatus::Active(callable) = \u0026*status {\n let ret = callable.invoke((), vm)?; // user callable returns a value that may be sentinel\n if vm.bool_eq(\u0026ret, \u0026zelf.sentinel)? { // runs sentinel.__eq__, which can call next() again\n *PyRwLockUpgradableReadGuard::upgrade(status) = IterStatus::Exhausted;\n PyIterReturn::StopIteration(None)\n } else {\n PyIterReturn::Return(ret)\n }\n } else {\n PyIterReturn::StopIteration(None)\n };\n Ok(next) // re-entrant next() blocks on the upgradable_read above, causing deadlock\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRust Output\u003c/strong\u003e\u003c/summary\u003e\n\n```\nError: Execution timed out\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 11, in \u003cmodule\u003e\nStopIteration\n\n```\n\u003c/details\u003e\n\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-30T04:55:44.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/6589/RustPython/issues/6589"}
| 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:4350dfc1-252d-599b-6877-8eb8d04b59d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B64C:B17C5:161E356:1ED0C2B:69651C63 |
| html-safe-nonce | 581e88bda1efdeb6c67f158353f3b8f8a0e9ada45933a47041904a661c37fdbe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjRDOkIxN0M1OjE2MUUzNTY6MUVEMEMyQjo2OTY1MUM2MyIsInZpc2l0b3JfaWQiOiI2NDU2Nzc5MDUxMTU2OTcwNTk1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c6d60981d3bb523ddc8d4fc8103d1abe3975a6453f1655780908c12fc2e897e8 |
| hovercard-subject-tag | issue:3769052658 |
| 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/6589/issue_layout |
| twitter:image | https://opengraph.githubassets.com/599231b4ca667a8fd374b7424c9c3aff026731734dd44eac22b36e5f63336a5c/RustPython/RustPython/issues/6589 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/599231b4ca667a8fd374b7424c9c3aff026731734dd44eac22b36e5f63336a5c/RustPython/RustPython/issues/6589 |
| og:image:alt | What happened? PyCallableIterator::next keeps an upgradable read lock while comparing the callable result to the sentinel with vm.bool_eq. If the sentinel’s __eq__ calls next() on the same iterator... |
| 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 | 78ed63d2e65e1c05681fba8eb2bd7ca09088f264570071f03eaf49d14d9bad87 |
| 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 | 6d0bd2cfa31184bb64dc21a57a0fa776eb79accc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width