Debian Codegraph @

pypy/module/thread/test/test_fork.py

raw symbols SCIP
1 from pypy.module.thread.test.support import GenericTestThread
2
3 class AppTestFork(GenericTestThread):
4 spaceconfig = dict(usemodules=GenericTestThread.spaceconfig['usemodules'] + ('imp',))
5
6 def test_fork_with_thread(self):
7 # XXX This test depends on a multicore machine, as busy_thread must
8 # aquire the GIL the instant that the main thread releases it.
9 # It will incorrectly pass if the GIL is not grabbed in time.
10 import _thread
11 import os
12 import time
13
14 if not hasattr(os, 'fork'):
15 skip("No fork on this platform")
16 if not self.runappdirect:
17 skip("Not reliable before translation")
18
19 def busy_thread():
20 print('sleep')
21 while run:
22 time.sleep(0)
23 done.append(None)
24
25 for i in range(150):
26 run = True
27 done = []
28 try:
29 print('sleep')
30 _thread.start_new(busy_thread, ())
31
32 pid = os.fork()
33 if pid == 0:
34 os._exit(0)
35 else:
36 self.timeout_killer(pid, 10)
37 exitcode = os.waitpid(pid, 0)[1]
38 assert exitcode == 0 # if 9, process was killed by timer!
39 finally:
40 run = False
41 self.waitfor(lambda: done)
42 assert done
43
44 def test_forked_can_thread(self):
45 "Checks that a forked interpreter can start a thread"
46 import _thread
47 import os
48
49 if not hasattr(os, 'fork'):
50 skip("No fork on this platform")
51
52 for i in range(10):
53 # pre-allocate some locks
54 _thread.start_new_thread(lambda: None, ())
55 print('sleep')
56
57 pid = os.fork()
58 if pid == 0:
59 _thread.start_new_thread(lambda: None, ())
60 os._exit(0)
61 else:
62 self.timeout_killer(pid, 10)
63 exitcode = os.waitpid(pid, 0)[1]
64 assert exitcode == 0 # if 9, process was killed by timer!
65
66 def test_forked_is_main_thread(self):
67 "Checks that a forked interpreter is the main thread"
68 import os, _thread, signal
69
70 if not hasattr(os, 'fork'):
71 skip("No fork on this platform")
72
73 def threadfunction():
74 pid = os.fork()
75 if pid == 0:
76 print('in child')
77 # signal() only works from the 'main' thread
78 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
79 os._exit(42)
80 else:
81 self.timeout_killer(pid, 10)
82 exitcode = os.waitpid(pid, 0)[1]
83 feedback.append(exitcode)
84
85 feedback = []
86 _thread.start_new_thread(threadfunction, ())
87 self.waitfor(lambda: feedback)
88 # if 0, an (unraisable) exception was raised from the forked thread.
89 # if 9, process was killed by timer.
90 # if 42<<8, os._exit(42) was correctly reached.
91 assert feedback == [42<<8]
92
93 def test_nested_import_lock_fork(self):
94 """Check fork() in main thread works while the main thread is doing an import"""
95 # Issue 9573: this used to trigger RuntimeError in the child process
96 import imp
97 import os
98 import time
99
100 if not hasattr(os, 'fork'):
101 skip("No fork on this platform")
102
103 def fork_with_import_lock(level):
104 release = 0
105 in_child = False
106 try:
107 try:
108 for i in range(level):
109 imp.acquire_lock()
110 release += 1
111 pid = os.fork()
112 in_child = not pid
113 finally:
114 for i in range(release):
115 imp.release_lock()
116 except RuntimeError:
117 if in_child:
118 if verbose > 1:
119 print("RuntimeError in child")
120 os._exit(1)
121 raise
122 if in_child:
123 os._exit(0)
124
125 for i in range(10):
126 spid, status = os.waitpid(pid, os.WNOHANG)
127 if spid == pid:
128 break
129 time.sleep(1.0)
130 assert spid == pid
131 assert status == 0
132
133 # Check this works with various levels of nested
134 # import in the main thread
135 for level in range(5):
136 fork_with_import_lock(level)