From 6c78049886877a5c455b2c3f8f1cb38586892918 Mon Sep 17 00:00:00 2001 From: Rob Speed Date: Sat, 10 Jan 2015 00:24:25 -0800 Subject: [PATCH 1/2] Fixes issue where a daemonized OctoPrint doesn't clean up after itself after receiving SIGTERM. --- src/octoprint/daemon.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/octoprint/daemon.py b/src/octoprint/daemon.py index f27a9102..87e53ca9 100644 --- a/src/octoprint/daemon.py +++ b/src/octoprint/daemon.py @@ -4,7 +4,7 @@ Generic linux daemon base class for python 3.x Originally from http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/#c35 """ -import sys, os, time, atexit, signal +import sys, os, time, signal class Daemon: """A generic daemon class. @@ -53,14 +53,15 @@ class Daemon: os.dup2(se.fileno(), sys.stderr.fileno()) # write pidfile - atexit.register(self.delpid) + signal.signal(signal.SIGTERM,self.delpid) pid = str(os.getpid()) with open(self.pidfile,'w+') as f: f.write(pid + '\n') - def delpid(self): + def delpid(self, _signo, _stack_frame): os.remove(self.pidfile) + sys.exit(0) def start(self): """Start the daemon.""" From 062131a10eb6304336605ddead13ef4f7da1c161 Mon Sep 17 00:00:00 2001 From: Rob Speed Date: Sat, 10 Jan 2015 01:34:47 -0800 Subject: [PATCH 2/2] Renamed delpid to term to better match its modified behavior. Moved the registration of the SIGTERM handler to avoid an extremely unlikely race condition. --- src/octoprint/daemon.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/octoprint/daemon.py b/src/octoprint/daemon.py index 87e53ca9..4ef56b16 100644 --- a/src/octoprint/daemon.py +++ b/src/octoprint/daemon.py @@ -53,13 +53,14 @@ class Daemon: os.dup2(se.fileno(), sys.stderr.fileno()) # write pidfile - signal.signal(signal.SIGTERM,self.delpid) - pid = str(os.getpid()) with open(self.pidfile,'w+') as f: f.write(pid + '\n') - - def delpid(self, _signo, _stack_frame): + + # register listener for SIGTERM + signal.signal(signal.SIGTERM, self.term) + + def term(self, _signo, _stack_frame): os.remove(self.pidfile) sys.exit(0)