git » pymisc » commit 665d0b5

Close unneeded fds in pcmp.Channel.

author Alberto Bertogli
2007-02-01 14:14:32 UTC
committer Alberto Bertogli
2007-02-01 14:14:32 UTC
parent bb86d4ecf305b5e1e5a8096474a23f87cb0a2b85

Close unneeded fds in pcmp.Channel.
According to pipe(7), read() or write() on a broken pipe only return EPIPE
when all fds referring to the other end of the pipe are closed. When a child
dies, it closes their fds, but if we haven't closed ours, no EPIPE will be
reported.

This patch fixes it by closing the unused fds in .child() and .parent().

pcmp.py +12 -4

diff --git a/pcmp.py b/pcmp.py
index 16aa688..4bf76a3 100644
--- a/pcmp.py
+++ b/pcmp.py
@@ -39,13 +39,21 @@ class Channel (object):
 
 	def child(self):
 		"Tell the object we're the child."
-		self._wfd = os.fdopen(self._c2p[1], 'w')  # we write via p1.w
-		self._rfd = os.fdopen(self._p2c[0], 'r')  # and read via p2.r
+		# write via p1.w, read via p2.r, and close unnecessary fds
+		# (see man 7 pipe)
+		self._wfd = os.fdopen(self._c2p[1], 'w')
+		self._rfd = os.fdopen(self._p2c[0], 'r')
+		os.close(self._c2p[0])
+		os.close(self._p2c[1])
 
 	def parent(self):
 		"Tell the object we're the parent."
-		self._wfd = os.fdopen(self._p2c[1], 'w')  # we write via p2.w
-		self._rfd = os.fdopen(self._c2p[0], 'r')  # we read via p1.r
+		# write via p2.w, read via p1.r, and close unnecessary fds
+		# (see man 7 pipe)
+		self._wfd = os.fdopen(self._p2c[1], 'w')
+		self._rfd = os.fdopen(self._c2p[0], 'r')
+		os.close(self._p2c[0])
+		os.close(self._c2p[1])
 
 	def send(self, o):
 		"Send an object."