Title: Deadlock when element `__repr__` mutates the list via list `__repr__` · Issue #6591 · RustPython/RustPython · GitHub
Open Graph Title: Deadlock when element `__repr__` mutates the list via list `__repr__` · Issue #6591 · RustPython/RustPython
X Title: Deadlock when element `__repr__` mutates the list via list `__repr__` · Issue #6591 · RustPython/RustPython
Description: What happened? list.__repr__ holds a read-lock on the backing vector while rendering elements. If any element's __repr__ mutates the same list (e.g., append), the re-entrant call blocks on the write-lock while __repr__ waits for the elem...
Open Graph Description: What happened? list.__repr__ holds a read-lock on the backing vector while rendering elements. If any element's __repr__ mutates the same list (e.g., append), the re-entrant call blocks on the writ...
X Description: What happened? list.__repr__ holds a read-lock on the backing vector while rendering elements. If any element's __repr__ mutates the same list (e.g., append), the re-entrant call blocks on the ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/6591
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deadlock when element `__repr__` mutates the list via list `__repr__`","articleBody":"### What happened?\n\n`list.__repr__` holds a read-lock on the backing vector while rendering elements. If any element's `__repr__` mutates the same list (e.g., `append`), the re-entrant call blocks on the write-lock while `__repr__` waits for the element to finish, producing a hard deadlock instead of a Python-level exception.\n\n**Proof of Concept:**\n```python\nclass Evil:\n def __repr__(self):\n lst.append(1)\n return \"evil\"\n\nlst = [Evil()]\nprint(lst)\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 repr_str(zelf: \u0026Py\u003cSelf\u003e, vm: \u0026VirtualMachine) -\u003e PyResult\u003cString\u003e {\n if zelf.__len__() == 0 {\n return Ok(\"[]\".to_owned());\n }\n if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {\n let iter = zelf.borrow_vec().iter(); // holds a read lock on elements for the whole render\n return collection_repr(None, \"[\", \"]\", iter, vm); // re-enters element __repr__ while lock is held\n }\n Ok(\"[...]\".to_owned())\n}\n\npub(crate) fn collection_repr\u003c'a, I\u003e(\n class_name: Option\u003c\u0026str\u003e,\n prefix: \u0026str,\n suffix: \u0026str,\n iter: I,\n vm: \u0026VirtualMachine,\n) -\u003e PyResult\u003cString\u003e\nwhere\n I: Iterator\u003cItem = \u0026'a PyObjectRef\u003e,\n{\n let mut repr = String::new();\n let mut parts_iter = iter.map(|o| o.repr(vm)); // re-enters Python-level __repr__ for each element\n repr.push_str(\n parts_iter\n .next()\n .transpose()?\n .expect(\"this is not called for empty collection\")\n .as_str(),\n );\n // ...\n}\n\n#[pymethod]\npub(crate) fn append(\u0026self, x: PyObjectRef) {\n self.borrow_vec_mut().push(x); // attempts write lock while read lock from repr_str is held -\u003e 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```\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```\n[evil, 1]\n\n```\n\u003c/details\u003e\n\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-30T04:56:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/6591/RustPython/issues/6591"}
| 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:e0349578-9d69-e9d4-995d-7c100071539b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94E2:1527E5:1CB7:234D:6964F27B |
| html-safe-nonce | 3b8d555b7ceec193f47f676533232d6828cf3debaca9d394f98b5d0fd8fa5b70 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEUyOjE1MjdFNToxQ0I3OjIzNEQ6Njk2NEYyN0IiLCJ2aXNpdG9yX2lkIjoiNzA5MzkzOTI5NjM1NDQ5NzE0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 937890fc2cddbc9a9b69280e8bbe7c51c25bc289de83cf62ab1b921ab99482a9 |
| hovercard-subject-tag | issue:3769053198 |
| 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/6591/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5d34df5541380476ab1ffad3b8afec1d41eff94a32ef135af40de7869518029b/RustPython/RustPython/issues/6591 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5d34df5541380476ab1ffad3b8afec1d41eff94a32ef135af40de7869518029b/RustPython/RustPython/issues/6591 |
| og:image:alt | What happened? list.__repr__ holds a read-lock on the backing vector while rendering elements. If any element's __repr__ mutates the same list (e.g., append), the re-entrant call blocks on the writ... |
| 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 | 003e962bf5ca78f3f8f71db2461c6a7f9cfcbe99b480d6aeddaef78d33e9161a |
| 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 | 21cbf6733480c5c09366f15b8dc8b6953472172a |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width