Title: PyErr_CheckSignals should be callable without holding the GIL · Issue #133465 · python/cpython · GitHub
Open Graph Title: PyErr_CheckSignals should be callable without holding the GIL · Issue #133465 · python/cpython
X Title: PyErr_CheckSignals should be callable without holding the GIL · Issue #133465 · python/cpython
Description: Feature or enhancement Proposal: Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently throughout each such operation, so that if...
Open Graph Description: Feature or enhancement Proposal: Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently th...
X Description: Feature or enhancement Proposal: Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently th...
Opengraph URL: https://github.com/python/cpython/issues/133465
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PyErr_CheckSignals should be callable without holding the GIL","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nCompiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call `PyErr_CheckSignals` frequently throughout each such operation, so that if the user interrupts the operation with control-C, it is cancelled promptly.\n\nIn the normal case where no signals are pending, `PyErr_CheckSignals` is cheap; however, callers must hold the GIL, and compiled-code modules that implement time-consuming operations are also supposed to release the GIL during each such operation. The overhead of reclaiming the GIL in order to call `PyErr_CheckSignals`,\nand then releasing it again, sufficiently often for reasonable user responsiveness, can be substantial.\n\nIf my understanding of the thread-state rules is correct, PyErr_CheckSignals only *needs* the GIL if it has work to do. *Checking* whether there is a pending signal, or a pending request to run the cycle collector, requires only a couple of atomic loads. Therefore, I propose that we should restructure `PyErr_CheckSignals` and its close relatives (`_PyErr_CheckSignals` and `_PyErr_CheckSignalsTstate`) so that all the “do we have anything to do” checks are done in a batch before anything that needs the GIL. If any of them are true, then the function should acquire the GIL itself, repeat the check (because another thread could have stolen the event while we were waiting for the GIL), and then actually do the work, thus enabling callers to *not* hold the GIL.\n\nI have already prepared and tested an implementation of this change and will submit a PR shortly.\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-133466\n* gh-135358\n* gh-140549\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/zackw","@type":"Person","name":"zackw"},"datePublished":"2025-05-05T19:11:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/133465/cpython/issues/133465"}
| 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:1a13f680-f2c0-7400-75af-42c56498c458 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCE8:206F88:3701F73:4BD21DD:6A54B510 |
| html-safe-nonce | a3fd3259a25909ed3b19c93e5ddbd27fc9633463a07ef140b81d7fe95f3dfdbb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0U4OjIwNkY4ODozNzAxRjczOjRCRDIxREQ6NkE1NEI1MTAiLCJ2aXNpdG9yX2lkIjoiNTI1MDIwMjE1MjIyNDQ3MDI4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e7ef3c1e4677b45e356e14a3ca8002cf903b9b1762bdcbacee2dd24f9a06b876 |
| hovercard-subject-tag | issue:3040568013 |
| 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/133465/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0f8b04993d75c59182bf915e73f24d166a70a1b0a788948691fe9e7eac762a54/python/cpython/issues/133465 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0f8b04993d75c59182bf915e73f24d166a70a1b0a788948691fe9e7eac762a54/python/cpython/issues/133465 |
| og:image:alt | Feature or enhancement Proposal: Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zackw |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 5d343e1152573ef294b28f54e4a14fb5fdb3a5a1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width