summaryrefslogtreecommitdiffstats
path: root/Modules/_threadmodule.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-87135: Raise PythonFinalizationError when joining a blocked daemon thread ↵Petr Viktorin2025-04-281-5/+15
| | | | | | | | | | | | | | | | (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.
* gh-112068: C API: Add support of nullable arguments in PyArg_Parse (GH-121303)Serhiy Storchaka2025-04-081-13/+5
|
* gh-115942: Add `locked` to several multiprocessing locks (#115944)sobolevn2025-04-081-0/+15
| | | | Co-authored-by: mpage <mpage@cs.stanford.edu> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
* gh-111178: Fix function signature for test_threading (#131663)Victor Stinner2025-03-241-2/+3
|
* gh-131238: Remove pycore_object_deferred.h from pycore_object.h (#131549)Victor Stinner2025-03-211-1/+2
| | | Remove also pycore_function.h from pycore_typeobject.h.
* gh-131268: Implement thread names on OpenBSD (#131528)Xavier G.2025-03-211-5/+22
|
* GH-131238: More refactoring of core header files (GH-131351)Mark Shannon2025-03-171-0/+1
| | | | Adds new pycore_stats.h header file to help break dependencies involving the pycore_code.h header.
* gh-111178: Fix function signatures to fix undefined behavior (#131191)Victor Stinner2025-03-141-3/+3
|
* gh-111178: Fix function signatures in misc files (#131180)Victor Stinner2025-03-131-2/+3
|
* gh-124878: Fix race conditions during interpreter finalization (#130649)Sam Gross2025-03-061-3/+0
| | | | | | | | | | | | | | | | | | | | 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()`.
* gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)Serhiy Storchaka2025-02-251-6/+6
| | | | | | | | 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().
* gh-111178: fix UBSan failures in `Modules/_threadmodule.c` (GH-129794)Bénédikt Tran2025-02-241-76/+83
| | | | | | | | | | Fix UBSan failures for `PyThreadHandleObject`, `lockobject`, `rlockobject`, `localdummyobject`, `localobject` Add safe casts Clean up module functions Use semantically correct parameter names
* gh-129354: Use PyErr_FormatUnraisable() function (#129518)Victor Stinner2025-01-311-16/+27
| | | Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
* gh-59705: Make PYTHREAD_NAME_MAXLEN macro private (#128945)Victor Stinner2025-01-181-10/+10
| | | Rename PYTHREAD_NAME_MAXLEN to _PYTHREAD_NAME_MAXLEN.
* gh-59705: Implement _thread.set_name() on Windows (#128675)Victor Stinner2025-01-171-2/+81
| | | | | | | | | | 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>
* gh-128691: Use deferred reference counting on `_thread._local` (#128693)Sam Gross2025-01-101-0/+4
| | | | | 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.
* gh-128279: Enhance the NetBSD compatibility for thread naming (#128280)Furkan Onder2024-12-281-0/+3
| | | Enhance NetBSD compatibility for thread naming in _threadmodule.c.
* gh-59705: Set OS thread name when Thread.name is changed (#127702)Victor Stinner2024-12-101-2/+1
| | | Co-authored-by: Petr Viktorin <encukou@gmail.com>
* gh-59705: Add _thread.set_name() function (#127338)Victor Stinner2024-12-061-0/+108
| | | | | | | | | | | 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>
* gh-127190: Fix local_setattro() error handling (#127366)Victor Stinner2024-11-281-1/+1
| | | | Don't make the assumption that the 'name' argument is a string. Use repr() to format the 'name' argument instead.
* gh-109746: Make _thread.start_new_thread delete state of new thread on its ↵Radislav Chugunov2024-11-221-0/+1
| | | | | | | | | | 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>
* gh-125139: use `_PyRecursiveMutex` in `_thread.RLock` (#125144)Kumar Aditya2024-10-141-118/+33
|
* gh-117721: use PyMutex in `_thread.lock` (#125110)Kumar Aditya2024-10-081-45/+11
|
* gh-111178: Fix function signatures in _threadmodule.c (#124964)Victor Stinner2024-10-041-49/+70
|
* Fix typos (#123775)algonell2024-09-091-2/+2
|
* gh-120973: Fix thread-safety issues with `threading.local` (#121655)mpage2024-07-191-150/+234
| | | | | | This is a small refactoring to the current design that allows us to avoid manually iterating over threads. This should also fix gh-118490.
* gh-116322: Add Py_mod_gil module slot (#116882)Brett Simmers2024-05-031-0/+1
| | | | | | | | | | | | | | 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).
* gh-118332: Fix deadlock involving stop the world (#118412)Sam Gross2024-04-301-1/+2
| | | | | | 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.
* gh-117764: Add signatures and improve docstrings in the _thread module ↵Serhiy Storchaka2024-04-121-46/+126
| | | | (GH-117772)
* gh-113964: Don't prevent new threads until all non-daemon threads exit (#116677)Sam Gross2024-03-191-1/+1
| | | | | | | | | | 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.
* gh-116915: Make `_thread._ThreadHandle` support GC (#116934)mpage2024-03-181-3/+13
| | | | | | 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.
* gh-114271: Fix race in `Thread.join()` (#114839)mpage2024-03-161-305/+674
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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]: https://github.com/python/cpython/blob/441affc9e7f419ef0b68f734505fa2f79fe653c7/Lib/threading.py#L1201 [^2]: https://github.com/python/cpython/blob/441affc9e7f419ef0b68f734505fa2f79fe653c7/Lib/threading.py#L1115 [^3]: https://github.com/mpage/cpython/commit/81946532792f938cd6f6ab4c4ff92a4edf61314f --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Antoine Pitrou <antoine@python.org>
* gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438)Serhiy Storchaka2024-03-071-6/+2
|
* gh-114271: Make `_thread.lock` thread-safe in free-threaded builds (#116433)mpage2024-03-061-2/+2
| | | | | | | | 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.
* gh-114271: Make `_thread.ThreadHandle` thread-safe in free-threaded builds ↵mpage2024-03-011-40/+127
| | | | | | | | | | | | | | | | | (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.
* gh-110850: Replace private _PyTime_MAX with public PyTime_MAX (#115751)Victor Stinner2024-02-211-1/+1
| | | | | | | 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.
* gh-110850: Use public PyTime functions (#115746)Victor Stinner2024-02-201-1/+1
| | | | | Replace private _PyTime functions with public PyTime functions. random_seed_time_pid() now reports errors to its caller.
* gh-110850: Cleanup pycore_time.h includes (#115724)Victor Stinner2024-02-201-0/+1
| | | | | <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.
* gh-110850: Replace _PyTime_t with PyTime_t (#115719)Victor Stinner2024-02-201-6/+6
| | | | | Run command: sed -i -e 's!\<_PyTime_t\>!PyTime_t!g' $(find -name "*.c" -o -name "*.h")
* gh-114271: Make `thread._rlock` thread-safe in free-threaded builds (#115102)mpage2024-02-161-10/+22
| | | | | | | | | 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.
* gh-114570: Add PythonFinalizationError exception (#115352)Victor Stinner2024-02-141-1/+1
| | | | | | | | | | | | | | | | | 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.
* gh-114271: Make `PyInterpreterState.threads.count` thread-safe in ↵mpage2024-02-121-3/+3
| | | | | free-threaded builds (gh-115093) Use atomics to mutate PyInterpreterState.threads.count.
* gh-115035: Mark ThreadHandles as non-joinable earlier after forking (#115042)Sam Gross2024-02-061-17/+36
| | | | | | 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.
* gh-114315: Make `threading.Lock` a real class, not a factory function (#114479)Nikita Sobolev2024-01-251-4/+29
| | | | | | | | | | `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>
* gh-114414: Assert PyType_GetModuleByDef result in _threadmodule (#114415)Nikita Sobolev2024-01-221-0/+3
|
* gh-111789: Use PyDict_GetItemRef() in Modules/_threadmodule.c (gh-112077)Serhiy Storchaka2023-11-271-6/+4
|
* gh-111262: Add PyDict_Pop() function (#112028)Victor Stinner2023-11-141-5/+2
| | | | | | | _PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int. Co-authored-by: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
* GH-110829: Ensure Thread.join() joins the OS thread (#110848)Antoine Pitrou2023-11-041-55/+282
| | | | | | | Joining a thread now ensures the underlying OS thread has exited. This is required for safer fork() in multi-threaded processes. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
* gh-108082: Remove _PyErr_WriteUnraisableMsg() (GH-111643)Serhiy Storchaka2023-11-031-2/+2
| | | | Replace the remaining calls with PyErr_FormatUnraisable().
* gh-106320: Re-add some PyLong/PyDict C-API functions (GH-#111162)scoder2023-10-251-1/+0
| | | | | | | | * gh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement. See https://github.com/python/cpython/issues/111140 See https://github.com/python/cpython/issues/111139 * gh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.