Skip to content

Instantly share code, notes, and snippets.

@ambv
Last active March 10, 2017 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambv/00641bc0fc101fc91077860649fc6017 to your computer and use it in GitHub Desktop.
Save ambv/00641bc0fc101fc91077860649fc6017 to your computer and use it in GitHub Desktop.
Defensive programming for unhandled errors. Add this to preexec_fn= in your subprocess calls.
import ctypes
import sys
import signal
def ensure_dead_with_parent():
"""A last resort measure to make sure this process dies with its parent.
Defensive programming for unhandled errors.
"""
if not sys.platform.startswith('linux'):
return # not supported on OS X, Windows, etc. Use process groups.
PR_SET_PDEATHSIG = 1 # include/uapi/linux/prctl.h
libc = ctypes.CDLL(ctypes.util.find_library('c'))
libc.prctl(PR_SET_PDEATHSIG, signal.SIGKILL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment