Using sys.exit() does not release PID
fedus opened this issue · 2 comments
When the python script running as a service uses sys.exit(), the process seems to quit without doing any of the service class' "clean up" functions that are supposed to be executed once the run() method finishes.
I see how this behaviour is actually not necessarily a bug, but is there any way of handling sys.exit() calls in a way that terminates the service gracefully?
In general, the daemon should not use sys.exit
at all, since it's a forked child of the original control process and hence should use os._exit
instead. However, calling os._exit
from within the daemon's run
method isn't a good idea, either, since that (by definition) immediately exits without any chance for cleanup.
The intended way of shutting down the daemon from within run
is to simply return
. That should be documented.
In addition, handling a call to sys.exit
in run
correctly would be rather easy, all we have to do is to catch the corresponding SystemExit
exception, clean up, and exit.
In the meantime, you can do that yourself, @fedus:
def run(self):
try:
# Your usual daemon code goes here
except SystemExit:
# Swallow exception from sys.exit so that we can clean up
return