[issue21895] signal.pause() doesn't wake up on SIGCHLD in non-main thread

Bohuslav "Slavek" Kabrda report at bugs.python.org
Tue Jul 1 12:01:35 CEST 2014


New submission from Bohuslav "Slavek" Kabrda:

Reproducer attached. To describe the problem in words, one needs to do this to reproduce this issue:
- Create a signal handler and register it using signal.signal to SIGCHLD.
- Create a thread that invokes a subprocess, e.g. "sleep 1" and then continues to do something for time period longer than the subprocess is running.
- Run this thread from main thread and call signal.pause() in the main thread.
- The pause() call is never interrupted and the signal handler is never run.

This happens because:
- Python adds signal_handler as a handler to the specified signal and stores the passed Python function in structure Handlers.
- When signal.pause() is run, the main thread releases the GIL, so that other threads can run.
- The non-main thread gets to lease and actually invokes the subprocess and then continues to do something.
- When subprocess finishes, it sends the signal *to the thread that invoked it* (assuming this thread is still running). This means that the signal.pause() call is not interrupted and main thread continues to sleep.
- The non-main thread adds handler call to the list of pending calls using Py_AddPendingCall.
- Pending calls are checked in Py_MakePendingCalls, which is called in every iteration of the bytecode evaluation loop in PyEval_EvalFrameEx.
- The problem is that since pause() isn't un-paused and hangs forever, the evaluation loop never gets to another iteration, hence can't do any pending call.

This happens on all Python versions I've tried, using pthread threading.
I think this could *possibly* be solved by issuing a pthread_kill from the non-main thread to the main thread to wake it up, but I'm not sure what all the implications of such a change would be.

----------
components: Interpreter Core
files: signal_pause_doesnt_wake_up.py
messages: 222021
nosy: bkabrda
priority: normal
severity: normal
status: open
title: signal.pause() doesn't wake up on SIGCHLD in non-main thread
versions: Python 2.7, Python 3.4, Python 3.5
Added file: http://bugs.python.org/file35815/signal_pause_doesnt_wake_up.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21895>
_______________________________________


More information about the Python-bugs-list mailing list