diff options
author | Ian Lynagh <ian@well-typed.com> | 2013-02-02 19:53:46 +0000 |
---|---|---|
committer | Ian Lynagh <ian@well-typed.com> | 2013-02-02 19:53:46 +0000 |
commit | 91aa609c448bdc04f59adf99431a12f27f6669e4 (patch) | |
tree | b7d8afaad184e00f713307f753349f540d14a712 | |
parent | 0eed595b37d79bff5681fdfbc27bc0cd3fc1b85d (diff) | |
download | haskell-91aa609c448bdc04f59adf99431a12f27f6669e4.tar.gz |
Handle ^C better when threads are being used too
-rw-r--r-- | testsuite/driver/testlib.py | 20 | ||||
-rw-r--r-- | testsuite/timeout/timeout.py | 85 |
2 files changed, 61 insertions, 44 deletions
diff --git a/testsuite/driver/testlib.py b/testsuite/driver/testlib.py index 9f1f759642..54bae8369c 100644 --- a/testsuite/driver/testlib.py +++ b/testsuite/driver/testlib.py @@ -1871,6 +1871,15 @@ def rawSystem(cmd_and_args): else: return os.spawnv(os.P_WAIT, cmd_and_args[0], cmd_and_args) +# Note that this doesn't handle the timeout itself; it is just used for +# commands that have timeout handling built-in. +def rawSystemWithTimeout(cmd_and_args): + r = rawSystem(cmd_and_args) + if r == 98: + # The python timeout program uses 98 to signal that ^C was pressed + stopNow() + return r + # cmd is a complex command in Bourne-shell syntax # e.g (cd . && 'c:/users/simonpj/darcs/HEAD/compiler/stage1/ghc-inplace' ...etc) # Hence it must ultimately be run by a Bourne shell @@ -1890,7 +1899,7 @@ def runCmd( cmd ): assert config.timeout_prog!='' if config.timeout_prog != '': - r = rawSystem([config.timeout_prog, str(config.timeout), cmd]) + r = rawSystemWithTimeout([config.timeout_prog, str(config.timeout), cmd]) else: r = os.system(cmd) return r << 8 @@ -1906,13 +1915,14 @@ def runCmdFor( name, cmd, timeout_multiplier=1.0 ): if config.timeout_prog != '': if config.check_files_written: fn = name + ".strace" - r = rawSystem(["strace", "-o", fn, "-fF", "-e", "creat,open,chdir,clone,vfork", - config.timeout_prog, str(timeout), - cmd]) + r = rawSystemWithTimeout( + ["strace", "-o", fn, "-fF", + "-e", "creat,open,chdir,clone,vfork", + config.timeout_prog, str(timeout), cmd]) addTestFilesWritten(name, fn) rm_no_fail(fn) else: - r = rawSystem([config.timeout_prog, str(timeout), cmd]) + r = rawSystemWithTimeout([config.timeout_prog, str(timeout), cmd]) else: r = os.system(cmd) return r << 8 diff --git a/testsuite/timeout/timeout.py b/testsuite/timeout/timeout.py index 76660a7390..6a57ac2f82 100644 --- a/testsuite/timeout/timeout.py +++ b/testsuite/timeout/timeout.py @@ -1,46 +1,53 @@ #!/usr/bin/env python -import errno -import os -import signal -import sys -import time +try: -secs = int(sys.argv[1]) -cmd = sys.argv[2] + import errno + import os + import signal + import sys + import time -def killProcess(pid): - os.killpg(pid, signal.SIGKILL) - for x in range(10): - try: - time.sleep(0.3) - r = os.waitpid(pid, os.WNOHANG) - if r == (0, 0): - os.killpg(pid, signal.SIGKILL) - else: - return - except OSError, e: - if e.errno == errno.ECHILD: - return - else: - raise e + secs = int(sys.argv[1]) + cmd = sys.argv[2] -pid = os.fork() -if pid == 0: - # child - os.setpgrp() - os.execvp('/bin/sh', ['/bin/sh', '-c', cmd]) -else: - # parent - def handler(signum, frame): - sys.stderr.write('Timeout happened...killing process...\n') - killProcess(pid) - sys.exit(99) - old = signal.signal(signal.SIGALRM, handler) - signal.alarm(secs) - (pid2, res) = os.waitpid(pid, 0) - if (os.WIFEXITED(res)): - sys.exit(os.WEXITSTATUS(res)) + def killProcess(pid): + os.killpg(pid, signal.SIGKILL) + for x in range(10): + try: + time.sleep(0.3) + r = os.waitpid(pid, os.WNOHANG) + if r == (0, 0): + os.killpg(pid, signal.SIGKILL) + else: + return + except OSError, e: + if e.errno == errno.ECHILD: + return + else: + raise e + + pid = os.fork() + if pid == 0: + # child + os.setpgrp() + os.execvp('/bin/sh', ['/bin/sh', '-c', cmd]) else: - sys.exit(res) + # parent + def handler(signum, frame): + sys.stderr.write('Timeout happened...killing process...\n') + killProcess(pid) + sys.exit(99) + old = signal.signal(signal.SIGALRM, handler) + signal.alarm(secs) + (pid2, res) = os.waitpid(pid, 0) + if (os.WIFEXITED(res)): + sys.exit(os.WEXITSTATUS(res)) + else: + sys.exit(res) + +except KeyboardInterrupt: + sys.exit(98) +except: + raise |