From 44373ca59807b88aefdea65b9618159df2513591 Mon Sep 17 00:00:00 2001 From: Yi Qiang Date: Sun, 30 Mar 2008 14:39:38 -0700 Subject: [PATCH] Fix dsage doctest failures caused by not joining the thread before exiting and not explicitely deleting subprocess.Popen references. --- src/sage/dsage/dsage.py | 16 +++++++++------- src/sage/dsage/interface/dsage_interface.py | 2 +- src/sage/dsage/tests/testdoc.py | 8 ++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/sage/dsage/dsage.py b/src/sage/dsage/dsage.py index 884242b986d..aa9e07e6698 100644 --- a/src/sage/dsage/dsage.py +++ b/src/sage/dsage/dsage.py @@ -52,7 +52,7 @@ def spawn(cmd, verbose=True, stdout=None, stdin=None): if verbose: print 'Spawned %s (pid = %s)\n' % (' '.join(cmdl), process.pid) - return process.pid + return process class DistributedSage(object): @@ -174,17 +174,19 @@ def kill_all(self): self.kill_worker() self.kill_server() - def kill_worker(self): try: - os.kill(self.worker_pid, 9) + os.kill(self.worker_proc.pid, 9) + self.worker_proc.wait() + del self.worker_proc except OSError, msg: print 'Error killing worker: %s' % msg - def kill_server(self): try: - os.kill(self.server_pid, 9) + os.kill(self.server_proc.pid, 9) + self.server_proc.wait() + del self.server_proc except OSError, msg: print 'Error killing server: %s' % msg @@ -248,7 +250,7 @@ def write_tac(tac): except: pass cmd += '--logfile=%s -y dsage_server.tac' % (log_file) - server_pid = spawn(cmd, verbose=verbose) + self.server_proc = spawn(cmd, verbose=verbose) # Need the following hack since subprocess.Popen reports the wrong # pid when launching an application with twistd while True: @@ -288,7 +290,7 @@ def worker(self, server='localhost', port=8081, workers=2, poll=1.0, if not blocking: cmd += ' --noblock' cmd = 'python ' + SAGE_ROOT + '/local/bin/' + cmd - self.worker_pid = spawn(cmd, verbose=verbose) + self.worker_proc = spawn(cmd, verbose=verbose) else: cmd = 'python ' + SAGE_ROOT + '/local/bin/' + cmd os.system(cmd) diff --git a/src/sage/dsage/interface/dsage_interface.py b/src/sage/dsage/interface/dsage_interface.py index baee5643646..66bafa1d2a8 100644 --- a/src/sage/dsage/interface/dsage_interface.py +++ b/src/sage/dsage/interface/dsage_interface.py @@ -868,4 +868,4 @@ def handler(signum, frame): while self.status != 'completed': time.sleep(1.0) self.get_job() - signal.alarm(0) \ No newline at end of file + signal.alarm(0) diff --git a/src/sage/dsage/tests/testdoc.py b/src/sage/dsage/tests/testdoc.py index 7f4cb320c43..58e8ee035a5 100644 --- a/src/sage/dsage/tests/testdoc.py +++ b/src/sage/dsage/tests/testdoc.py @@ -38,4 +38,12 @@ True sage: t.result # long time 15 + + The following code block makes sure that things exit cleanly + + sage: dsage.kill_all() + sage: from twisted.internet import reactor + sage: reactor.callFromThread(reactor.stop) + sage: d._dsage_thread.join() + [DSage] Closed connection to localhost """ \ No newline at end of file