Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 1 | """This test checks for correct fork() behavior. |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 2 | """ |
| 3 | |
Thomas Wouters | c4dcb38 | 2009-09-16 19:55:54 +0000 | [diff] [blame^] | 4 | import errno |
| 5 | import imp |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 6 | import os |
Thomas Wouters | c4dcb38 | 2009-09-16 19:55:54 +0000 | [diff] [blame^] | 7 | import signal |
| 8 | import sys |
Neal Norwitz | 84bc19a | 2006-07-07 06:03:15 +0000 | [diff] [blame] | 9 | import time |
Thomas Wouters | c4dcb38 | 2009-09-16 19:55:54 +0000 | [diff] [blame^] | 10 | import threading |
| 11 | |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 12 | from test.fork_wait import ForkWait |
R. David Murray | 3db8a34 | 2009-03-30 23:05:48 +0000 | [diff] [blame] | 13 | from test.test_support import run_unittest, reap_children, get_attribute |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 14 | |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 15 | #Skip test if fork does not exist. |
R. David Murray | 3db8a34 | 2009-03-30 23:05:48 +0000 | [diff] [blame] | 16 | get_attribute(os, 'fork') |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 4951782 | 2000-05-04 00:36:42 +0000 | [diff] [blame] | 18 | |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 19 | class ForkTest(ForkWait): |
| 20 | def wait_impl(self, cpid): |
Neal Norwitz | 84bc19a | 2006-07-07 06:03:15 +0000 | [diff] [blame] | 21 | for i in range(10): |
| 22 | # waitpid() shouldn't hang, but some of the buildbots seem to hang |
| 23 | # in the forking tests. This is an attempt to fix the problem. |
| 24 | spid, status = os.waitpid(cpid, os.WNOHANG) |
| 25 | if spid == cpid: |
| 26 | break |
| 27 | time.sleep(1.0) |
| 28 | |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 29 | self.assertEqual(spid, cpid) |
| 30 | self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | c4dcb38 | 2009-09-16 19:55:54 +0000 | [diff] [blame^] | 32 | def test_import_lock_fork(self): |
| 33 | import_started = threading.Event() |
| 34 | fake_module_name = "fake test module" |
| 35 | partial_module = "partial" |
| 36 | complete_module = "complete" |
| 37 | def importer(): |
| 38 | imp.acquire_lock() |
| 39 | sys.modules[fake_module_name] = partial_module |
| 40 | import_started.set() |
| 41 | time.sleep(0.01) # Give the other thread time to try and acquire. |
| 42 | sys.modules[fake_module_name] = complete_module |
| 43 | imp.release_lock() |
| 44 | t = threading.Thread(target=importer) |
| 45 | t.start() |
| 46 | import_started.wait() |
| 47 | pid = os.fork() |
| 48 | try: |
| 49 | if not pid: |
| 50 | m = __import__(fake_module_name) |
| 51 | if m == complete_module: |
| 52 | os._exit(0) |
| 53 | else: |
| 54 | os._exit(1) |
| 55 | else: |
| 56 | t.join() |
| 57 | # Exitcode 1 means the child got a partial module (bad.) No |
| 58 | # exitcode (but a hang, which manifests as 'got pid 0') |
| 59 | # means the child deadlocked (also bad.) |
| 60 | self.wait_impl(pid) |
| 61 | finally: |
| 62 | try: |
| 63 | os.kill(pid, signal.SIGKILL) |
| 64 | except OSError: |
| 65 | pass |
| 66 | |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 67 | def test_main(): |
| 68 | run_unittest(ForkTest) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 69 | reap_children() |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 70 | |
| 71 | if __name__ == "__main__": |
| 72 | test_main() |