@@ -133,6 +133,7 @@ def task():
133
133
del task
134
134
while not done :
135
135
time .sleep (POLL_SLEEP )
136
+ support .gc_collect () # For PyPy or other GCs.
136
137
self .assertEqual (thread ._count (), orig )
137
138
138
139
def test_unraisable_exception (self ):
@@ -227,30 +228,31 @@ def setUp(self):
227
228
@unittest .skipUnless (hasattr (os , 'fork' ), 'need os.fork' )
228
229
@threading_helper .reap_threads
229
230
def test_forkinthread (self ):
230
- status = "not set"
231
+ pid = None
231
232
232
- def thread1 ( ):
233
- nonlocal status
233
+ def fork_thread ( read_fd , write_fd ):
234
+ nonlocal pid
234
235
235
236
# fork in a thread
236
237
pid = os .fork ()
237
- if pid == 0 :
238
- # child
239
- try :
240
- os .close (self .read_fd )
241
- os .write (self .write_fd , b"OK" )
242
- finally :
243
- os ._exit (0 )
244
- else :
245
- # parent
246
- os .close (self .write_fd )
247
- pid , status = os .waitpid (pid , 0 )
238
+ if pid :
239
+ # parent process
240
+ return
241
+
242
+ # child process
243
+ try :
244
+ os .close (read_fd )
245
+ os .write (write_fd , b"OK" )
246
+ finally :
247
+ os ._exit (0 )
248
248
249
249
with threading_helper .wait_threads_exit ():
250
- thread .start_new_thread (thread1 , ())
251
- self .assertEqual (os .read (self .read_fd , 2 ), b"OK" ,
252
- "Unable to fork() in thread" )
253
- self .assertEqual (status , 0 )
250
+ thread .start_new_thread (fork_thread , (self .read_fd , self .write_fd ))
251
+ self .assertEqual (os .read (self .read_fd , 2 ), b"OK" )
252
+ os .close (self .write_fd )
253
+
254
+ self .assertIsNotNone (pid )
255
+ support .wait_process (pid , exitcode = 0 )
254
256
255
257
def tearDown (self ):
256
258
try :
0 commit comments