René's URL Explorer Experiment


Title: `with lock`: can skip `__exit__` when a signal handler raises right after `__enter__` · Issue #148874 · python/cpython · GitHub

Open Graph Title: `with lock`: can skip `__exit__` when a signal handler raises right after `__enter__` · Issue #148874 · python/cpython

X Title: `with lock`: can skip `__exit__` when a signal handler raises right after `__enter__` · Issue #148874 · python/cpython

Description: Bug report A with X: statement can return without calling X.__exit__ when a Python signal handler raises between __enter__ returning and the body starting. This leaks whatever resource __enter__ acquired (e.g. a threading.Lock stays lock...

Open Graph Description: Bug report A with X: statement can return without calling X.__exit__ when a Python signal handler raises between __enter__ returning and the body starting. This leaks whatever resource __enter__ ac...

X Description: Bug report A with X: statement can return without calling X.__exit__ when a Python signal handler raises between __enter__ returning and the body starting. This leaks whatever resource __enter__ ac...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`with lock`: can skip `__exit__` when a signal handler raises right after `__enter__`","articleBody":"# Bug report\n\nA `with X:` statement can return without calling `X.__exit__` when a Python signal handler raises between `__enter__` returning and the body starting.\n\nThis leaks whatever resource `__enter__` acquired (e.g. a `threading.Lock` stays locked forever).\n\n### Cause\n\nIn 3.14, the following changed how `with X:` compiles:\n\n* GH-120507\n\nBefore 3.14:\n\n```\nLOAD X\nBEFORE_WITH        # single op: calls enter, sets up exit on stack\nPOP_TOP            # \u003c- exception table starts here\n... body ...\n```\n\n`BEFORE_WITH` has no periodic/eval-breaker check, so there was no window for a signal handler to fire between `__enter__` returning and the with-statement's exception handler being established.\n\nFrom 3.14 onward:\n```\nLOAD X\nCOPY 1\nLOAD_SPECIAL exit\nSWAP 2 / SWAP 3\nLOAD_SPECIAL enter\nCALL 0             # ends with _CHECK_PERIODIC_AT_END\nPOP_TOP            # \u003c- exception table starts here\n... body ...\n```\n\n`CALL` includes _CHECK_PERIODIC_AT_END, which runs the Python signal handler. If that handler raises, `JUMP_TO_LABEL(error)` fires with `frame->instr_ptr` still pointing at `CALL` (set before the micro-ops run).\n\n### Reproducer (via Claude Code)\n\n\u003cdetails>\n\u003csummary>test_with_signal_leak.py\u003c/summary>\n\n```python\nimport ctypes, ctypes.util, signal, sys, threading\n\n# signal.pthread_kill / os.kill both call PyErr_CheckSignals after the syscall,\n# which perturbs timing enough to suppress the race. Use libc.pthread_kill\n# directly.\n_pthread_kill = ctypes.CDLL(ctypes.util.find_library(\"c\")).pthread_kill\n_pthread_kill.argtypes = [ctypes.c_ulong, ctypes.c_int]\n_pthread_kill.restype = ctypes.c_int\n\n_MAIN_TID = threading.get_ident()\n\ndef _handler(signum, frame):\n    raise RuntimeError(\"signal\")\n\ndef _send():\n    _pthread_kill(_MAIN_TID, signal.SIGUSR1)\n\ndef run_trial(lock, iterations=200):\n    t = threading.Thread(target=_send)\n    t.start()\n    try:\n        for _ in range(iterations):\n            with lock:\n                pass\n    except BaseException:\n        pass\n    try:\n        t.join()\n    except BaseException:\n        pass\n    if lock.locked():\n        lock.release()\n        return True\n    return False\n\nsignal.signal(signal.SIGUSR1, _handler)\nlock = threading.Lock()\nleaked = 0\nfor _ in range(2000):\n    try:\n        if run_trial(lock):\n            leaked += 1\n    except BaseException:\n        if lock.locked():\n            lock.release()\nprint(f\"leaked={leaked}/2000\")\n```\n\u003c/details>\n\ncc @markshannon \n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149019\n* gh-150911\n* gh-154830\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2026-04-22T14:33:01Z","url":"https://github.com/python/cpython/issues/148874"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:8e786f2d-01bb-9502-5117-ad514aafc406
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD380:65478:3AF924C:4E2FE8F:6B021B79
html-safe-nonce120f8f7da807176a5463a00b9138cebe78bd987a4fc26a5ccd37c34baf4016ba
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzgwOjY1NDc4OjNBRjkyNEM6NEUyRkU4Rjo2QjAyMUI3OSIsInZpc2l0b3JfaWQiOiI4NjgwMzQ4NTM0NDU4OTQ4NDczIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacbb3e63425c48ddff95a6ea14741b9cba400ae406647167bfc935938585b00418
hovercard-subject-tagissue:4309733651
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/148874/issue_layout
twitter:imagehttps://opengraph.githubassets.com/aa8021a50f7cbe27be5994d9685d7a4f5a82f63808b5189b0a1a628a24fdc757/python/cpython/issues/148874
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/aa8021a50f7cbe27be5994d9685d7a4f5a82f63808b5189b0a1a628a24fdc757/python/cpython/issues/148874
og:image:altBug report A with X: statement can return without calling X.__exit__ when a Python signal handler raises between __enter__ returning and the body starting. This leaks whatever resource __enter__ ac...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamecolesbury
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
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
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/148874#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148874
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148874
Sign uphttps://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/148874
Reloadhttps://github.com/python/cpython/issues/148874
Reloadhttps://github.com/python/cpython/issues/148874
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148874
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148874
with lock: can skip __exit__ when a signal handler raises right after __enter__https://github.com/python/cpython/issues/148874#top
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Apr 22, 2026https://github.com/python/cpython/issues/148874#issue-4309733651
Lower BEFORE_WITH and BEFORE_ASYNC_WITH to attribute lookups and calls. #120507https://github.com/python/cpython/issues/120507
@markshannonhttps://github.com/markshannon
gh-148874: add flag to CALL to skip periodic check in with #149019https://github.com/python/cpython/pull/149019
GH-148874: Make sure that mngr.__exit__() is always called in a with statement #150911https://github.com/python/cpython/pull/150911
[3.15] Backport GH-148874: Make sure that mngr.__exit__() is always called in a with statement (GH-150911) #154830https://github.com/python/cpython/pull/154830
colesburyhttps://github.com/colesbury
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
on Apr 22, 2026https://github.com/python/cpython/issues/148874#event-24759533277
https://github.com/markshannon
markshannonhttps://github.com/markshannon
on Apr 23, 2026https://github.com/python/cpython/issues/148874#issuecomment-4305439996
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Apr 23, 2026https://github.com/python/cpython/issues/148874#issuecomment-4305966562
https://github.com/markshannon
markshannonhttps://github.com/markshannon
on Apr 23, 2026https://github.com/python/cpython/issues/148874#issuecomment-4306386135
NekoAsakurahttps://github.com/NekoAsakura
on Apr 26, 2026https://github.com/python/cpython/issues/148874#event-24883167059
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/6a07b77346169bd881b6067f01bd1d5a87073789
pythongh-148874https://github.com/python/cpython/issues/148874
6a07b77https://github.com/NekoAsakura/cpython/commit/6a07b77346169bd881b6067f01bd1d5a87073789
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/0237f8efacb9f9ae32cd62d07a5bd6fad24284f3
pythongh-148874https://github.com/python/cpython/issues/148874
0237f8ehttps://github.com/NekoAsakura/cpython/commit/0237f8efacb9f9ae32cd62d07a5bd6fad24284f3
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/7262c62ed5c1dede8ff8ee852091323756ec5755
pythongh-148874https://github.com/python/cpython/issues/148874
7262c62https://github.com/NekoAsakura/cpython/commit/7262c62ed5c1dede8ff8ee852091323756ec5755
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/8fc75f04e30e86f4c6d426c8488164493e0db533
pythongh-148874https://github.com/python/cpython/issues/148874
8fc75f0https://github.com/NekoAsakura/cpython/commit/8fc75f04e30e86f4c6d426c8488164493e0db533
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/e8e3d32ff93f78344aa0aee7aec9475412d6d752
pythongh-148874https://github.com/python/cpython/issues/148874
e8e3d32https://github.com/NekoAsakura/cpython/commit/e8e3d32ff93f78344aa0aee7aec9475412d6d752
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/55f1981a32c4386a3f4ed760393944e42f069044
pythongh-148874https://github.com/python/cpython/issues/148874
55f1981https://github.com/NekoAsakura/cpython/commit/55f1981a32c4386a3f4ed760393944e42f069044
NekoAsakurahttps://github.com/NekoAsakura
on May 28, 2026https://github.com/python/cpython/issues/148874#event-26074990071
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/236d271cda85701fc71c89bce4c884a59f6243fa
pythongh-148874https://github.com/python/cpython/issues/148874
236d271https://github.com/NekoAsakura/cpython/commit/236d271cda85701fc71c89bce4c884a59f6243fa
Merge branch 'main' into pythongh-148874/with-signal-exit-skiphttps://github.com/NekoAsakura/cpython/commit/62bd5bb987e7e7db3446c55502948eac2229323f
pythongh-148874https://github.com/python/cpython/issues/148874
62bd5bbhttps://github.com/NekoAsakura/cpython/commit/62bd5bb987e7e7db3446c55502948eac2229323f
markshannonhttps://github.com/markshannon
on Jul 1, 2026https://github.com/python/cpython/issues/148874#event-27433356498
GH-148874: Make sure that mngr.__exit__() is always called in a with …https://github.com/python/cpython/commit/c6982439ee0dd11f8014f78205132df0a8a85027
GH-148874https://github.com/python/cpython/issues/148874
c698243https://github.com/python/cpython/commit/c6982439ee0dd11f8014f78205132df0a8a85027
markshannonhttps://github.com/markshannon
on Aug 3, 2026https://github.com/python/cpython/issues/148874#event-28896930014
[3.15] Backport GH-148874: Make sure that mngr.__exit__() is always c…https://github.com/python/cpython/commit/e972f76ac1e1ff9d36dc466424f5922f4a5b301e
GH-148874https://github.com/python/cpython/issues/148874
e972f76https://github.com/python/cpython/commit/e972f76ac1e1ff9d36dc466424f5922f4a5b301e
pranavrthhttps://github.com/pranavrth
on Oct 29, 2026https://github.com/python/cpython/issues/148874#event-34568629498
Python release: wheel verification fixes (CPython 3.14 Ctrl+C, macOS …https://github.com/confluentinc/kafka-clients/commit/390233eb57844203d450f30f16dd0d63afe4d353
390233ehttps://github.com/confluentinc/kafka-clients/commit/390233eb57844203d450f30f16dd0d63afe4d353
crusaderkyhttps://github.com/crusaderky
on Nov 13, 2026https://github.com/python/cpython/issues/148874#event-36048346123
Run test_interrupt in a subprocesshttps://github.com/crusaderky/dask/commit/b304c430e623c29fafb300940820c8353d696e15
b304c43https://github.com/crusaderky/dask/commit/b304c430e623c29fafb300940820c8353d696e15
https://github.com/gamarino
gamarinohttps://github.com/gamarino
on Nov 14, 2026https://github.com/python/cpython/issues/148874#issuecomment-6869079908
GH-150911https://github.com/python/cpython/pull/150911
https://github.com/sergey-miryanov
sergey-miryanovhttps://github.com/sergey-miryanov
on Nov 14, 2026https://github.com/python/cpython/issues/148874#issuecomment-6872255896
@hugovkhttps://github.com/hugovk
https://github.com/hugovk
hugovkhttps://github.com/hugovk
on Nov 15, 2026https://github.com/python/cpython/issues/148874#issuecomment-6896273526
#150911https://github.com/python/cpython/pull/150911
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/148874
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/148874
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%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.