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 | |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 4 | import _imp as imp |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5 | import os |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 6 | import signal |
| 7 | import sys |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | import time |
Zachary Ware | ac28b79 | 2015-12-04 23:32:23 -0600 | [diff] [blame] | 9 | import unittest |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | from test.fork_wait import ForkWait |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 12 | from test.support import (reap_children, get_attribute, |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 13 | import_module, verbose) |
| 14 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 15 | threading = import_module('threading') |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 16 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 17 | # Skip test if fork does not exist. |
| 18 | get_attribute(os, 'fork') |
| 19 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 20 | class ForkTest(ForkWait): |
| 21 | def wait_impl(self, cpid): |
Victor Stinner | 1e48eb3 | 2014-03-18 00:39:04 +0100 | [diff] [blame] | 22 | deadline = time.monotonic() + 10.0 |
| 23 | while time.monotonic() <= deadline: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 24 | # waitpid() shouldn't hang, but some of the buildbots seem to hang |
| 25 | # in the forking tests. This is an attempt to fix the problem. |
| 26 | spid, status = os.waitpid(cpid, os.WNOHANG) |
| 27 | if spid == cpid: |
| 28 | break |
Victor Stinner | 1e48eb3 | 2014-03-18 00:39:04 +0100 | [diff] [blame] | 29 | time.sleep(0.1) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 30 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 31 | self.assertEqual(spid, cpid) |
| 32 | 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] | 33 | |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 34 | def test_threaded_import_lock_fork(self): |
| 35 | """Check fork() in main thread works while a subthread is doing an import""" |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 36 | import_started = threading.Event() |
| 37 | fake_module_name = "fake test module" |
| 38 | partial_module = "partial" |
| 39 | complete_module = "complete" |
| 40 | def importer(): |
| 41 | imp.acquire_lock() |
| 42 | sys.modules[fake_module_name] = partial_module |
| 43 | import_started.set() |
| 44 | time.sleep(0.01) # Give the other thread time to try and acquire. |
| 45 | sys.modules[fake_module_name] = complete_module |
| 46 | imp.release_lock() |
| 47 | t = threading.Thread(target=importer) |
| 48 | t.start() |
| 49 | import_started.wait() |
| 50 | pid = os.fork() |
| 51 | try: |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 52 | # PyOS_BeforeFork should have waited for the import to complete |
| 53 | # before forking, so the child can recreate the import lock |
| 54 | # correctly, but also won't see a partially initialised module |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 55 | if not pid: |
| 56 | m = __import__(fake_module_name) |
| 57 | if m == complete_module: |
| 58 | os._exit(0) |
| 59 | else: |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 60 | if verbose > 1: |
| 61 | print("Child encountered partial module") |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 62 | os._exit(1) |
| 63 | else: |
| 64 | t.join() |
| 65 | # Exitcode 1 means the child got a partial module (bad.) No |
| 66 | # exitcode (but a hang, which manifests as 'got pid 0') |
| 67 | # means the child deadlocked (also bad.) |
| 68 | self.wait_impl(pid) |
| 69 | finally: |
| 70 | try: |
| 71 | os.kill(pid, signal.SIGKILL) |
| 72 | except OSError: |
| 73 | pass |
| 74 | |
Nick Coghlan | b2ddf79 | 2010-12-02 04:11:46 +0000 | [diff] [blame] | 75 | |
| 76 | def test_nested_import_lock_fork(self): |
| 77 | """Check fork() in main thread works while the main thread is doing an import""" |
| 78 | # Issue 9573: this used to trigger RuntimeError in the child process |
| 79 | def fork_with_import_lock(level): |
| 80 | release = 0 |
| 81 | in_child = False |
| 82 | try: |
| 83 | try: |
| 84 | for i in range(level): |
| 85 | imp.acquire_lock() |
| 86 | release += 1 |
| 87 | pid = os.fork() |
| 88 | in_child = not pid |
| 89 | finally: |
| 90 | for i in range(release): |
| 91 | imp.release_lock() |
| 92 | except RuntimeError: |
| 93 | if in_child: |
| 94 | if verbose > 1: |
| 95 | print("RuntimeError in child") |
| 96 | os._exit(1) |
| 97 | raise |
| 98 | if in_child: |
| 99 | os._exit(0) |
| 100 | self.wait_impl(pid) |
| 101 | |
| 102 | # Check this works with various levels of nested |
| 103 | # import in the main thread |
| 104 | for level in range(5): |
| 105 | fork_with_import_lock(level) |
| 106 | |
| 107 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 108 | def tearDownModule(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | reap_children() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 110 | |
| 111 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 112 | unittest.main() |