Skip to content
Browse files

closing open filehandlers, thanks /u/ionelmc :cake:

  • Loading branch information...
1 parent 1b7b464 commit de801d053a8d0e3161ff07abbd0e39e2b93f76af @waawal committed Jul 9, 2013
Showing with 28 additions and 20 deletions.
  1. +28 −20 undead.py
View
48 undead.py
@@ -31,6 +31,7 @@ def start(self, action):
import resource
import atexit
import inspect
+ from resource import getrlimit, RLIMIT_NOFILE
from lockfile import FileLock
from logbook import Logger, FileHandler
@@ -45,17 +46,7 @@ def start(self, action):
if not os.path.exists(home):
os.makedirs(home)
self.pid = os.path.join(home, "{0}.pid".format(self.name))
- # Initialize logging.
- self.log = Logger(self.name)
-
- if self.log_handler is None:
- if not os.path.exists(home):
- os.makedirs(home)
- self.log_handler = FileHandler(
- os.path.join(home, "{0}.log".format(self.name)),
- level=self.log_level
- )
- with self.log_handler.applicationbound():
+
process_id = os.fork()
if process_id < 0:
sys.exit(1)
@@ -68,6 +59,12 @@ def start(self, action):
if hasattr(os, "devnull"):
devnull = os.devnull
+ for fd in range(getrlimit(RLIMIT_NOFILE)[0]):
+ try:
+ os.close(fd)
+ except OSError:
+ pass
+
os.open(devnull, os.O_RDWR)
os.dup(0)
os.dup(0)
@@ -83,17 +80,28 @@ def start(self, action):
lockfile.write("{0}".format(os.getpid()))
self.lock.acquire()
+ # Initialize logging.
+ self.log = Logger(self.name)
+
+ if self.log_handler is None:
+ if not os.path.exists(home):
+ os.makedirs(home)
+ self.log_handler = FileHandler(
+ os.path.join(home, "{0}.log".format(self.name)),
+ level=self.log_level
+ )
+ with self.log_handler.applicationbound():
+
+ # Set custom action on SIGTERM.
+ signal.signal(signal.SIGTERM, self._sigterm)
+ atexit.register(self._sigterm)
- # Set custom action on SIGTERM.
- signal.signal(signal.SIGTERM, self._sigterm)
- atexit.register(self._sigterm)
-
- self.log.warning("Starting daemon.")
+ self.log.warning("Starting daemon.")
- args = inspect.getargspec(self.action)[0]
- if 'log' not in args:
- return self.action()
- self.action(log=self.log)
+ args = inspect.getargspec(self.action)[0]
+ if 'log' not in args:
+ return self.action()
+ self.action(log=self.log)
def _sigterm(self, signum=None, frame=None):
import os

0 comments on commit de801d0

Please sign in to comment.
Something went wrong with that request. Please try again.