Created
July 11, 2019 02:36
-
-
Save qxcv/fe5be4d14f855fedf7a5db723aad22c2 to your computer and use it in GitHub Desktop.
Forcing Python process to exit with parent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import sys | |
def parent_death_pact(signal=signal.SIGINT): | |
"""Commit to kill current process when parent process dies. | |
Each time you spawn a new process, run this to set signal | |
handler appropriately (e.g put it at the beginning of each | |
script, and in multiprocessing startup code).""" | |
assert sys.platform == 'linux', \ | |
"this fn only works on Linux right now" | |
libc = ctypes.CDLL("libc.so.6") | |
# see include/uapi/linux/prctl.h in kernel | |
PR_SET_PDEATHSIG = 1 | |
# last three args are unused for PR_SET_PDEATHSIG | |
retcode = libc.prctl(PR_SET_PDEATHSIG, signal, 0, 0, 0) | |
if retcode != 0: | |
raise Exception("prctl() returned nonzero retcode %d" % retcode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment