Title: JIT: stale `recorded_values` leak when specialized op deopts · Issue #148571 · python/cpython · GitHub
Open Graph Title: JIT: stale `recorded_values` leak when specialized op deopts · Issue #148571 · python/cpython
X Title: JIT: stale `recorded_values` leak when specialized op deopts · Issue #148571 · python/cpython
Description: Bug report Bug description: TRACE_RECORD fills recorded_values before dispatching the instruction body cpython/Python/bytecodes.c Lines 6366 to 6372 in 52a7f1b const _PyOpcodeRecordEntry *record_entry = &_PyOpcode_RecordEntries[opcode]; ...
Open Graph Description: Bug report Bug description: TRACE_RECORD fills recorded_values before dispatching the instruction body cpython/Python/bytecodes.c Lines 6366 to 6372 in 52a7f1b const _PyOpcodeRecordEntry *record_en...
X Description: Bug report Bug description: TRACE_RECORD fills recorded_values before dispatching the instruction body cpython/Python/bytecodes.c Lines 6366 to 6372 in 52a7f1b const _PyOpcodeRecordEntry *record_en...
Opengraph URL: https://github.com/python/cpython/issues/148571
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JIT: stale `recorded_values` leak when specialized op deopts","articleBody":"# Bug report\n\n### Bug description:\n\n1. `TRACE_RECORD` fills `recorded_values` before dispatching the instruction body\n\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/bytecodes.c#L6366-L6372\n\nAt this point, `recorded_values[0..record_entry->count-1]` hold the values recorded for the specialised opcode that is about to run. For `LOAD_ATTR_CLASS_WITH_METACLASS_CHECK` in #148394, the record entry is `_RECORD_TOS`, so `recorded_values[0]` holds the raw TOS (`PyObject *` itself). Unlike `_RECORD_TOS_TYPE` stores `Py_TYPE(tos)`, which is always a `PyTypeObject *`.\n\n2. The specialised body hits `EXIT_IF` when its guard fails. Then `EXIT_IF` / `DEOPT_IF` compile down to a plain `goto` through `JUMP_TO_PREDICTED`, with no cleanup in the emitted code:\n\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Tools/cases_generator/generators_common.py#L190\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/ceval_macros.h#L136\nNothing on this path touches `tracer->prev_state.recorded_values[]`.\n\n3. `_SPECIALIZE_LOAD_ATTR` calls the adaptive specializer at the same pc.\n\nCall chain:\n\u003cp>\u003ca href=\"https://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/bytecodes.c#L2705\">\u003ccode>_SPECIALIZE_LOAD_ATTR\u003c/code>\u003c/a>\u003cbr>\n → \u003ca href=\"https://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/specialize.c#L992\">\u003ccode>_Py_Specialize_LoadAttr\u003c/code>\u003c/a>\u003cbr>\n → \u003ca href=\"https://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/specialize.c#L886\">\u003ccode>specialize(instr, LOAD_ATTR_SLOT)\u003c/code>\u003c/a>\u003cbr>\n → \u003ca href=\"https://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/specialize.c#L363\">\u003ccode>set_opcode(instr, LOAD_ATTR_SLOT)\u003c/code>\u003c/a>\u003cbr>\n → \u003ca href=\"https://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/specialize.c#L338\">\u003ccode>instr->op.code = opcode;\u003c/code>\u003c/a>\u003c/p>\n\nOnce `_Py_Specialize_LoadAttr` returns, the pc that previously held `LOAD_ATTR_CLASS_WITH_METACLASS_CHECK` (181) now holds, for example, `LOAD_ATTR_SLOT` (191). Control then falls through to `DISPATCH_SAME_OPARG()`, which re-reads the opcode byte and dispatches the new specialisation at the same pc:\n\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/ceval_macros.h#L216-L221\nBecause that dispatch uses `DISPATCH_GOTO_NON_TRACING`, the new specialisation runs without re-entering `TRACE_RECORD`.\n\n4. After the re-specialised op completes, the next `TRACE_RECORD` translates against a stale picture.\n\nIt reads `prev_state.instr->op.code`. `prev_state.instr` is still the pointer set in step 1; only the byte at that address has changed.\n\n5. Translate feeds the stale recording into the new opcode's uops\n\nInside translate, each uop in the macro expansion of the current opcode byte (e.g. 191 = `LOAD_ATTR_SLOT`) that carries `HAS_RECORDS_VALUE_FLAG` is handed `recorded_values[record_idx]` as its `operand0`. There is no check that the recording actually came from the opcode currently being translated:\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/optimizer.c#L949-L954\n\n`LOAD_ATTR_SLOT` begins with `_RECORD_TOS_TYPE`, which expects a `PyTypeObject`:\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/bytecodes.c#L2883-L2890\n\nBut the value still sitting in `recorded_values[0]` is whatever `_RECORD_TOS` stored for the old 181 — Boom.\n\n---\n\nThe only existing cleanup in `_PyJit_FinalizeTracing` runs when tracing ends — far too late for a deopt that occurs mid-trace:\n\nhttps://github.com/python/cpython/blob/52a7f1b7f88d7f25e4cf02b6bc5e7521848d99f0/Python/optimizer.c#L1126-L1129\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148730\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/NekoAsakura","@type":"Person","name":"NekoAsakura"},"datePublished":"2026-04-14T19:58:39Z","url":"https://github.com/python/cpython/issues/148571"}
| 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:255b170d-9fc5-c078-2b41-adefeca827ac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDA4:DB09F:38BDA9E:486B62C:6B020217 |
| html-safe-nonce | 6911cc40f386e902938f4354e59d282ab02124c4212b66ae047da35da44356d7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREE0OkRCMDlGOjM4QkRBOUU6NDg2QjYyQzo2QjAyMDIxNyIsInZpc2l0b3JfaWQiOiI3NzczMzM5NTA1NTUzMTc5MTU5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e84ae87a79d06a041bae7b3d73fcfbcf8793620a420661dbb830f885047fff36 |
| hovercard-subject-tag | issue:4264418520 |
| 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/148571/issue_layout |
| twitter:image | https://opengraph.githubassets.com/29e1512fbbdd8a054a5f40a46229f1b5725731518138467eb3944990fab84d81/python/cpython/issues/148571 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/29e1512fbbdd8a054a5f40a46229f1b5725731518138467eb3944990fab84d81/python/cpython/issues/148571 |
| og:image:alt | Bug report Bug description: TRACE_RECORD fills recorded_values before dispatching the instruction body cpython/Python/bytecodes.c Lines 6366 to 6372 in 52a7f1b const _PyOpcodeRecordEntry *record_en... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | NekoAsakura |
| 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