14

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
charliehorse55
  • 1,940
  • 5
  • 24
  • 38

1 Answers1

21

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)

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • @ire_and_curses -- Thanks for adding the link. it's appreciated. – mgilson Jan 02 '13 at 20:20
  • 7
    the "nasty case" is the most interesting one ) – vak Oct 24 '13 at 09:09
  • 1
    I give an option for the "nasty case" in http://stackoverflow.com/questions/25542110/kill-child-process-if-parent-is-killed-in-python/25634723#25634723, but it doesn't help here (it requires access to the client source code). – Bas Wijnen Sep 03 '14 at 01:12