Title: Doc: signal.sig(timed)wait do not work outside the main thread
Type: enhancement Stage:
Components: Documentation Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, martin.panter,
Priority: normal Keywords:

Created on 2017-01-25 17:25 by, last changed 2021-10-22 10:01 by iritkatriel.

Messages (2)
msg286269 - (view) Author: Petr MOTEJLEK ( * Date: 2017-01-25 17:25

The documentation for signal.signal() clearly states that it is only supposed to be called on MainThread

However, it does not say so for the signal.sigwait() and neither signal.sigtimedwait()

I think this is an error on the documentation side of things (unless I misread it). When either signal.sigwait or signal.sigtimedwait are called outside MainThread, they simply never catch any signals (signal.sigwait blocks indefinitely)

I did not test this on Windows, but on both Linux and OS X the behavior is the same

Consider the below simple code

  import signal
  import os
  def sigwait():
    print("Send me a signal, my PID is {p}".format(p=os.getpid()))  
    print("Got the signal: {i}".format(i=signal.sigwait((signal.SIGUSR1,))))

If sigwait() is called on MainThread and the process receives SIGUSR1, "Got the signal: ..." gets printed. However, calling sigwait in a different thread blocks the thread indefinitely. The behavior is the same with signal.sigtimedwait() as well
msg286284 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-01-25 22:48
This works for me on Linux:

>>> signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR1})
>>> import threading
>>> t = threading.Thread(target=sigwait)
>>> t.start()
Send me a signal, my PID is 24197
>>> os.kill(os.getpid(), signal.SIGUSR1)
Got the signal: 10
>>> t.join()

Posix <> only defines sigwait() if the signals are already blocked. Two reasons behind this come to mind:

1. There would be a race where the signal handler may be called first (or the signal may be ignored) at the OS level, and the sigwait() function will miss the signal and block.

2. If the signal is handled in the context of another thread that isn’t using sigwait(), it may be consumed (handled or ignored) in the context of the other thread, with no effect on your sigwait() call.

This detail of blocking the signal seems to be a common error, so maybe the Python documentation could help point it out. (Issue 25868 comes to mind; there is a test case that IMO should block a signal before waiting for it.)
Date User Action Args
2021-10-22 10:01:01iritkatrielsettype: behavior -> enhancement
versions: + Python 3.11, - Python 3.5, Python 3.6, Python 3.7
2017-01-27 19:38:37terry.reedysettitle: Doc bug: signal.sigwait and signal.sigtimedwait do not work outside the Main thread -> Doc: signal.sig(timed)wait do not work outside the main thread
2017-01-25 22:48:24martin.pantersetnosy: + martin.panter

messages: + msg286284
versions: - Python 3.4
2017-01-25 17:25:31petr@motejlek.netcreate