classification
Title: TypeError: 'int' object is not callable if the signal handler is SIG_IGN
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Possible race condition between signal catching and signal.signal
View: 43406
Assigned To: Nosy List: Ronald Li, ethan.furman, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-12-31 00:06 by Ronald Li, last changed 2021-03-04 23:20 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
ign2ndsig.py Ronald Li, 2019-12-31 00:06 ign2ndsig.py for demonstrating the behavior
Messages (1)
msg359071 - (view) Author: Ronald Li (Ronald Li) Date: 2019-12-31 00:06
The attached script ign2ndsig.py demonstrates an attempted way to handle signals at most once and ignore any subsequent signals. SIGINT and SIGTERM are used in the demo.


In Python 3.5, the subprocess would go into one of the "except KeyboardInterrupt:" or "except SystemExit:" blocks before doing "finally:" and exiting zero:


# execute the script with no args:
# ./ign2ndsig.py

subproc: sys.version_info(major=3, minor=5, micro=9, releaselevel='final', serial=0)
raising KeyboardInterrupt
handling KeyboardInterrupt
doing finally
rc: 0



In Python 3.6, 3.7 or 3.8, the subprocess would go into neither the "except KeyboardInterrupt:" nor "except SystemExit:" blocks before doing "finally:" and exiting non-zero, with a traceback like this:


subproc: sys.version_info(major=3, minor=6, micro=10, releaselevel='final', serial=0)
raising KeyboardInterrupt
doing finally
Traceback (most recent call last):
  File "./ign2ndsig.py", line 30, in subproc
    input()
  File "./ign2ndsig.py", line 18, in handler
    raise KeyboardInterrupt
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./ign2ndsig.py", line 58, in <module>
    subproc()
  File "./ign2ndsig.py", line 32, in subproc
    printef('handling KeyboardInterrupt')
  File "./ign2ndsig.py", line 10, in printef
    print(*objects, file=sys.stderr, flush=True)
TypeError: 'int' object is not callable
rc: 1



More on the behaviors of ign2ndsig.py :
1. Replacing SIG_IGN at line 14 and 15 with a no-op Python function "fixes" the problem - such that in Python 3.6, 3.7 or 3.8, it'd behave similarly to the Python 3.5 output above.
2. Sending a SIGTERM then a SIGINT (instead of a SIGINT followed by a SIGTERM) (i.e. reversing the order of SIGINT and SIGTERM) at line 49-50 results in a similar behavior (that the TypeError is raised).
3. Sending 2 SIGINTs or 2 SIGTERMs (instead of 1 SIGINT + 1 SIGTERM) (i.e. sending the same signal twice) at line 49-50 does NOT result in the TypeError.


Potentially related issues:

_thread.interrupt_main() errors if SIGINT handler in SIG_DFL, SIG_IGN
https://bugs.python.org/issue23395

Turn SIG_DFL and SIG_IGN into functions
https://bugs.python.org/issue23325
History
Date User Action Args
2021-03-04 23:20:50pitrousetstatus: open -> closed
superseder: Possible race condition between signal catching and signal.signal
resolution: duplicate
stage: test needed -> resolved
2020-01-04 06:31:21terry.reedysetstage: test needed
versions: + Python 3.9, - Python 3.6
2019-12-31 09:11:03serhiy.storchakasetnosy: + ethan.furman, serhiy.storchaka
2019-12-31 00:06:25Ronald Licreate