Title: Executor management code lacks proper invariants leading to bugs. · Issue #142513 · python/cpython · GitHub
Open Graph Title: Executor management code lacks proper invariants leading to bugs. · Issue #142513 · python/cpython
X Title: Executor management code lacks proper invariants leading to bugs. · Issue #142513 · python/cpython
Description: Bug report Bug description: Our lifetime management of JIT executors is ad-hoc and has some logical flaws. This results in bugs, and is hard to fix as fixing one bug often seems to produce another. Some examples: #139540 #141658 #141786 ...
Open Graph Description: Bug report Bug description: Our lifetime management of JIT executors is ad-hoc and has some logical flaws. This results in bugs, and is hard to fix as fixing one bug often seems to produce another....
X Description: Bug report Bug description: Our lifetime management of JIT executors is ad-hoc and has some logical flaws. This results in bugs, and is hard to fix as fixing one bug often seems to produce another....
Opengraph URL: https://github.com/python/cpython/issues/142513
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Executor management code lacks proper invariants leading to bugs.","articleBody":"# Bug report\n\n### Bug description:\n\nOur lifetime management of JIT executors is ad-hoc and has some logical flaws.\nThis results in bugs, and is hard to fix as fixing one bug often seems to produce another.\n\nSome examples:\n\nhttps://github.com/python/cpython/issues/139540\nhttps://github.com/python/cpython/issues/141658\nhttps://github.com/python/cpython/issues/141786\n\n\n## Executor management overview\n\nExecutors are reference counted `PyObject` objects allowing them to be interacted with from Python.\nFor performance reasons, the references to currently executing executors are deferred (not counted).\nExecutors can be invalidated at any time, even when it would be unsafe to deallocate an object.\nInvalidation means that the machine code for that executor is no longer valid. The executor object remains a valid object.\n\n### Invalidation\n\nInvalidating an executor does not make it unreachable, but in order to free executors as soon as\npossible, when an executor is made invalid, we delete references to all executors that are side exits\nfrom the invalidated executor. The reference from the code object is also deleted.\nInvalidation can happen at any time, so care must be taken not to break any invariants when\ninvalidating executors.\n\n### Refcount dropping to zero\n\nBecause references to currently executing executors are deferred, executors may still be live when\ntheir reference count drops to zero.\nTo handle this, when an executor's refcount drops to zero, it should not be deallocated, but moved to a \"zero count\" list.\nAlso, deferring deallocation means that it safe to call `Py_DECREF()` on an executor, even if it would not be safe to deallocate it.\n\n### Cycles\n\nSince cycles can be formed between executors, we need the cycle GC to handle executors.\nWhen executors become invalid, we should break any cycles involving them.\n\n## Current implementation\n\nThe current implementation (2025-12-01) maintains a doubly linked list of all executors.\nThis list is scanned when a dependency is broken, `_Py_Executors_InvalidateDependency` and `_Py_Executors_InvalidateAll`, to invalidate exectors.\nThere is also a second double linked list, `executor_deletion_list`.\nWhen the reference count of an executor reacahes zero, it is moved from the main list\nto the deletion list. This helps the GC find any executors with a zero reference count quickly.\n\nWhen the `executor_deletion_list` reaches a certain capacity, we clear it.\nThis is broken. We should only do this during cycle GC.\n\nNote that invalidation and unreachability (being garbage) are two different things.\nWhen an executor becomes invalid, it is useful to drop some references to and from it,\nbut it could still be being executed and may still be reachable, so its integrity as an\nobject must be maintained.\nWhen an executor becomes unreachable, it should be GC'd like any other object, but\nwhen its reference count drops to zero it may still be being executed and should be kept\nintact.\n\n## Proposed implementation\n\n### Invalidation\n\n1. Detach the executor from the code object\n2. Clear all exits.\n\n### Refcount dropping to zero\n\n* Move the executor to the `executor_deletion_list`\n* Invalidate the executor\n\n### `tp_clear` \n\n* Clear all exits and any constants\n\n### Change to the cycle GC\n\n* Move all currently executing executors in the `executor_deletion_list` to the `currently_executing_list`, incrementing the reference count of each.\n* Deallocate all executors in the `executor_deletion_list` \n* Perform cycle GC as normal\n* For all executors in the `currently_executing_list`:\n * decrement the refcount, which will move it back to the `executor_deletion_list` if its refcount drops to zero.\n\n\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-142931\n* gh-143073\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2025-12-10T14:11:59Z","url":"https://github.com/python/cpython/issues/142513"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:8f496222-811c-9496-781a-1d01c5b38862 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE7A:2C4BED:479FBCD:5AC15A2:6B024469 |
| html-safe-nonce | eb374ee0a6fce7cd80dfb279b96ad567ff23402cdd01b9356897953eab4aba43 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTdBOjJDNEJFRDo0NzlGQkNEOjVBQzE1QTI6NkIwMjQ0NjkiLCJ2aXNpdG9yX2lkIjoiMzY2OTMzMDc0MTkxMDcxNzU0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e8fda06975c7fc28d7389b483054be3f91215af5e0f48e529d5bcede59adef68 |
| hovercard-subject-tag | issue:3715373230 |
| 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/python/cpython/142513/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11bb4df9c854e9883884b269ca93613dfe79996987c0a1c576aae8c0a16b0b8a/python/cpython/issues/142513 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11bb4df9c854e9883884b269ca93613dfe79996987c0a1c576aae8c0a16b0b8a/python/cpython/issues/142513 |
| og:image:alt | Bug report Bug description: Our lifetime management of JIT executors is ad-hoc and has some logical flaws. This results in bugs, and is hard to fix as fixing one bug often seems to produce another.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markshannon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width