classification
Title: signal.sigwait* do not intercept certain signals
Type: behavior Stage: resolved
Components: Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Blindfreddy, pitrou
Priority: normal Keywords:

Created on 2019-09-26 11:19 by Blindfreddy, last changed 2019-10-04 12:44 by pitrou. This issue is now closed.

Messages (3)
msg353294 - (view) Author: (Blindfreddy) Date: 2019-09-26 11:19
On debian, signal.sigwait/sigwaitinfo/sigtimedwait do not properly handle the below signals (NOK).

Steps to reproduce

1. start python and type:
>>> import signal
>>> signal.sigwait([<any of the below signals with NOK])
2. send corresponding signal to the python process

Signal Name	Signal Number	Result	Description / Output
SIGHUP	1	NOK	Process terminates with 'HANGUP'
SIGINT	2	OK	
SIGQUIT	3	NOK	Process terminates with 'QUIT'
SIGILL	4	OK	
SIGTRAP	5	NOK	Process terminates with 'Trace/breakpoint trap'
SIGABRT	6	OK	
SIGEMT	7	NOK	Process terminates with 'Bus error'
SIGFPE	8	OK	
SIGKILL	9	OK	cannot be caught by design
SIGBUS	10	NOK	Process terminates with 'User defined signal 1'
SIGSEGV	11	OK	
SIGSYS	12	NOK	Process terminates with 'User defined signal 2'
SIGPIPE	13	NOK	signal not handled
SIGALRM	14	NOK	Process terminates with 'Alarm clock'
SIGTERM	15	NOK	Process terminates with 'Terminated'
SIGURG	16	NOK	Process terminates with 'Stack fault'
SIGSTOP	17	NOK	signal not handled
SIGCHLD	20	OK	
SIGUSR1	30	NOK	Process terminates with 'Power failure''
SIGUSR2	31	OK
msg353939 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2019-10-04 12:43
Python just exposes thin wrappers around the underlying libc calls, so you have to understand how those work.

On Linux, the sigwaitinfo() man page says:

NOTES
       In normal usage, the calling program blocks the signals  in
       set via a prior call to sigprocmask(2) (so that the default
       disposition for these signals does not occur if they become
       pending  between  successive calls to sigwaitinfo() or sig‐
       timedwait()) and does not establish handlers for these sig‐
       nals.

So you need to block the given signal with pthread_sigmask() before waiting on it.  For example:

>>> import signal
>>> signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGHUP])
set()
>>> signal.sigwait([signal.SIGHUP])
<Signals.SIGHUP: 1>
msg353940 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2019-10-04 12:44
Closing as not a Python bug.
History
Date User Action Args
2019-10-04 12:44:55pitrousetstatus: open -> closed
resolution: not a bug
messages: + msg353940

stage: resolved
2019-10-04 12:43:51pitrousetnosy: + pitrou
messages: + msg353939
2019-09-26 11:19:03Blindfreddycreate