Repository navigation
Open
Labels
extension-modulesC modules in the Modules dirtopic-free-threadingtype-bugAn unexpected behavior, bug, or error
Description
Context
The Dask threaded scheduler (e.g. dask.array or dask.dataframe without dask.distributed) blocks the main thread when the user calls compute() and internally spawns a concurrent.futures.ThreadPoolExecutor to schedule tasks. If a user hits Ctrl+C, they expect the program to terminate soon (at most waiting for the tasks that are halfway through execution to terminate).
Bug report
This sequence frequently deadlocks on Linux x64 free-threaded:
- main thread calls
threading.Thread.start - another thread calls
signal.pthread_kill(main_thread, signal.SIGINT), which lands afterstart()has returned but before the thread'stargetfunction has started executing - main thread handles the resulting
KeyboardInterrupt - main thread calls
.join()on the child thread started at point 1. - frequently,
.join()deadlocks. A second SIGINT (e.g. from Ctrl+C) unblocks it.
Reproducer
import signal
import threading
USE_BARRIER = False
NTHREADS = 20
def demo():
main_thread = threading.get_ident()
barrier = threading.Barrier(NTHREADS) if USE_BARRIER else None
thread_can_die = threading.Event()
def f() -> None:
if barrier:
barrier.wait()
thread_can_die.wait()
def interrupt() -> None:
if barrier:
barrier.wait()
signal.pthread_kill(main_thread, signal.SIGINT)
threads = []
for _ in range(NTHREADS - 1):
t = threading.Thread(target=f)
t.start()
threads.append(t)
try:
t = threading.Thread(target=interrupt)
t.start()
threads.append(t)
for t in threads:
t.join()
except KeyboardInterrupt:
pass
else:
assert False, "Expected KeyboardInterrupt"
thread_can_die.set()
for t in threads:
t.join()
if __name__ == "__main__":
i = 0
while True:
print(i, flush=True)
demo()
i += 1On these setups, the above program hangs within a handful of cycles:
- Linux x64 3.14.5t
- Linux x64 git tip #7a468a101268d2b13105f94ae027df8b502d0c87 (built with
Tools/pixi-packages/freethreading)
Cannot reproduce on
- Linux x64 3.14.5
Setting USE_BARRIER=True, which ensures that all child threads are already fully started when the SIGINT lands on the main thread, makes the issue disappear.
CPython versions tested on:
CPython main branch, 3.14
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirtopic-free-threadingtype-bugAn unexpected behavior, bug, or error
Activity
picnixz commentedon Jun 3, 2026
cc @vstinner
vstinner commentedon Jun 4, 2026
It's not easy to make sure that the main thread is interrupted immediately (
raise KeyboardInterrupt) when it receives a signal. It depends exactly when the signal is received.Call stack:
t.join()=>ThreadHandle_join()=>PyEvent_WaitTimed()=>_PyParkingLot_Park()=>_PySemaphore_Wait()=>sem_wait()(on Linux).When the signal is sent during
sem_wait(),_PySemaphore_Wait()and_PyParkingLot_Park()returnPy_PARK_INTR. IfThreadHandle_join()detects thatPyEvent_WaitTimed()was interrupted, it callsPy_MakePendingCalls()which executes pending signal handlers. In this case, we are good, the code works as expected.The problem is when the signal is received by PyEvent_WaitTimed(), before sem_wait() call. In this case, sem_wait() hangs and is not interrupted, even if Python has a pending signal and we are in the main thread.
_PyParkingLot_Park()can be called with no attached thread state, or it can detach the current thread state. So the Python C API cannot be used:PyErr_CheckSignals()cannot be called after the thread state is detached.vstinner commentedon Jun 4, 2026
cc @colesbury
colesbury commentedon Jun 4, 2026
Is this an issue primarily in Dask test code (as opposed to application code)?
The underlying problem, which Victor described, is common to all Python versions. If the ctrl-c arrives in C code, but before
sem_wait()(or other similar calls), then it won't interrupt the thread and you need to press ctrl-c again.The difference with free-threading is a matter of timing in the test code, but not in real applications. The GIL controls the synchronization of the
signal.pthread_kill()with respect to thethread.join(), but it doesn't affect when a user presses ctrl-c or when the OS triggers the signal handling.I don't think this is something we can fix in CPython.
crusaderky commentedon Jun 4, 2026
I think it's in application code.
Here's a reproducer where the human has to actually hit Ctrl+C. I managed to get it hanging after 16 cycles on the first go and after 54 cycles on the second go. YMMV.
The mitigation factor in real Dask use cases is that Dask keeps a warm global ThreadPoolExecutor, which is cleaned up by
atexit.register(default_pool.shutdown). So this issue will manifest only if the user hits Ctrl+C when the executor needs an extra thread for the very first time since process start.colesbury commentedon Jun 4, 2026
I get the same behavior when running that in 3.14 and 3.14t. Most ctrl-c presses work every so often I get something like: