annotate Lib/test/test_fork1.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 09ed411a1090
children 1b8f08c4efd5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11855
79ea19085fb6 Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """This test checks for correct fork() behavior.
79ea19085fb6 Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 """
79ea19085fb6 Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
4 import imp
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
5 import os
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
6 import signal
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
7 import sys
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
8 import time
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
9
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
10 from test.fork_wait import ForkWait
66599
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
11 from test.support import (run_unittest, reap_children, get_attribute,
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
12 import_module, verbose)
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
13
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59615
diff changeset
14 threading = import_module('threading')
11855
79ea19085fb6 Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
52625
f84340886936 Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
R. David Murray <rdmurray@bitdance.com>
parents: 52433
diff changeset
16 # Skip test if fork does not exist.
f84340886936 Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
R. David Murray <rdmurray@bitdance.com>
parents: 52433
diff changeset
17 get_attribute(os, 'fork')
f84340886936 Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
R. David Murray <rdmurray@bitdance.com>
parents: 52433
diff changeset
18
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
19 class ForkTest(ForkWait):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
20 def wait_impl(self, cpid):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
21 for i in range(10):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
22 # waitpid() shouldn't hang, but some of the buildbots seem to hang
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
23 # in the forking tests. This is an attempt to fix the problem.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
24 spid, status = os.waitpid(cpid, os.WNOHANG)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
25 if spid == cpid:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
26 break
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
27 time.sleep(1.0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
28
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
29 self.assertEqual(spid, cpid)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
30 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
12332
6b4667fa4938 Use a constant to specify the number of child threads to create.
Fred Drake <fdrake@acm.org>
parents: 11855
diff changeset
31
66599
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
32 def test_threaded_import_lock_fork(self):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
33 """Check fork() in main thread works while a subthread is doing an import"""
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
34 import_started = threading.Event()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
35 fake_module_name = "fake test module"
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
36 partial_module = "partial"
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
37 complete_module = "complete"
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
38 def importer():
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
39 imp.acquire_lock()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
40 sys.modules[fake_module_name] = partial_module
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
41 import_started.set()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
42 time.sleep(0.01) # Give the other thread time to try and acquire.
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
43 sys.modules[fake_module_name] = complete_module
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
44 imp.release_lock()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
45 t = threading.Thread(target=importer)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
46 t.start()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
47 import_started.wait()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
48 pid = os.fork()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
49 try:
66599
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
50 # PyOS_BeforeFork should have waited for the import to complete
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
51 # before forking, so the child can recreate the import lock
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
52 # correctly, but also won't see a partially initialised module
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
53 if not pid:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
54 m = __import__(fake_module_name)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
55 if m == complete_module:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
56 os._exit(0)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
57 else:
66599
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
58 if verbose > 1:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
59 print("Child encountered partial module")
56382
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
60 os._exit(1)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
61 else:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
62 t.join()
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
63 # Exitcode 1 means the child got a partial module (bad.) No
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
64 # exitcode (but a hang, which manifests as 'got pid 0')
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
65 # means the child deadlocked (also bad.)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
66 self.wait_impl(pid)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
67 finally:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
68 try:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
69 os.kill(pid, signal.SIGKILL)
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
70 except OSError:
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
71 pass
2e4d7a40c2dd Merged revisions 75246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52625
diff changeset
72
66599
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
73
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
74 def test_nested_import_lock_fork(self):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
75 """Check fork() in main thread works while the main thread is doing an import"""
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
76 # Issue 9573: this used to trigger RuntimeError in the child process
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
77 def fork_with_import_lock(level):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
78 release = 0
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
79 in_child = False
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
80 try:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
81 try:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
82 for i in range(level):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
83 imp.acquire_lock()
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
84 release += 1
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
85 pid = os.fork()
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
86 in_child = not pid
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
87 finally:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
88 for i in range(release):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
89 imp.release_lock()
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
90 except RuntimeError:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
91 if in_child:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
92 if verbose > 1:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
93 print("RuntimeError in child")
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
94 os._exit(1)
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
95 raise
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
96 if in_child:
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
97 os._exit(0)
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
98 self.wait_impl(pid)
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
99
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
100 # Check this works with various levels of nested
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
101 # import in the main thread
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
102 for level in range(5):
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
103 fork_with_import_lock(level)
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
104
09ed411a1090 Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61073
diff changeset
105
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
106 def test_main():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
107 run_unittest(ForkTest)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
108 reap_children()
11855
79ea19085fb6 Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
110 if __name__ == "__main__":
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 24366
diff changeset
111 test_main()