Skip to content

gh-128384: Use a context variable for warnings.catch_warnings #130010

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

Merged
merged 47 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ba99f2e
Make _contextvars a builtin module.
nascheme Dec 28, 2024
6d00c2a
Add 'context' parameter to Thread.
nascheme Dec 21, 2024
a868fe9
Tweak blurb markup.
nascheme Feb 7, 2025
16fa2c3
Doc markup fix.
nascheme Feb 7, 2025
f0ccc8d
Use contextvar for catch_warnings().
nascheme Dec 27, 2024
bcadd20
Add blurb.
nascheme Feb 11, 2025
0cfe578
Add "_warnings_context" as identifier.
nascheme Feb 11, 2025
928c2df
Fix test_support for context var filters.
nascheme Feb 11, 2025
65751d9
Regenerate 'configure' script.
nascheme Feb 11, 2025
09e72b8
Rename flag to `thread_inherit_context`.
nascheme Feb 11, 2025
75f1b38
Merge branch 'main' into thread_inherit_context
nascheme Feb 11, 2025
872920d
Regenerate 'configure' script.
nascheme Feb 11, 2025
5b6b59e
Merge branch 'thread_inherit_context' into gh-128384-warnings-contextvar
nascheme Feb 11, 2025
9e955dc
Use separate flag `thread_safe_warnings`.
nascheme Feb 12, 2025
3a56c64
Add doc for ``thread_safe_warnings`` flag.
nascheme Feb 12, 2025
daa3d52
Create _py_warnings.py for Python implementation.
nascheme Feb 18, 2025
83419e4
Add _warnings_context to _warnings module.
nascheme Feb 18, 2025
15443e8
Remove '_warnings_context' global string.
nascheme Feb 18, 2025
983e7ee
Rename flag to 'context_aware_warnings'.
nascheme Feb 18, 2025
e5e660c
Use catch_warnings() for module under test.
nascheme Feb 18, 2025
e054a57
Don't pass 'module' to catch_warnings().
nascheme Feb 18, 2025
2466cec
Correct error in warnings module docs.
nascheme Feb 19, 2025
165a573
Use PyObject_GetAttr.
nascheme Feb 19, 2025
af8728d
Typo fix for docstring.
nascheme Feb 19, 2025
1c02b7e
Avoid DECREF calls on None.
nascheme Feb 19, 2025
dba89e0
Add warnings.py module (missed in previous commit).
nascheme Feb 19, 2025
2c48fc5
Add comment about why 'context' is passed in test.
nascheme Feb 19, 2025
53eb72d
Revise "decimal' docs, adding note about flag.
nascheme Feb 21, 2025
9cb6f73
Merge branch 'thread_inherit_context' into gh-128384-warnings-contextvar
nascheme Feb 21, 2025
f79daaa
Fix race in filter_search().
nascheme Feb 25, 2025
5ca1d39
Add note to free-threading howto.
nascheme Mar 11, 2025
9420a9d
Merge branch 'main' into gh-128384-warnings-contextvar
nascheme Mar 11, 2025
bad0fdb
Doc fixes for missing refs.
nascheme Mar 12, 2025
bb35c2e
Add "_py_warnings" to stdlib_module_names.h.
nascheme Mar 12, 2025
adf32cb
Improve error text in Python/_warnings.c
nascheme Mar 17, 2025
47d0b6f
Avoid unused-variable warning in _warnings.c.
nascheme Mar 17, 2025
b880dd1
Minor code improvement to _warnings.c.
nascheme Mar 17, 2025
5a1115b
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 17, 2025
ae701e3
Merge branch 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 18, 2025
153c6b1
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 26, 2025
9220223
Add extra unit tests.
nascheme Mar 27, 2025
c2c90cb
Use asyncio events to get deterministic execution.
nascheme Mar 27, 2025
c1def22
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Apr 9, 2025
4e461d9
Add unit test for asyncio tasks.
nascheme Apr 9, 2025
543927b
Update Doc/howto/free-threading-python.rst
nascheme Apr 9, 2025
4f11910
Use asyncio.gather() rather than create_task().
nascheme Apr 9, 2025
42d157b
Call resetwarnings() in a few places.
nascheme Apr 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Doc/howto/free-threading-python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,33 @@ to re-enable it in a thread-safe way in the 3.14 release. This overhead is
expected to be reduced in upcoming Python release. We are aiming for an
overhead of 10% or less on the pyperformance suite compared to the default
GIL-enabled build.


