Commit Graph

223 Commits

Author SHA1 Message Date
Serhiy Storchaka bac3fcba5b
gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)
Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
2025-05-28 20:11:09 +03:00
Jiucheng(Oliver) 9a2346df86
gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) 2025-05-23 15:22:14 -04:00
Duprat fade146cfb
gh-134322: Fix `repr(threading.RLock)` (#134389)
Fix the `__repr__` value of `threading.RLock` from `_thread` module, when just created.
2025-05-22 16:46:57 +00:00
Duprat 3effede97c
gh-134323: Fix the new `threading.RLock.locked` method (#134368)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-05-22 13:48:24 +00:00
Bénédikt Tran d6dc33ed80
gh-134087: enforce signature of `threading.RLock` (#134178)
- Reject positional and keyword arguments in `_thread.RLock.__new__`.
- Convert `_thread.lock.__new__` to AC.
2025-05-19 11:26:14 +02:00
Petr Viktorin 4ebbfcf30e
gh-87135: Raise PythonFinalizationError when joining a blocked daemon thread (gh-130402)
If `Py_IsFinalizing()` is true, non-daemon threads (other than the current one)
are done, and daemon threads are prevented from running, so they
cannot finalize themselves and become done. Joining them (without timeout)
would block forever.

Raise PythonFinalizationError instead of hanging.

Raise even when a timeout is given, for consistency with trying to join your own thread.

See gh-123940 for a use case: calling `join()` from `__del__`. This is
ill-advised, but an exception should at least make it easier to diagnose.
2025-04-28 15:48:48 +02:00
Serhiy Storchaka f5f1ac84b3
gh-112068: C API: Add support of nullable arguments in PyArg_Parse (GH-121303) 2025-04-08 22:08:00 +03:00
sobolevn f7305a06c7
gh-115942: Add `locked` to several multiprocessing locks (#115944)
Co-authored-by: mpage <mpage@cs.stanford.edu>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2025-04-08 11:14:12 +03:00
Victor Stinner 78f1bac7f2
gh-111178: Fix function signature for test_threading (#131663) 2025-03-24 13:56:45 +00:00
Victor Stinner 1a082085ae
gh-131238: Remove pycore_object_deferred.h from pycore_object.h (#131549)
Remove also pycore_function.h from pycore_typeobject.h.
2025-03-21 16:44:10 +00:00
Xavier G. b70d45ab22
gh-131268: Implement thread names on OpenBSD (#131528) 2025-03-21 11:12:35 +01:00
Mark Shannon a45f25361d
GH-131238: More refactoring of core header files (GH-131351)
Adds new pycore_stats.h header file to help break dependencies involving the pycore_code.h header.
2025-03-17 14:41:05 +00:00
Victor Stinner a5776639c8
gh-111178: Fix function signatures to fix undefined behavior (#131191) 2025-03-14 09:52:15 +00:00
Victor Stinner 9a63138e09
gh-111178: Fix function signatures in misc files (#131180) 2025-03-13 16:55:08 +01:00
Sam Gross 052cb717f5
gh-124878: Fix race conditions during interpreter finalization (#130649)
The PyThreadState field gains a reference count field to avoid
issues with PyThreadState being a dangling pointer to freed memory.
The refcount starts with a value of two: one reference is owned by the
interpreter's linked list of thread states and one reference is owned by
the OS thread. The reference count is decremented when the thread state
is removed from the interpreter's linked list and before the OS thread
calls `PyThread_hang_thread()`. The thread that decrements it to zero
frees the `PyThreadState` memory.

The `holds_gil` field is moved out of the `_status` bit field, to avoid
a data race where on thread calls `PyThreadState_Clear()`, modifying the
`_status` bit field while the OS thread reads `holds_gil` when
attempting to acquire the GIL.

The `PyThreadState.state` field now has `_Py_THREAD_SHUTTING_DOWN` as a
possible value. This corresponds to the `_PyThreadState_MustExit()`
check. This avoids race conditions in the free threading build when
checking `_PyThreadState_MustExit()`.
2025-03-06 10:38:34 -05:00
Serhiy Storchaka 0ef4ffeefd
gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
2025-02-25 23:04:27 +02:00
Bénédikt Tran f8eefc2f35
gh-111178: fix UBSan failures in `Modules/_threadmodule.c` (GH-129794)
Fix UBSan failures for `PyThreadHandleObject`, `lockobject`, `rlockobject`, `localdummyobject`, `localobject`

Add safe casts

Clean up module functions

Use semantically correct parameter names
2025-02-24 13:42:39 +01:00
Victor Stinner 79f85a0bc1
gh-129354: Use PyErr_FormatUnraisable() function (#129518)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-31 15:06:14 +01:00
Victor Stinner 81159fce36
gh-59705: Make PYTHREAD_NAME_MAXLEN macro private (#128945)
Rename PYTHREAD_NAME_MAXLEN to _PYTHREAD_NAME_MAXLEN.
2025-01-18 15:10:29 +00:00
Victor Stinner d7f703d54d
gh-59705: Implement _thread.set_name() on Windows (#128675)
Implement set_name() with SetThreadDescription() and _get_name() with
GetThreadDescription(). If SetThreadDescription() or
GetThreadDescription() is not available in kernelbase.dll, delete the
method when the _thread module is imported.

Truncate the thread name to 32766 characters.

Co-authored-by: Eryk Sun <eryksun@gmail.com>
2025-01-17 14:55:43 +01:00
Sam Gross c1417487e9
gh-128691: Use deferred reference counting on `_thread._local` (#128693)
This change, along with the LOAD_ATTR specializations, makes the
"thread_local_read" micro benchmark in Tools/ftscalingbench/ftscalingbench.py
scale well to multiple threads.
2025-01-10 00:59:10 +00:00
Furkan Onder 492b224b99
gh-128279: Enhance the NetBSD compatibility for thread naming (#128280)
Enhance NetBSD compatibility for thread naming in _threadmodule.c.
2024-12-28 19:49:45 +01:00
Victor Stinner c91ccbe4ac
gh-59705: Set OS thread name when Thread.name is changed (#127702)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2024-12-10 17:33:11 +01:00
Victor Stinner 67b18a18b6
gh-59705: Add _thread.set_name() function (#127338)
On Linux, threading.Thread now sets the thread name to the operating
system.

* configure now checks if pthread_getname_np()
  and pthread_setname_np() functions are available.
* Add PYTHREAD_NAME_MAXLEN macro.
* Add _thread._NAME_MAXLEN constant for test_threading.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-12-06 16:27:12 +00:00
Victor Stinner 20657fbdb1
gh-127190: Fix local_setattro() error handling (#127366)
Don't make the assumption that the 'name' argument is a string. Use
repr() to format the 'name' argument instead.
2024-11-28 17:35:48 +01:00
Radislav Chugunov ca3ea9ad05
gh-109746: Make _thread.start_new_thread delete state of new thread on its startup failure (GH-109761)
If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-11-22 21:20:07 +02:00
Kumar Aditya 67f6e08147
gh-125139: use `_PyRecursiveMutex` in `_thread.RLock` (#125144) 2024-10-14 14:06:31 +05:30
Kumar Aditya fca552993d
gh-117721: use PyMutex in `_thread.lock` (#125110) 2024-10-08 20:17:32 +05:30
Victor Stinner ddccd546a0
gh-111178: Fix function signatures in _threadmodule.c (#124964) 2024-10-04 12:50:01 +02:00
algonell 9017b95ff2
Fix typos (#123775) 2024-09-09 14:58:26 +02:00
mpage e059aa6b01
gh-120973: Fix thread-safety issues with `threading.local` (#121655)
This is a small refactoring to the current design that allows us to
avoid manually iterating over threads.

This should also fix gh-118490.
2024-07-19 13:22:02 -04:00
Brett Simmers c2627d6eea
gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Sam Gross b2c3b70c71
gh-118332: Fix deadlock involving stop the world (#118412)
Avoid detaching thread state when stopping the world. When re-attaching
the thread state, the thread would attempt to resume the top-most
critical section, which might now be held by a thread paused for our
stop-the-world request.
2024-04-30 15:01:28 -04:00
Serhiy Storchaka ffbd97428d
gh-117764: Add signatures and improve docstrings in the _thread module (GH-117772) 2024-04-12 09:49:59 +00:00
Sam Gross 60e105c1c1
gh-113964: Don't prevent new threads until all non-daemon threads exit (#116677)
Starting in Python 3.12, we prevented calling fork() and starting new threads
during interpreter finalization (shutdown). This has led to a number of
regressions and flaky tests. We should not prevent starting new threads
(or `fork()`) until all non-daemon threads exit and finalization starts in
earnest.

This changes the checks to use `_PyInterpreterState_GetFinalizing(interp)`,
which is set immediately before terminating non-daemon threads.
2024-03-19 14:40:20 -04:00
mpage b3f0c1591a
gh-116915: Make `_thread._ThreadHandle` support GC (#116934)
Even though it has no internal references to Python objects it still
has a reference to its type by virtue of being a heap type. We need
to provide a traverse function that visits the type, but we do not
need to provide a clear function.
2024-03-18 09:40:16 +01:00
mpage 33da0e844c
gh-114271: Fix race in `Thread.join()` (#114839)
There is a race between when `Thread._tstate_lock` is released[^1] in `Thread._wait_for_tstate_lock()`
and when `Thread._stop()` asserts[^2] that it is unlocked. Consider the following execution
involving threads A, B, and C:

1. A starts.
2. B joins A, blocking on its `_tstate_lock`.
3. C joins A, blocking on its `_tstate_lock`.
4. A finishes and releases its `_tstate_lock`.
5. B acquires A's `_tstate_lock` in `_wait_for_tstate_lock()`, releases it, but is swapped
   out before calling `_stop()`.
6. C is scheduled, acquires A's `_tstate_lock` in `_wait_for_tstate_lock()` but is swapped
   out before releasing it.
7. B is scheduled, calls `_stop()`, which asserts that A's `_tstate_lock` is not held.
   However, C holds it, so the assertion fails.

The race can be reproduced[^3] by inserting sleeps at the appropriate points in
the threading code. To do so, run the `repro_join_race.py` from the linked repo.

There are two main parts to this PR:

1. `_tstate_lock` is replaced with an event that is attached to `PyThreadState`.
   The event is set by the runtime prior to the thread being cleared (in the same
   place that `_tstate_lock` was released). `Thread.join()` blocks waiting for the
   event to be set.
2. `_PyInterpreterState_WaitForThreads()` provides the ability to wait for all
   non-daemon threads to exit. To do so, an `is_daemon` predicate was added to
   `PyThreadState`. This field is set each time a thread is created. `threading._shutdown()`
   now calls into `_PyInterpreterState_WaitForThreads()` instead of waiting on
   `_tstate_lock`s.

[^1]: 441affc9e7/Lib/threading.py (L1201)
[^2]: 441affc9e7/Lib/threading.py (L1115)
[^3]: 8194653279

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Antoine Pitrou <antoine@python.org>
2024-03-16 13:56:30 +01:00
Serhiy Storchaka 72d3cc94cd
gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438) 2024-03-07 11:21:08 +02:00
mpage c62144a02c
gh-114271: Make `_thread.lock` thread-safe in free-threaded builds (#116433)
Previously, the `locked` field was set after releasing the lock. This reverses
the order so that the `locked` field is set while the lock is still held.

There is still one thread-safety issue where `locked` is checked prior to
releasing the lock, however, in practice that will only be an issue when
unlocking the lock is contended, which should be rare.
2024-03-06 15:46:36 -05:00
mpage 9e88173d36
gh-114271: Make `_thread.ThreadHandle` thread-safe in free-threaded builds (GH-115190)
Make `_thread.ThreadHandle` thread-safe in free-threaded builds

We protect the mutable state of `ThreadHandle` using a `_PyOnceFlag`.
Concurrent operations (i.e. `join` or `detach`) on `ThreadHandle` block
until it is their turn to execute or an earlier operation succeeds.
Once an operation has been applied successfully all future operations
complete immediately.

The `join()` method is now idempotent. It may be called multiple times
but the underlying OS thread will only be joined once. After `join()`
succeeds, any future calls to `join()` will succeed immediately.

The internal thread handle `detach()` method has been removed.
2024-03-01 13:43:12 -08:00
Victor Stinner 77430b6a32
gh-110850: Replace private _PyTime_MAX with public PyTime_MAX (#115751)
Remove references to the old names _PyTime_MIN
and _PyTime_MAX, now that PyTime_MIN and
PyTime_MAX are public.

Replace also _PyTime_MIN with PyTime_MIN.
2024-02-21 08:11:40 +00:00
Victor Stinner 145bc2d638
gh-110850: Use public PyTime functions (#115746)
Replace private _PyTime functions with public PyTime functions.

random_seed_time_pid() now reports errors to its caller.
2024-02-20 23:31:30 +00:00
Victor Stinner d207c7cd5a
gh-110850: Cleanup pycore_time.h includes (#115724)
<pycore_time.h> include is no longer needed to get the PyTime_t type
in internal header files. This type is now provided by <Python.h>
include. Add <pycore_time.h> includes to C files instead.
2024-02-20 16:50:43 +00:00
Victor Stinner 9af80ec83d
gh-110850: Replace _PyTime_t with PyTime_t (#115719)
Run command:

sed -i -e 's!\<_PyTime_t\>!PyTime_t!g' $(find -name "*.c" -o -name "*.h")
2024-02-20 15:02:27 +00:00
mpage f366e21504
gh-114271: Make `thread._rlock` thread-safe in free-threaded builds (#115102)
The ID of the owning thread (`rlock_owner`) may be accessed by
multiple threads without holding the underlying lock; relaxed
atomics are used in place of the previous loads/stores.

The number of times that the lock has been acquired (`rlock_count`)
is only ever accessed by the thread that holds the lock; we do not
need to use atomics to access it.
2024-02-16 13:29:25 -05:00
Victor Stinner 3e7b7df5cb
gh-114570: Add PythonFinalizationError exception (#115352)
Add PythonFinalizationError exception. This exception derived from
RuntimeError is raised when an operation is blocked during the Python
finalization.

The following functions now raise PythonFinalizationError, instead of
RuntimeError:

* _thread.start_new_thread()
* subprocess.Popen
* os.fork()
* os.fork1()
* os.forkpty()

Morever, _winapi.Overlapped finalizer now logs an unraisable
PythonFinalizationError, instead of an unraisable RuntimeError.
2024-02-14 23:35:06 +01:00
mpage de7d67b19b
gh-114271: Make `PyInterpreterState.threads.count` thread-safe in free-threaded builds (gh-115093)
Use atomics to mutate PyInterpreterState.threads.count.
2024-02-12 10:44:00 -07:00
Sam Gross b6228b521b
gh-115035: Mark ThreadHandles as non-joinable earlier after forking (#115042)
This marks dead ThreadHandles as non-joinable earlier in
`PyOS_AfterFork_Child()` before we execute any Python code. The handles
are stored in a global linked list in `_PyRuntimeState` because `fork()`
affects the entire process.
2024-02-06 14:45:04 -05:00
Nikita Sobolev d96358ff9d
gh-114315: Make `threading.Lock` a real class, not a factory function (#114479)
`threading.Lock` is now the underlying class and is constructable rather than the old
factory function. This allows for type annotations to refer to it which had no non-ugly
way to be expressed prior to this.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2024-01-25 19:46:32 +00:00
Nikita Sobolev d1b031cc58
gh-114414: Assert PyType_GetModuleByDef result in _threadmodule (#114415) 2024-01-22 10:19:25 +01:00