Title: JIT: Assertion `PyType_Check((PyObject *)type)` failed · Issue #149112 · python/cpython · GitHub
Open Graph Title: JIT: Assertion `PyType_Check((PyObject *)type)` failed · Issue #149112 · python/cpython
X Title: JIT: Assertion `PyType_Check((PyObject *)type)` failed · Issue #149112 · python/cpython
Description: Crash report What happened? It's possible to make a JIT build abort by running the code below. MRE: def f1(): class C: def __getitem__(self, key): pass c = C() for _ in range(300): c[2] for _ in range(300): f1() Backtrace: python: Python...
Open Graph Description: Crash report What happened? It's possible to make a JIT build abort by running the code below. MRE: def f1(): class C: def __getitem__(self, key): pass c = C() for _ in range(300): c[2] for _ in ra...
X Description: Crash report What happened? It's possible to make a JIT build abort by running the code below. MRE: def f1(): class C: def __getitem__(self, key): pass c = C() for _ in range(300): c[2] for _ i...
Opengraph URL: https://github.com/python/cpython/issues/149112
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JIT: Assertion `PyType_Check((PyObject *)type)` failed","articleBody":"# Crash report\n\n### What happened?\n\nIt's possible to make a JIT build abort by running the code below.\n\n\nMRE:\n```python\ndef f1():\n class C:\n def __getitem__(self, key): pass\n\n c = C()\n for _ in range(300):\n c[2]\n\nfor _ in range(300):\n f1()\n```\n\nBacktrace:\n```\npython: Python/optimizer_symbols.c:1277: void _Py_uop_sym_set_recorded_type(JitOptContext *, JitOptRef, PyTypeObject *): Assertion `PyType_Check((PyObject *)type)' failed.\n\nProgram received signal SIGABRT, Aborted.\n\n#0 __pthread_kill_implementation (threadid=\u003coptimized out>, signo=6, no_tid=0) at ./nptl/pthread_kill.c:44\n#1 __pthread_kill_internal (threadid=\u003coptimized out>, signo=6) at ./nptl/pthread_kill.c:89\n#2 __GI___pthread_kill (threadid=\u003coptimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:100\n#3 0x00007ffff7c45e2e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26\n#4 0x00007ffff7c28888 in __GI_abort () at ./stdlib/abort.c:77\n#5 0x00007ffff7c287f0 in __assert_fail_base (fmt=\u003coptimized out>, assertion=\u003coptimized out>, file=\u003coptimized out>, line=\u003coptimized out>, function=\u003coptimized out>) at ./assert/assert.c:118\n#6 0x00007ffff7c3c19f in __assert_fail (assertion=\u003coptimized out>, file=\u003coptimized out>, line=\u003coptimized out>, function=\u003coptimized out>) at ./assert/assert.c:127\n#7 0x000055555610a025 in _Py_uop_sym_set_recorded_type (ctx=ctx@entry=0x7bfff480b890, ref=ref@entry=..., type=type@entry=0x7d2ff6e37870) at Python/optimizer_symbols.c:1277\n#8 0x00005555560ebe73 in optimize_uops (tstate=\u003coptimized out>, trace=\u003coptimized out>, trace_len=\u003coptimized out>, curr_stacklen=\u003coptimized out>, output=\u003coptimized out>,\n dependencies=\u003coptimized out>) at Python/optimizer_cases.c.h:5651\n#9 0x00005555560e4728 in _Py_uop_analyze_and_optimize (tstate=tstate@entry=0x555556a754b8 \u003c_PyRuntime+360760>, buffer=buffer@entry=0x7bfff483e2e0, length=6, length@entry=14,\n curr_stacklen=curr_stacklen@entry=4, output=0x16, output@entry=0x7bfff484cd40, dependencies=dependencies@entry=0x7bfff5a849a0) at Python/optimizer_analysis.c:796\n#10 0x00005555560d4b0a in uop_optimize (frame=0x7e8ff6de5290, tstate=0x555556a754b8 \u003c_PyRuntime+360760>, progress_needed=false, exec_ptr=\u003coptimized out>) at Python/optimizer.c:1646\n#11 _PyOptimizer_Optimize (frame=\u003coptimized out>, tstate=\u003coptimized out>) at Python/optimizer.c:166\n#12 0x0000555555ef42cb in stop_tracing_and_jit (tstate=0x555556a754b8 \u003c_PyRuntime+360760>, frame=frame@entry=0x7e8ff6de5290) at Python/ceval.c:1088\n#13 0x0000555555eb724a in _PyEval_EvalFrameDefault (tstate=\u003coptimized out>, frame=\u003coptimized out>, throwflag=\u003coptimized out>) at Python/generated_cases.c.h:13130\n#14 0x0000555555e89af8 in _PyEval_EvalFrame (tstate=0x555556a754b8 \u003c_PyRuntime+360760>, frame=0x7e8ff6de5220, throwflag=0) at ./Include/internal/pycore_ceval.h:118\n#15 _PyEval_Vector (tstate=\u003coptimized out>, func=\u003coptimized out>, locals=\u003coptimized out>, args=\u003coptimized out>, argcount=\u003coptimized out>, kwnames=0x0) at Python/ceval.c:2124\n#16 0x0000555555e89515 in PyEval_EvalCode (co=\u003coptimized out>, globals=\u003coptimized out>, locals=0x7c7ff6e89440) at Python/ceval.c:686\n#17 0x000055555617dad0 in run_eval_code_obj (tstate=tstate@entry=0x555556a754b8 \u003c_PyRuntime+360760>, co=co@entry=0x7d1ff6e86cd0, globals=globals@entry=0x7c7ff6e89440,\n locals=locals@entry=0x7c7ff6e89440) at Python/pythonrun.c:1369\n#18 0x000055555617cc9c in run_mod (mod=\u003coptimized out>, filename=\u003coptimized out>, globals=\u003coptimized out>, locals=\u003coptimized out>, flags=\u003coptimized out>, arena=\u003coptimized out>,\n interactive_src=\u003coptimized out>, generate_new_source=\u003coptimized out>) at Python/pythonrun.c:1472\n```\n\nOutput of running with `PYTHON_LLTRACE=4 PYTHON_OPT_DEBUG=4`:\n[lltrace_opt_debug.txt](https://github.com/user-attachments/files/27176415/lltrace_opt_debug.txt)\n\n\u003cdetails>\u003csummary>Claude's diagnosis and proposed fix\u003c/summary>\n\u003cp>\n\n## Diagnosis\n\nThe non-type pointer reaching `sym_set_recorded_type` originates as the `operand0` of a `_RECORD_TOS_TYPE` uop in the just-finalized trace. That `operand0` is set from `tracer->prev_state.recorded_values[i]` during translation. The values in those slots were populated by `_PyJit_TryInitializeTracing` (or by the `TRACE_RECORD` step) using the record-function schema for one opcode, but `_PyJit_translate_single_bytecode_to_trace` later interprets those same slots under a *different* opcode's schema, because translation has its own deopt-aware opcode rewrite that initialization does not mirror:\n\n```c\n// Python/optimizer.c, inside _PyJit_translate_single_bytecode_to_trace\nif (_PyOpcode_Caches[_PyOpcode_Deopt[opcode]] > 0) {\n uint16_t backoff = (this_instr + 1)->counter.value_and_backoff;\n if (backoff != adaptive_counter_cooldown().value_and_backoff &&\n backoff != trigger_backoff_counter().value_and_backoff) {\n opcode = _PyOpcode_Deopt[opcode];\n }\n}\n```\n\nThe MRE makes this visible with a single seam: each call to `f1()` defines a fresh class `C`, whose specialization cache for `BINARY_OP_SUBSCR_GETITEM` is empty on the first iteration. `_BINARY_OP_SUBSCR_CHECK_FUNC` therefore exits, and a side trace is requested at offset 36 — the same `BINARY_OP` instruction. At that moment:\n\n- the bytecode at offset 36 still reads `BINARY_OP_SUBSCR_GETITEM` (the specialization isn't physically rewritten); but\n- the adaptive counter is in neither cooldown nor trigger state, because tracing just deopted there.\n\nWalking through both functions:\n\n1. `_PyJit_TryInitializeTracing` (`Python/optimizer.c:1156`) reads `curr_instr->op.code` directly:\n ```c\n const _PyOpcodeRecordEntry *record_entry =\n &_PyOpcode_RecordEntries[curr_instr->op.code];\n ```\n That's `BINARY_OP_SUBSCR_GETITEM`, whose macro starts with `_RECORD_NOS` — a **value** recorder. So `recorded_values[0]` receives the NOS object itself: `stack[-2]`, which is the `C` *instance* (`Py_TYPE` is never called).\n\n2. `_PyJit_translate_single_bytecode_to_trace` runs the deopt shim shown above. The counter check fires, so `opcode` flips to plain `BINARY_OP`, whose macro is `_SPECIALIZE_BINARY_OP + _RECORD_TOS_TYPE + _RECORD_NOS_TYPE + unused/4 + _BINARY_OP + POP_TOP + POP_TOP`. Translation now emits `_RECORD_TOS_TYPE` and `_RECORD_NOS_TYPE` and consumes `recorded_values[0]` and `recorded_values[1]` as their `operand0`s.\n\n3. The optimizer's abstract interpreter (`case _RECORD_TOS_TYPE` at `Python/optimizer_cases.c.h:5651`):\n ```c\n PyTypeObject *tp = (PyTypeObject *)this_instr->operand0;\n sym_set_recorded_type(tos, tp);\n ```\n `tp` is the `C` instance. `PyType_Check(tp)` fails. Abort.\n\n(`recorded_values[1]` is also wrong — it's whatever the slot held before, since the GETITEM schema only ever wrote slot 0 — but it never gets reached because slot 0 trips the assertion first.)\n\nThis isn't specific to subscript: any opcode whose specialized form has a different record schema from its deopted form is vulnerable, since the schemas drive both the *set* of recorded slots and what each slot is assumed to be.\n\nCaptured optimizer trace at the failing site (truncated to the relevant uops):\n\n```\n 0 abs: _START_EXECUTOR (target=36)\n ...\n 3 abs: _SET_IP (target=36, operand0=\u003cbytecode>)\n 4 abs: _RECORD_TOS_TYPE (oparg=26, target=36, operand0=0x...ec30) ← C instance\n ABORT\n```\n\nAnd the lltrace just before:\n\n```\nSIDE EXIT: ... target 36 -> BINARY_OP_SUBSCR_GETITEM\nstack=[range_iter, NULL, \u003cC inst @0x...ec30>, \u003cint 2>]\n```\n\n## Fix\n\nApply the same deopt-aware opcode rewrite inside `_PyJit_TryInitializeTracing` before computing `record_entry`, so initialization and translation agree on which schema is in force. Local change in `Python/optimizer.c`:\n\n```c\n// Mirror the opcode-deopt decision in _PyJit_translate_single_bytecode_to_trace:\n// if the cache backoff indicates a deopt during prior tracing, the translator\n// will use the deopted opcode -- so we must record values for that opcode too,\n// otherwise the recorded_values produced here are interpreted under a\n// different uop-record schema (e.g. BINARY_OP_SUBSCR_GETITEM records 1 NOS\n// value while BINARY_OP records 2 type slots), and the resulting trace gets\n// a non-type pointer as the operand0 of _RECORD_TOS_TYPE.\nint record_opcode = curr_instr->op.code;\nif (_PyOpcode_Caches[_PyOpcode_Deopt[record_opcode]] > 0) {\n uint16_t backoff = (curr_instr + 1)->counter.value_and_backoff;\n if (backoff != adaptive_counter_cooldown().value_and_backoff &&\n backoff != trigger_backoff_counter().value_and_backoff) {\n record_opcode = _PyOpcode_Deopt[record_opcode];\n }\n}\nconst _PyOpcodeRecordEntry *record_entry = &_PyOpcode_RecordEntries[record_opcode];\n```\n\n`oparg` doesn't need to change — it's the bytecode-level oparg, identical for the specialization and its deopted form (e.g. `NB_SUBSCR` = 26 for both `BINARY_OP` and `BINARY_OP_SUBSCR_GETITEM`). The translator independently fetches its own `instr_oparg` out of `prev_state`, which `_PyJit_TryInitializeTracing` set from the same source. Only the record schema was inconsistent, and that's exactly what this rewrite repairs.\n\nFactoring the deopt check into a small shared helper used by both functions would also work and would be more robust against future drift; this snippet sticks to the minimum-surface change.\n\n## Verification\n\n- The MRE aborts on unmodified `main` and runs to completion (5/5) with the patch.\n- The same `TRACE_RECORD` path in `Python/generated_cases.c.h:12506` already runs on the *executed* opcode (after the JIT-tracing instruction has been resolved), so it's not affected; the bug is specific to side-trace start, where init reads the bytecode-as-stored.\n\n\u003c/p>\n\u003c/details>\n\nFound using [lafleur](https://github.com/devdanzin/lafleur/).\n\n### CPython versions tested on:\n\nCPython main branch, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a8+ (heads/main-dirty:804c213c893, Apr 28 2026, 08:55:31) [Clang 21.1.2 (2ubuntu6)]","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-04-28T17:57:47Z","url":"https://github.com/python/cpython/issues/149112"}
| 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:099fef51-a0e6-2366-c0fb-985ca2b0c522 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C488:3A4875:352F633:46D4A1F:6B0202A4 |
| html-safe-nonce | a186da3534e84fcc88ea3c504325a3cca51357d70d3fbb0379fa2ebceefde818 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDg4OjNBNDg3NTozNTJGNjMzOjQ2RDRBMUY6NkIwMjAyQTQiLCJ2aXNpdG9yX2lkIjoiNzk3MTM3MDk0NzEwNDA4MDU0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8dae5a994d09fad272ccabe4bfedfd783aad9969ea7e91d57cfeab10e19b8666 |
| hovercard-subject-tag | issue:4345205000 |
| 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/149112/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cafe8ea357fe268795dd3edb3cc4fea947c29fd38bbf4f7f6ddf64f0fd3dd70e/python/cpython/issues/149112 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cafe8ea357fe268795dd3edb3cc4fea947c29fd38bbf4f7f6ddf64f0fd3dd70e/python/cpython/issues/149112 |
| og:image:alt | Crash report What happened? It's possible to make a JIT build abort by running the code below. MRE: def f1(): class C: def __getitem__(self, key): pass c = C() for _ in range(300): c[2] for _ in ra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| 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