diff hgext/inotify/server.py @ 7451:fca9947652ce

inotify: close most file descriptors when autostarting Otherwise, operations that autostart while talking to an SSH repository prevent SSH stderr from closing normally. This causes hangs at the end of hg clone or hg pull -u.
author Brendan Cully <brendan@kublai.com>
date Mon, 01 Dec 2008 13:38:26 -0800
parents b4ac1e2cd38c
children 67e59a9886d5
line wrap: on
line diff
--- a/hgext/inotify/server.py	Mon Dec 01 14:20:20 2008 +0100
+++ b/hgext/inotify/server.py	Mon Dec 01 13:38:26 2008 -0800
@@ -708,6 +708,26 @@
                 timeobj.handle_timeout()
 
 def start(ui, repo):
+    def closefds(ignore):
+        # (from python bug #1177468)
+        # close all inherited file descriptors
+        # Python 2.4.1 and later use /dev/urandom to seed the random module's RNG
+        # a file descriptor is kept internally as os._urandomfd (created on demand
+        # the first time os.urandom() is called), and should not be closed
+        try:
+            os.urandom(4)
+            urandom_fd = getattr(os, '_urandomfd', None)
+        except AttributeError:
+            urandom_fd = None
+        ignore.append(urandom_fd)
+        for fd in range(3, 256):
+            if fd in ignore:
+                continue
+            try:
+                os.close(fd)
+            except OSError:
+                pass
+
     m = Master(ui, repo)
     sys.stdout.flush()
     sys.stderr.flush()
@@ -716,6 +736,7 @@
     if pid:
         return pid
 
+    closefds([m.server.fileno(), m.watcher.fileno()])
     os.setsid()
 
     fd = os.open('/dev/null', os.O_RDONLY)