René's URL Explorer Experiment


Title: JIT can trigger assert(uop_buffer_remaining_space(trace) > space_needed); for widened uops · Issue #149335 · python/cpython · GitHub

Open Graph Title: JIT can trigger assert(uop_buffer_remaining_space(trace) > space_needed); for widened uops · Issue #149335 · python/cpython

X Title: JIT can trigger assert(uop_buffer_remaining_space(trace) > space_needed); for widened uops · Issue #149335 · python/cpython

Description: Bug report Bug description: The assert(uop_buffer_remaining_space(trace) > space_needed); in _PyJit_translate_single_bytecode_to_trace can be triggered if a uop is widened. Found when the CI for #148271 was failing (there TO_BOOL gets an...

Open Graph Description: Bug report Bug description: The assert(uop_buffer_remaining_space(trace) > space_needed); in _PyJit_translate_single_bytecode_to_trace can be triggered if a uop is widened. Found when the CI for #1...

X Description: Bug report Bug description: The assert(uop_buffer_remaining_space(trace) > space_needed); in _PyJit_translate_single_bytecode_to_trace can be triggered if a uop is widened. Found when the CI for...

Opengraph URL: https://github.com/python/cpython/issues/149335

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JIT can trigger assert(uop_buffer_remaining_space(trace) \u003e space_needed); for widened uops","articleBody":"# Bug report\n\n### Bug description:\n\n\nThe `assert(uop_buffer_remaining_space(trace) \u003e space_needed);` in `_PyJit_translate_single_bytecode_to_trace` can be triggered if a uop is widened. Found when the CI for #148271 was failing (there `TO_BOOL` gets an extra `_RECORD_TOS_TYPE`). @markshannon \n\nReporting the issue already, I will try to create a simple reproducer and more concise explanation.\n\n\u003cdetails\u003e\u003csummary\u003eFull explanation by Claude\u003c/summary\u003e\n\n# JIT trace-buffer assertion: full explanation\n\n## The assertion\n\nIn `_PyJit_translate_single_bytecode_to_trace` (Python/optimizer.c), each bytecode appended to a tier-2 trace is gated by a Py_DEBUG assertion:\n\n```\nassert(uop_buffer_remaining_space(trace) \u003e space_needed);\n```\nwhere\n```\nspace_needed = expansion-\u003enuops + needs_guard_ip + 2 + (!OPCODE_HAS_NO_SAVE_IP(opcode))\n```\n\n`expansion-\u003enuops` is the number of uops the bytecode's macro expands to. The `+ 2` covers the `_CHECK_VALIDITY` uop emitted at the start of every bytecode plus a reserved `_DEOPT` slot for it. `+ needs_guard_ip` covers the `_GUARD_IP_*` slot for opcodes that need it. `+ (!OPCODE_HAS_NO_SAVE_IP)` covers the `_SET_IP` slot.\n\nJust before the assertion, `trace-\u003eend` has already been decremented to reserve the side-exit/error/deopt/guard-ip stubs at the buffer's tail:\n\n```\ntrace-\u003eend -= 2 + has_exit + has_error + has_deopt + needs_guard_ip\n```\n\nSo if `R` is the buffer's true remaining space at the start of the bytecode, the assertion is checking `R - tail \u003e space_needed`, i.e.\n\n```\nR \u003e 4 + has_exit + has_error + has_deopt + 2*needs_guard_ip + nuops + has_save_ip\n```\n\n## The fitness mechanism\n\n`_PyJit_translate_single_bytecode_to_trace` also runs a fitness gate before the assertion:\n\n```\nif (fitness \u003c eq) goto done;\n```\n\n`eq` is `compute_exit_quality(opcode)`. For specializable opcodes (like `TO_BOOL`, `CALL_*`) it returns `EXIT_QUALITY_SPECIALIZABLE`, defined as `FITNESS_INITIAL / 80`. With `FITNESS_INITIAL == UOP_MAX_TRACE_LENGTH == 1000` in debug builds, that's **12**.\n\nPer bytecode, fitness is debited by `slots_used = remaining_before − remaining_after`, i.e. exactly the number of buffer slots the bytecode just consumed (uops emitted + tail reservations made). So fitness and remaining_space are *intended* to track in lockstep, with branch/frame penalties only making fitness drop *faster* (which would only ever make the assertion safer).\n\nThe intended invariant is therefore `fitness \u003c= remaining_space`. If that held, then `fitness \u003e= eq = 12` would imply `remaining_space \u003e= 12` too, and 12 slots is enough for every existing `space_needed`.\n\n## Where the invariant breaks\n\nTwo accounting holes push `fitness` *above* `remaining_space`:\n\n1. **Trace prelude not charged.** `_PyJit_StartTracing` emits `_START_EXECUTOR` and `_MAKE_WARM` into the buffer, costing 2 slots. Fitness is initialized to `FITNESS_INITIAL` (1000) right after, with no debit. So before the first bytecode: `fitness = 1000`, `remaining = 998`. Drift = **+2**.\n\n2. **`needs_guard_ip` under-reserved.** When `needs_guard_ip` is true (e.g. after `_PUSH_FRAME`), the code emits up to three uops (`_RECORD_CODE`, the `guard_ip` uop itself, and `guard_code_version_uop` if the called object is a code object). But `space_needed` only adds `+ needs_guard_ip` (i.e. 1). Each such occurrence under-reserves by 1–2 slots, but those slots *are* still consumed by `next++`, so `slots_used` does include them and fitness drops correctly. The mismatch is between the predicted `space_needed` and the actual emission, not between fitness and buffer — but it tightens the assertion further.\n\nI confirmed the drift directly. With a release build with `UOP_MAX_TRACE_LENGTH` hacked down to 1000 (matching debug) and a printf inserted at the trace-finalize site, a long branchless reproducer produced:\n\n```\nTRACE_LEN: length=666 remaining=14 fitness=18 UOP_MAX=1000\n```\n\n`fitness = 18, remaining = 14` — fitness sits 4 slots higher than the buffer.\n\n## Why this PR exposes it\n\nThe PR for gh-143732 changes the `TO_BOOL` macro from\n```\nmacro(TO_BOOL) = _SPECIALIZE_TO_BOOL + unused/2 + _TO_BOOL;\n```\nto\n```\nmacro(TO_BOOL) = _SPECIALIZE_TO_BOOL + _RECORD_TOS_TYPE + unused/2 + _TO_BOOL;\n```\n\nThat bumps `expansion-\u003enuops` for `TO_BOOL` from 1 to 2.\n\nConcrete `space_needed` for `TO_BOOL` (no exit/deopt/guard_ip, has_error, has_save_ip):\n\n| | nuops | needs_guard_ip | +2 | has_save_ip | **space_needed** |\n|---|---|---|---|---|---|\n| main | 1 | 0 | 2 | 1 | **4** |\n| PR   | 2 | 0 | 2 | 1 | **5** |\n\nAfter tail reservations (`trace-\u003eend -= 2 + has_error = 3`), the post-decrement remaining must exceed `space_needed`. So the true-remaining `R` we need is:\n\n| | tail | space_needed | **R required** |\n|---|---|---|---|\n| main | 3 | 4 | **\u003e 7** |\n| PR   | 3 | 5 | **\u003e 8** |\n\nWorst case at the fitness gate: `fitness == 12` and the drift is `fitness − remaining = +4`, so `R = 8`. On main, `8 \u003e 7` ✓. On the PR, `8 \u003e 8` ✗ — assertion fires.\n\n## Empirical confirmation\n\nDebug + JIT build, 30 runs of `./python -m test test_strtod --multiprocess 0 --verbose2 --verbose3`:\n\n| Build | TO_BOOL macro | Pass | Fail |\n|---|---|---|---|\n| main | original | 30 | 0 |\n| main + PR's macro change | with `_RECORD_TOS_TYPE` | 28 | 2 |\n\nFailures are always in `test_short_halfway_cases`, which uses `random.randrange`, so the failing trace shape varies and the failure is flaky (~5–10%). The C backtrace lands in `_PyJit_translate_single_bytecode_to_trace` reached from the test's `if e + q.bit_length() \u003e max_exp:` line — so the bytecode that actually trips the assertion is plausibly a wide CALL (e.g. `CALL_METHOD_DESCRIPTOR_NOARGS` for `q.bit_length()`), not `TO_BOOL` itself; widening `TO_BOOL` just shifts trace timing enough to expose it.\n\n## Bottom line\n\nIt's a thin-margin coupling, not a PR bug. The intended invariant `fitness \u003c= remaining_space` doesn't actually hold today; in some traces fitness is ~4 slots above remaining. With `EXIT_QUALITY_SPECIALIZABLE = 12`, the worst-case assertion threshold is right at the boundary for the PR's wider `TO_BOOL`, so any future macro widening can resurface this. Fixes (in increasing principled-ness): (a) graceful `goto done` instead of asserting; (b) raise `EXIT_QUALITY_SPECIALIZABLE` to ~25 to leave room for the widest opcode plus the +4 drift; (c) fix the two accounting holes so the invariant actually holds.\n\n\u003c/details\u003e\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-149633\n* gh-150245\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2026-05-03T20:54:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/149335/cpython/issues/149335"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:543139ac-5a01-c88d-7469-3c80092fb7b0
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id956E:3CF864:3AA3FE:4E187D:6A516BE6
html-safe-nonce4bfa9db63d0d5c792a5d3cd344b7d683bd07374c2afffbd7e38f3e79ff563595
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTZFOjNDRjg2NDozQUEzRkU6NEUxODdEOjZBNTE2QkU2IiwidmlzaXRvcl9pZCI6IjYxODc2MjM5ODYyODY5MTQ1MzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaca37902e41900dc0f9a1b3654edf4a428656e49ade2c2f87a83eab3842beaadcf
hovercard-subject-tagissue:4372954630
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149335/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1d10c86efaf16868e428d56a370a036b4ef16b53937e6ce7382dbb0e08ae6aa2/python/cpython/issues/149335
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1d10c86efaf16868e428d56a370a036b4ef16b53937e6ce7382dbb0e08ae6aa2/python/cpython/issues/149335
og:image:altBug report Bug description: The assert(uop_buffer_remaining_space(trace) > space_needed); in _PyJit_translate_single_bytecode_to_trace can be triggered if a uop is widened. Found when the CI for #1...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameeendebakpt
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release60eaaf6d4dbaa817031b556aebd8a0a7eeb81b97
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/149335#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F149335
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F149335
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/149335
Reloadhttps://github.com/python/cpython/issues/149335
Reloadhttps://github.com/python/cpython/issues/149335
Please reload this pagehttps://github.com/python/cpython/issues/149335
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/149335
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
JIT can trigger assert(uop_buffer_remaining_space(trace) > space_needed); for widened uopshttps://github.com/python/cpython/issues/149335#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-JIThttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-JIT%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/eendebakpt
eendebakpthttps://github.com/eendebakpt
on May 3, 2026https://github.com/python/cpython/issues/149335#issue-4372954630
#148271https://github.com/python/cpython/pull/148271
@markshannonhttps://github.com/markshannon
gh-143732https://github.com/python/cpython/issues/143732
gh-149335: Avoid JIT trace buffer asserts with overhead above FITNESS_INITIAL #149633https://github.com/python/cpython/pull/149633
[3.15] gh-149335: Avoid JIT trace buffer asserts with overhead above FITNESS_INITIAL (GH-149633) #150245https://github.com/python/cpython/pull/150245
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-JIThttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-JIT%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.