From: Avery Pennarun Date: Sun, 13 Feb 2011 10:29:59 +0000 (-0800) Subject: cmd/daemon: close file descriptors correctly in parent process. X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=a5ef544d3152e3c587a00f809bed1ae8fff29c39;p=packages%2Fb%2Fbup.git cmd/daemon: close file descriptors correctly in parent process. The client wasn't getting disconnected when the server died, because the daemon was still hanging on to its copy of the original socket, due to some misplaced os.dup() calls. Signed-off-by: Avery Pennarun --- diff --git a/cmd/daemon-cmd.py b/cmd/daemon-cmd.py index b83669a..c46f411 100755 --- a/cmd/daemon-cmd.py +++ b/cmd/daemon-cmd.py @@ -49,10 +49,16 @@ try: [rl,wl,xl] = select.select(socks, [], [], 60) for l in rl: s, src = l.accept() - log("Socket accepted connection from %s\n" % (src,)) - sp = subprocess.Popen([path.exe(), 'mux', '--', 'server'] + extra, - stdin=os.dup(s.fileno()), stdout=os.dup(s.fileno())) - s.close() + try: + log("Socket accepted connection from %s\n" % (src,)) + fd1 = os.dup(s.fileno()) + fd2 = os.dup(s.fileno()) + sp = subprocess.Popen([path.exe(), 'mux', '--', 'server'] + + extra, stdin=fd1, stdout=fd2) + finally: + s.close() + os.close(fd1) + os.close(fd2) finally: for l in socks: l.shutdown(socket.SHUT_RDWR)