Browse Source

HUP now closes all open files before execv. Closes #1270

Ask Solem 12 years ago
parent
commit
803655b79c
2 changed files with 14 additions and 6 deletions
  1. 3 0
      celery/apps/worker.py
  2. 11 6
      celery/platforms.py

+ 3 - 0
celery/apps/worker.py

@@ -353,6 +353,9 @@ else:
 
 def _clone_current_worker():
     if os.fork() == 0:
+        platforms.close_open_fds([
+            sys.__stdin__, sys.__stdout__, sys.__stderr__,
+        ])
         os.execv(sys.executable, [sys.executable] + sys.argv)
 
 

+ 11 - 6
celery/platforms.py

@@ -254,12 +254,22 @@ def _create_pidlock(pidfile):
 
 def fileno(f):
     """Get object fileno, or :const:`None` if not defined."""
+    if isinstance(f, int):
+        return f
     try:
         return f.fileno()
     except AttributeError:
         pass
 
 
+def close_open_fds(keep=None):
+    keep = [fileno(f) for f in keep if fileno(f)] if keep else []
+    for fd in reversed(range(get_fdmax(default=2048))):
+        if fd not in keep:
+            with ignore_errno(errno.EBADF):
+                os.close(fd)
+
+
 class DaemonContext(object):
     _is_open = False
 
@@ -287,12 +297,7 @@ class DaemonContext(object):
             if self.after_chdir:
                 self.after_chdir()
 
-            preserve = [fileno(f) for f in self.stdfds if fileno(f)]
-            for fd in reversed(range(get_fdmax(default=2048))):
-                if fd not in preserve:
-                    with ignore_errno(errno.EBADF):
-                        os.close(fd)
-
+            close_open_fds(self.stdfds)
             for fd in self.stdfds:
                 self.redirect_to_null(fileno(fd))