Title: warnings.catch_warnings is not thread safe or async safe · Issue #128384 · python/cpython · GitHub
Open Graph Title: warnings.catch_warnings is not thread safe or async safe · Issue #128384 · python/cpython
X Title: warnings.catch_warnings is not thread safe or async safe · Issue #128384 · python/cpython
Description: Bug report Bug description: This is an old issue but it has become more urgent to fix given free-threaded Python. A simple example to show how the current context manager can fail: from concurrent.futures import ThreadPoolExecutor import...
Open Graph Description: Bug report Bug description: This is an old issue but it has become more urgent to fix given free-threaded Python. A simple example to show how the current context manager can fail: from concurrent....
X Description: Bug report Bug description: This is an old issue but it has become more urgent to fix given free-threaded Python. A simple example to show how the current context manager can fail: from concurrent....
Opengraph URL: https://github.com/python/cpython/issues/128384
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"warnings.catch_warnings is not thread safe or async safe","articleBody":"# Bug report\n\n### Bug description:\n\nThis is an old issue but it has become more urgent to fix given free-threaded Python. A simple example to show how the current context manager can fail:\n```\nfrom concurrent.futures import ThreadPoolExecutor\n\nimport warnings\n\ndef test_warning():\n with warnings.catch_warnings():\n warnings.simplefilter(\"ignore\")\n warnings.warn(\"my warning\", UserWarning)\n\ntpe = ThreadPoolExecutor(max_workers=10)\n\nthreads = [tpe.submit(test_warning) for _ in range(100)]\n[t.result() for t in threads]\n```\n\nSince `test_warning()` is running in separate threads, there is no consistent ordering of which context manager exits first. That means the filters value that gets restored is also not consistent (a race between the threads about which version of the filters value they will restore).\n\nThe context manager is also not friendly to async code. These issues have been reported before:\n- gh-91505\n- gh-81785\n- gh-50896\n\nThe fairly obvious fix to this issue is to use `contextvars`, similar to how the `decimal` module uses a context. This is both thread safe and friendly to async code.\n\nThe `warnings` module itself has some \"shallow\" thread safety issues but these are relatively easy to fix and no API changes are needed.\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-128300\n* gh-128386\n* gh-128463\n* gh-130010\n* gh-132601\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/nascheme","@type":"Person","name":"nascheme"},"datePublished":"2024-12-31T22:53:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/128384/cpython/issues/128384"}
| 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:289abed4-7d50-8651-92b8-efc39f89518c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A526:29B748:4635F4:618638:6A537A45 |
| html-safe-nonce | 8e806cba16ed4ab4bbbcc14c62a72e5f9242f6530bcf034c2fbd3e2d3c57ba44 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTI2OjI5Qjc0ODo0NjM1RjQ6NjE4NjM4OjZBNTM3QTQ1IiwidmlzaXRvcl9pZCI6IjI4NzUwMzc0MTczNzA1MTYwMzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 323ee12896cdd682d1d54ebb80b583bb16a504253cbfe2340fef0bbda2b8bada |
| hovercard-subject-tag | issue:2764713401 |
| 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/128384/issue_layout |
| twitter:image | https://opengraph.githubassets.com/82cfcd0a65b9fa7822d928bf4183ceedd4f3d47b1e2c2b0f94fdad69d9c0892e/python/cpython/issues/128384 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/82cfcd0a65b9fa7822d928bf4183ceedd4f3d47b1e2c2b0f94fdad69d9c0892e/python/cpython/issues/128384 |
| og:image:alt | Bug report Bug description: This is an old issue but it has become more urgent to fix given free-threaded Python. A simple example to show how the current context manager can fail: from concurrent.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nascheme |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width