-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
warnings.catch_warnings is not thread safe or async safe #128384
Labels
stdlib
Python modules in the Lib dir
topic-asyncio
topic-free-threading
type-bug
An unexpected behavior, bug, or error
Comments
This was referenced Dec 31, 2024
nascheme
added a commit
that referenced
this issue
Jan 14, 2025
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Feb 12, 2025
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Feb 21, 2025
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Mar 11, 2025
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Mar 27, 2025
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Mar 27, 2025
nascheme
added a commit
to nascheme/cpython
that referenced
this issue
Mar 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
stdlib
Python modules in the Lib dir
topic-asyncio
topic-free-threading
type-bug
An unexpected behavior, bug, or error
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:
Since
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).The context manager is also not friendly to async code. These issues have been reported before:
The fairly obvious fix to this issue is to use
contextvars
, similar to how thedecimal
module uses a context. This is both thread safe and friendly to async code.The
warnings
module itself has some "shallow" thread safety issues but these are relatively easy to fix and no API changes are needed.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: