1717import socket
1818import random
1919import logging
20+ import subprocess
2021import struct
2122import operator
2223import pickle
@@ -3769,7 +3770,6 @@ def test_shared_memory_SharedMemoryServer_ignores_sigint(self):
37693770 def test_shared_memory_SharedMemoryManager_reuses_resource_tracker (self ):
37703771 # bpo-36867: test that a SharedMemoryManager uses the
37713772 # same resource_tracker process as its parent.
3772- import subprocess
37733773 cmd = '''if 1:
37743774 from multiprocessing.managers import SharedMemoryManager
37753775
@@ -3779,16 +3779,13 @@ def test_shared_memory_SharedMemoryManager_reuses_resource_tracker(self):
37793779 sl = smm.ShareableList(range(10))
37803780 smm.shutdown()
37813781 '''
3782- p = subprocess .Popen ([sys .executable , '-E' , '-c' , cmd ],
3783- stderr = subprocess .PIPE )
3784- p .wait ()
3782+ rc , out , err = test .support .script_helper .assert_python_ok ('-c' , cmd )
37853783
37863784 # Before bpo-36867 was fixed, a SharedMemoryManager not using the same
37873785 # resource_tracker process as its parent would make the parent's
37883786 # tracker complain about sl being leaked even though smm.shutdown()
37893787 # properly released sl.
3790- stderr = p .stderr .read ().strip ().decode ()
3791- self .assertNotRegex (stderr , "resource_tracker" )
3788+ self .assertFalse (err )
37923789
37933790 def test_shared_memory_SharedMemoryManager_basics (self ):
37943791 smm1 = multiprocessing .managers .SharedMemoryManager ()
@@ -3929,8 +3926,6 @@ def test_shared_memory_ShareableList_pickling(self):
39293926 sl .shm .close ()
39303927
39313928 def test_shared_memory_cleaned_after_process_termination (self ):
3932- import subprocess
3933- from multiprocessing import shared_memory
39343929 cmd = '''if 1:
39353930 import os, time, sys
39363931 from multiprocessing import shared_memory
@@ -3941,18 +3936,24 @@ def test_shared_memory_cleaned_after_process_termination(self):
39413936 sys.stdout.flush()
39423937 time.sleep(100)
39433938 '''
3944- p = subprocess .Popen ([sys .executable , '-E' , '-c' , cmd ],
3945- stdout = subprocess .PIPE )
3946- name = p .stdout .readline ().strip ().decode ()
3939+ with subprocess .Popen ([sys .executable , '-E' , '-c' , cmd ],
3940+ stdout = subprocess .PIPE ,
3941+ stderr = subprocess .PIPE ) as p :
3942+ name = p .stdout .readline ().strip ().decode ()
39473943
3948- # killing abruptly processes holding reference to a shared memory
3949- # segment should not leak the given memory segment.
3950- p .terminate ()
3951- p .wait ()
3952- time .sleep (1.0 ) # wait for the OS to collect the segment
3944+ # killing abruptly processes holding reference to a shared memory
3945+ # segment should not leak the given memory segment.
3946+ p .terminate ()
3947+ p .wait ()
3948+ time .sleep (1.0 ) # wait for the OS to collect the segment
39533949
3954- with self .assertRaises (FileNotFoundError ):
3955- smm = shared_memory .SharedMemory (name , create = False )
3950+ # The shared memory file was deleted.
3951+ with self .assertRaises (FileNotFoundError ):
3952+ smm = shared_memory .SharedMemory (name , create = False )
3953+ # A warning was emitted by the subprocess' own resource_tracker.
3954+ err = p .stderr .read ().decode ()
3955+ self .assertIn ("resource_tracker: There appear to be 1 leaked "
3956+ "shared_memory objects to clean up at shutdown" , err )
39563957
39573958#
39583959#
@@ -4585,7 +4586,7 @@ def run_in_child(cls):
45854586 print (json .dumps (flags ))
45864587
45874588 def test_flags (self ):
4588- import json , subprocess
4589+ import json
45894590 # start child process using unusual flags
45904591 prog = ('from test._test_multiprocessing import TestFlags; ' +
45914592 'TestFlags.run_in_child()' )
@@ -4891,7 +4892,6 @@ def test_resource_tracker(self):
48914892 #
48924893 # Check that killing process does not leak named semaphores
48934894 #
4894- import subprocess
48954895 cmd = '''if 1:
48964896 import time, os, tempfile
48974897 import multiprocessing as mp
0 commit comments