[PATCH] Reliably close stdin in 'qm.executable.Filter'

Nathaniel Smith njs at pobox.com
Thu Aug 14 23:38:53 UTC 2003


This patch modifies 'TimeoutExecutable' so that the monitor process it
creates closes all fds; this fixes a bug where children spawned by
'Filter' would not see their stdin closed at the appropriate time.
Also adds a new test for this.

-- Nathaniel

-- 
"But in Middle-earth, the distinct accusative case disappeared from
the speech of the Noldor (such things happen when you are busy
fighting Orcs, Balrogs, and Dragons)."
-------------- next part --------------
diff -urN --exclude='*~' --exclude='.*' --exclude=CVS --exclude='*.pyo' --exclude='*.pyc' --exclude=build --exclude=GNUmakefile --exclude=config.log --exclude=config.status --exclude=config.cache --exclude=qmtest --exclude=qm.spec --exclude='*.html' --exclude='*.dtd' --exclude=CATALOG --exclude=__version.py --exclude=thread_target --exclude=process_target qm-clean/ChangeLog qm-close-stdin/ChangeLog
--- qm-clean/ChangeLog	2003-08-14 12:16:07.000000000 -0700
+++ qm-close-stdin/ChangeLog	2003-08-14 16:34:48.000000000 -0700
@@ -1,5 +1,12 @@
 2003-08-14  Nathaniel Smith  <njs at codesourcery.com>
 
+	* qm/executable.py (TimeoutExecutable._HandleChild): Close all
+	fds in the monitor process.
+	* tests/xmldb/test.qms/classes.qms/scripttest_closes_stdin.qmt:
+	New test.
+	
+2003-08-14  Nathaniel Smith  <njs at codesourcery.com>
+
 	* qm/executable.py (RedirectedExecutable._InitializeChild): Fix
 	indentation.
 
diff -urN --exclude='*~' --exclude='.*' --exclude=CVS --exclude='*.pyo' --exclude='*.pyc' --exclude=build --exclude=GNUmakefile --exclude=config.log --exclude=config.status --exclude=config.cache --exclude=qmtest --exclude=qm.spec --exclude='*.html' --exclude='*.dtd' --exclude=CATALOG --exclude=__version.py --exclude=thread_target --exclude=process_target qm-clean/qm/executable.py qm-close-stdin/qm/executable.py
--- qm-clean/qm/executable.py	2003-08-14 12:15:59.000000000 -0700
+++ qm-close-stdin/qm/executable.py	2003-08-14 16:34:56.000000000 -0700
@@ -464,6 +464,19 @@
                 # group as the child 
                 os.setpgid(self.__monitor_pid, child_pid)
             else:
+                # Close all fds.  This is especially important to avoid
+                # keeping any pipes to the child process open after our
+                # parent has closed them.
+                try:
+                    max_fds = os.sysconf("SC_OPEN_MAX")
+                except:
+                    max_fds = 256
+                for fd in range(max_fds):
+                    try:
+                        os.close(fd)
+                    except:
+                        pass
+
                 try:
                     # Put the monitoring process into the child's process
                     # group.  We know the process group still exists at this
Binary files qm-clean/results.qmr and qm-close-stdin/results.qmr differ
diff -urN --exclude='*~' --exclude='.*' --exclude=CVS --exclude='*.pyo' --exclude='*.pyc' --exclude=build --exclude=GNUmakefile --exclude=config.log --exclude=config.status --exclude=config.cache --exclude=qmtest --exclude=qm.spec --exclude='*.html' --exclude='*.dtd' --exclude=CATALOG --exclude=__version.py --exclude=thread_target --exclude=process_target qm-clean/tests/xmldb/test.qms/classes.qms/scripttest_closes_stdin.qmt qm-close-stdin/tests/xmldb/test.qms/classes.qms/scripttest_closes_stdin.qmt
--- qm-clean/tests/xmldb/test.qms/classes.qms/scripttest_closes_stdin.qmt	1969-12-31 16:00:00.000000000 -0800
+++ qm-close-stdin/tests/xmldb/test.qms/classes.qms/scripttest_closes_stdin.qmt	2003-08-14 13:22:55.000000000 -0700
@@ -0,0 +1,13 @@
+<?xml version="1.0" ?>
+<!DOCTYPE extension
+  PUBLIC '-//Software Carpentry//QMTest Extension V0.1//EN'
+  'http://www.software-carpentry.com/qm/xml/extension'>
+<extension class="command.ShellScriptTest" kind="test"><argument name="stdin"><text>foo</text></argument><argument name="stdout"><text>Going to read, expecting data
+Read foo
+Going to read, expecting nothing
+Read </text></argument><argument name="stderr"><text/></argument><argument name="script"><text>echo "Going to read, expecting data"
+read FOO
+echo "Read $FOO"
+echo "Going to read, expecting nothing"
+read BAR
+echo "Read $BAR"</text></argument><argument name="prerequisites"><set/></argument><argument name="target_group"><text>.*</text></argument><argument name="exit_code"><integer>0</integer></argument><argument name="environment"><set/></argument><argument name="arguments"><set/></argument><argument name="timeout"><integer>5</integer></argument><argument name="resources"><set/></argument></extension>
\ No newline at end of file


More information about the qmtest mailing list