Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing child process when parent crashes in python

I am trying to write a python program to test a server written in C. The python program launches the compiled server using the subprocess module:

pid = subprocess.Popen(args.server_file_path).pid

This works fine, however if the python program terminates unexpectedly due to an error, the spawned process is left running. I need a way to ensure that if the python program exits unexpectedly, the server process is killed as well.

Some more details:

  • Linux or OSX operating systems only
  • Server code can not be modified in any way
like image 487
charliehorse55 Avatar asked Jan 02 '13 20:01

charliehorse55


1 Answers

I would atexit.register a function to terminate the process:

import atexit
process = subprocess.Popen(args.server_file_path)
atexit.register(process.terminate)
pid = process.pid

Or maybe:

import atexit
process = subprocess.Popen(args.server_file_path)
@atexit.register
def kill_process():
    try:
        process.terminate()
    except OSError:
        pass #ignore the error.  The OSError doesn't seem to be documented(?)
             #as such, it *might* be better to process.poll() and check for 
             #`None` (meaning the process is still running), but that 
             #introduces a race condition.  I'm not sure which is better,
             #hopefully someone that knows more about this than I do can 
             #comment.

pid = process.pid

Note that this doesn't help you if you do something nasty to cause python to die in a non-graceful way (e.g. via os._exit or if you cause a SegmentationFault or BusError)

like image 130
mgilson Avatar answered Sep 21 '22 12:09

mgilson