Behavioral changes
==================

This section describes CPython behavioural changes with the free-threaded
build.


Context variables
-----------------

In the free-threaded build, the flag :data:`~sys.flags.thread_inherit_context`
is set to true by default which causes threads created with
:class:`threading.Thread` to start with a copy of the
:class:`~contextvars.Context()` of the caller of
:meth:`~threading.Thread.start`. In the default GIL-enabled build, the flag
defaults to false so threads start with an
empty :class:`~contextvars.Context()`.


Warning filters
---------------

In the free-threaded build, the flag :data:`~sys.flags.context_aware_warnings`
is set to true by default. In the default GIL-enabled build, the flag defaults
to false. If the flag is true then the :class:`warnings.catch_warnings`
context manager uses a context variable for warning filters. If the flag is
false then :class:`~warnings.catch_warnings` modifies the global filters list,
which is not thread-safe. See the :mod:`warnings` module for more details.
21 changes: 14 additions & 7 deletions Doc/library/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1884,13 +1884,20 @@ the current thread.

If :func:`setcontext` has not been called before :func:`getcontext`, then
:func:`getcontext` will automatically create a new context for use in the
current thread.

The new context is copied from a prototype context called *DefaultContext*. To
control the defaults so that each thread will use the same values throughout the
application, directly modify the *DefaultContext* object. This should be done
*before* any threads are started so that there won't be a race condition between
threads calling :func:`getcontext`. For example::
current thread. New context objects have default values set from the
:data:`decimal.DefaultContext` object.

The :data:`sys.flags.thread_inherit_context` flag affects the context for
new threads. If the flag is false, new threads will start with an empty
context. In this case, :func:`getcontext` will create a new context object
when called and use the default values from *DefaultContext*. If the flag
is true, new threads will start with a copy of context from the caller of
:meth:`threading.Thread.start`.

To control the defaults so that each thread will use the same values throughout
the application, directly modify the *DefaultContext* object. This should be
done *before* any threads are started so that there won't be a race condition
between threads calling :func:`getcontext`. For example::

# Set applicationwide defaults for all threads about to be launched
DefaultContext.prec = 12
Expand Down
24 changes: 23 additions & 1 deletion Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ always available. Unless explicitly noted otherwise, all variables are read-only
.. data:: flags

The :term:`named tuple` *flags* exposes the status of command line
flags. The attributes are read only.
flags. Flags should only be accessed only by name and not by index. The
attributes are read only.

.. list-table::

Expand Down Expand Up @@ -594,6 +595,18 @@ always available. Unless explicitly noted otherwise, all variables are read-only
* - .. attribute:: flags.warn_default_encoding
- :option:`-X warn_default_encoding <-X>`

* - .. attribute:: flags.gil
- :option:`-X gil <-X>` and :envvar:`PYTHON_GIL`

* - .. attribute:: flags.thread_inherit_context
- :option:`-X thread_inherit_context <-X>` and
:envvar:`PYTHON_THREAD_INHERIT_CONTEXT`

* - .. attribute:: flags.context_aware_warnings
- :option:`-X thread_inherit_context <-X>` and
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS`


.. versionchanged:: 3.2
Added ``quiet`` attribute for the new :option:`-q` flag.

Expand All @@ -620,6 +633,15 @@ always available. Unless explicitly noted otherwise, all variables are read-only
.. versionchanged:: 3.11
Added the ``int_max_str_digits`` attribute.

.. versionchanged:: 3.13
Added the ``gil`` attribute.

.. versionchanged:: 3.14
Added the ``thread_inherit_context`` attribute.

.. versionchanged:: 3.14
Added the ``context_aware_warnings`` attribute.


.. data:: float_info

Expand Down
15 changes: 14 additions & 1 deletion Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ since it is impossible to detect the termination of alien threads.


.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
daemon=None)
daemon=None, context=None)

This constructor should always be called with keyword arguments. Arguments
are:
Expand All @@ -359,6 +359,16 @@ since it is impossible to detect the termination of alien threads.
If ``None`` (the default), the daemonic property is inherited from the
current thread.

*context* is the :class:`~contextvars.Context` value to use when starting
the thread. The default value is ``None`` which indicates that the
:data:`sys.flags.thread_inherit_context` flag controls the behaviour. If
the flag is true, threads will start with a copy of the context of the
caller of :meth:`~Thread.start`. If false, they will start with an empty
context. To explicitly start with an empty context, pass a new instance of
:class:`~contextvars.Context()`. To explicitly start with a copy of the
current context, pass the value from :func:`~contextvars.copy_context`. The
flag defaults true on free-threaded builds and false otherwise.

If the subclass overrides the constructor, it must make sure to invoke the
base class constructor (``Thread.__init__()``) before doing anything else to
the thread.
Expand All @@ -369,6 +379,9 @@ since it is impossible to detect the termination of alien threads.
.. versionchanged:: 3.10
Use the *target* name if *name* argument is omitted.

.. versionchanged:: 3.14
Added the *context* parameter.

.. method:: start()

Start the thread's activity.
Expand Down
88 changes: 76 additions & 12 deletions Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,13 @@ the warning using the :class:`catch_warnings` context manager::
While within the context manager all warnings will simply be ignored. This
allows you to use known-deprecated code without having to see the warning while
not suppressing the warning for other code that might not be aware of its use
of deprecated code. Note: this can only be guaranteed in a single-threaded
application. If two or more threads use the :class:`catch_warnings` context
manager at the same time, the behavior is undefined.
of deprecated code.

.. note::

See :ref:`warning-concurrent-safe` for details on the
concurrency-safety of the :class:`catch_warnings` context manager when
used in programs using multiple threads or async functions.

.. _warning-testing:

Expand Down Expand Up @@ -364,10 +366,13 @@ the warning has been cleared.
Once the context manager exits, the warnings filter is restored to its state
when the context was entered. This prevents tests from changing the warnings
filter in unexpected ways between tests and leading to indeterminate test
results. The :func:`showwarning` function in the module is also restored to
its original value. Note: this can only be guaranteed in a single-threaded
application. If two or more threads use the :class:`catch_warnings` context
manager at the same time, the behavior is undefined.
results.

.. note::

See :ref:`warning-concurrent-safe` for details on the
concurrency-safety of the :class:`catch_warnings` context manager when
used in programs using multiple threads or async functions.

When testing multiple operations that raise the same kind of warning, it
is important to test them in a manner that confirms each operation is raising
Expand Down Expand Up @@ -615,12 +620,71 @@ Available Context Managers

.. note::

The :class:`catch_warnings` manager works by replacing and
then later restoring the module's
:func:`showwarning` function and internal list of filter
specifications. This means the context manager is modifying
global state and therefore is not thread-safe.
See :ref:`warning-concurrent-safe` for details on the
concurrency-safety of the :class:`catch_warnings` context manager when
used in programs using multiple threads or async functions.


.. versionchanged:: 3.11

Added the *action*, *category*, *lineno*, and *append* parameters.


.. _warning-concurrent-safe:

Concurrent safety of Context Managers
-------------------------------------

The behavior of :class:`catch_warnings` context manager depends on the
:data:`sys.flags.context_aware_warnings` flag. If the flag is true, the
context manager behaves in a concurrent-safe fashion and otherwise not.
Concurrent-safe means that it is both thread-safe and safe to use within
:ref:`asyncio coroutines <coroutine>` and tasks. Being thread-safe means
that behavior is predictable in a multi-threaded program. The flag defaults
to true for free-threaded builds and false otherwise.

If the :data:`~sys.flags.context_aware_warnings` flag is false, then
:class:`catch_warnings` will modify the global attributes of the
:mod:`warnings` module. This is not safe if used within a concurrent program
(using multiple threads or using asyncio coroutines). For example, if two
or more threads use the :class:`catch_warnings` class at the same time, the
behavior is undefined.

If the flag is true, :class:`catch_warnings` will not modify global
attributes and will instead use a :class:`~contextvars.ContextVar` to
store the newly established warning filtering state. A context variable
provides thread-local storage and it makes the use of :class:`catch_warnings`
thread-safe.

The *record* parameter of the context handler also behaves differently
depending on the value of the flag. When *record* is true and the flag is
false, the context manager works by replacing and then later restoring the
module's :func:`showwarning` function. That is not concurrent-safe.

When *record* is true and the flag is true, the :func:`showwarning` function
is not replaced. Instead, the recording status is indicated by an internal
property in the context variable. In this case, the :func:`showwarning`
function will not be restored when exiting the context handler.

The :data:`~sys.flags.context_aware_warnings` flag can be set the :option:`-X
context_aware_warnings<-X>` command-line option or by the
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS` environment variable.

.. note::

It is likely that most programs that desire thread-safe
behaviour of the warnings module will also want to set the
:data:`~sys.flags.thread_inherit_context` flag to true. That flag
causes threads created by :class:`threading.Thread` to start
with a copy of the context variables from the thread starting
it. When true, the context established by :class:`catch_warnings`
in one thread will also apply to new threads started by it. If false,
new threads will start with an empty warnings context variable,
meaning that any filtering that was established by a
:class:`catch_warnings` context manager will no longer be active.

.. versionchanged:: 3.14

Added the :data:`sys.flags.context_aware_warnings` flag and the use of a
context variable for :class:`catch_warnings` if the flag is true. Previous
versions of Python acted as if the flag was always set to false.
37 changes: 37 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,23 @@ Miscellaneous options

.. versionadded:: 3.13

* :samp:`-X thread_inherit_context={0,1}` causes :class:`~threading.Thread`
to, by default, use a copy of context of of the caller of
``Thread.start()`` when starting. Otherwise, threads will start
with an empty context. If unset, the value of this option defaults
to ``1`` on free-threaded builds and to ``0`` otherwise. See also
:envvar:`PYTHON_THREAD_INHERIT_CONTEXT`.

.. versionadded:: 3.14

* :samp:`-X context_aware_warnings={0,1}` causes the
:class:`warnings.catch_warnings` context manager to use a
:class:`~contextvars.ContextVar` to store warnings filter state. If
unset, the value of this option defaults to ``1`` on free-threaded builds
and to ``0`` otherwise. See also :envvar:`PYTHON_CONTEXT_AWARE_WARNINGS`.

.. versionadded:: 3.14

It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.

Expand Down Expand Up @@ -1241,6 +1258,26 @@ conflict.

.. versionadded:: 3.13

.. envvar:: PYTHON_THREAD_INHERIT_CONTEXT

If this variable is set to ``1`` then :class:`~threading.Thread` will,
by default, use a copy of context of of the caller of ``Thread.start()``
when starting. Otherwise, new threads will start with an empty context.
If unset, this variable defaults to ``1`` on free-threaded builds and to
``0`` otherwise. See also :option:`-X thread_inherit_context<-X>`.

.. versionadded:: 3.14

.. envvar:: PYTHON_CONTEXT_AWARE_WARNINGS

If set to ``1`` then the :class:`warnings.catch_warnings` context
manager will use a :class:`~contextvars.ContextVar` to store warnings
filter state. If unset, this variable defaults to ``1`` on
free-threaded builds and to ``0`` otherwise. See :option:`-X
context_aware_warnings<-X>`.

.. versionadded:: 3.14

Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ typedef struct PyConfig {
int use_frozen_modules;
int safe_path;
int int_max_str_digits;
int thread_inherit_context;
int context_aware_warnings;
#ifdef __APPLE__
int use_system_logger;
#endif
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(_feature_version)
STRUCT_FOR_ID(_field_types)
STRUCT_FOR_ID(_fields_)
STRUCT_FOR_ID(_filters)
STRUCT_FOR_ID(_finalizing)
STRUCT_FOR_ID(_find_and_load)
STRUCT_FOR_ID(_fix_up_module)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ struct _warnings_runtime_state {
PyObject *default_action; /* String */
_PyRecursiveMutex lock;
long filters_version;
PyObject *context;
};

struct _Py_mem_interp_free_queue {
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading