1515from unittest import mock
1616
1717from test import support
18- from test .support import os_helper
18+ from test .support import os_helper , warnings_helper
1919from test .support import socket_helper
2020from test .support import wait_process
2121from test .support import hashlib_helper
@@ -1180,67 +1180,63 @@ async def runner():
11801180
11811181
11821182@support .requires_fork ()
1183- class TestFork (unittest .TestCase ):
1184-
1185- def test_fork_not_share_current_task (self ):
1186- loop = object ()
1187- task = object ()
1188- asyncio ._set_running_loop (loop )
1189- self .addCleanup (asyncio ._set_running_loop , None )
1190- asyncio .tasks ._enter_task (loop , task )
1191- self .addCleanup (asyncio .tasks ._leave_task , loop , task )
1192- self .assertIs (asyncio .current_task (), task )
1193- r , w = os .pipe ()
1194- self .addCleanup (os .close , r )
1195- self .addCleanup (os .close , w )
1196- pid = os .fork ()
1197- if pid == 0 :
1198- # child
1199- try :
1200- asyncio ._set_running_loop (loop )
1201- current_task = asyncio .current_task ()
1202- if current_task is None :
1203- os .write (w , b'NO TASK' )
1204- else :
1205- os .write (w , b'TASK:' + str (id (current_task )).encode ())
1206- except BaseException as e :
1207- os .write (w , b'ERROR:' + ascii (e ).encode ())
1208- finally :
1209- asyncio ._set_running_loop (None )
1210- os ._exit (0 )
1211- else :
1212- # parent
1213- result = os .read (r , 100 )
1214- self .assertEqual (result , b'NO TASK' )
1215- wait_process (pid , exitcode = 0 )
1216-
1217- def test_fork_not_share_event_loop (self ):
1218- # The forked process should not share the event loop with the parent
1219- loop = object ()
1220- asyncio ._set_running_loop (loop )
1221- self .assertIs (asyncio .get_running_loop (), loop )
1222- self .addCleanup (asyncio ._set_running_loop , None )
1223- r , w = os .pipe ()
1224- self .addCleanup (os .close , r )
1225- self .addCleanup (os .close , w )
1226- pid = os .fork ()
1227- if pid == 0 :
1228- # child
1229- try :
1230- loop = asyncio .get_event_loop ()
1231- os .write (w , b'LOOP:' + str (id (loop )).encode ())
1232- except RuntimeError :
1233- os .write (w , b'NO LOOP' )
1234- except BaseException as e :
1235- os .write (w , b'ERROR:' + ascii (e ).encode ())
1236- finally :
1237- os ._exit (0 )
1238- else :
1239- # parent
1240- result = os .read (r , 100 )
1241- self .assertEqual (result , b'NO LOOP' )
1242- wait_process (pid , exitcode = 0 )
1183+ class TestFork (unittest .IsolatedAsyncioTestCase ):
12431184
1185+ async def test_fork_not_share_current_task (self ):
1186+ with warnings_helper .ignore_fork_in_thread_deprecation_warnings ():
1187+ loop = asyncio .get_running_loop ()
1188+ task = asyncio .current_task ()
1189+ self .assertIsNotNone (task )
1190+ r , w = os .pipe ()
1191+ self .addCleanup (os .close , r )
1192+ self .addCleanup (os .close , w )
1193+ pid = os .fork ()
1194+ if pid == 0 :
1195+ # child
1196+ try :
1197+ asyncio ._set_running_loop (loop )
1198+ current_task = asyncio .current_task ()
1199+ if current_task is None :
1200+ os .write (w , b'NO TASK' )
1201+ else :
1202+ os .write (w , b'TASK:' + str (id (current_task )).encode ())
1203+ except BaseException as e :
1204+ os .write (w , b'ERROR:' + ascii (e ).encode ())
1205+ finally :
1206+ asyncio ._set_running_loop (None )
1207+ os ._exit (0 )
1208+ else :
1209+ # parent
1210+ result = os .read (r , 100 )
1211+ self .assertEqual (result , b'NO TASK' )
1212+ wait_process (pid , exitcode = 0 )
1213+
1214+ async def test_fork_not_share_event_loop (self ):
1215+ with warnings_helper .ignore_fork_in_thread_deprecation_warnings ():
1216+ # The forked process should not share the event loop with the parent
1217+ loop = asyncio .get_running_loop ()
1218+ r , w = os .pipe ()
1219+ self .addCleanup (os .close , r )
1220+ self .addCleanup (os .close , w )
1221+ pid = os .fork ()
1222+ if pid == 0 :
1223+ # child
1224+ try :
1225+ loop = asyncio .get_event_loop ()
1226+ os .write (w , b'LOOP:' + str (id (loop )).encode ())
1227+ except RuntimeError :
1228+ os .write (w , b'NO LOOP' )
1229+ except BaseException as e :
1230+ os .write (w , b'ERROR:' + ascii (e ).encode ())
1231+ finally :
1232+ os ._exit (0 )
1233+ else :
1234+ # parent
1235+ result = os .read (r , 100 )
1236+ self .assertEqual (result , b'NO LOOP' )
1237+ wait_process (pid , exitcode = 0 )
1238+
1239+ @warnings_helper .ignore_fork_in_thread_deprecation_warnings ()
12441240 @hashlib_helper .requires_hashdigest ('md5' )
12451241 @support .skip_if_sanitizer ("TSAN doesn't support threads after fork" , thread = True )
12461242 def test_fork_signal_handling (self ):
@@ -1288,6 +1284,7 @@ async def func():
12881284 self .assertFalse (parent_handled .is_set ())
12891285 self .assertTrue (child_handled .is_set ())
12901286
1287+ @warnings_helper .ignore_fork_in_thread_deprecation_warnings ()
12911288 @hashlib_helper .requires_hashdigest ('md5' )
12921289 @support .skip_if_sanitizer ("TSAN doesn't support threads after fork" , thread = True )
12931290 def test_fork_asyncio_run (self ):
@@ -1308,6 +1305,7 @@ async def child_main():
13081305
13091306 self .assertEqual (result .value , 42 )
13101307
1308+ @warnings_helper .ignore_fork_in_thread_deprecation_warnings ()
13111309 @hashlib_helper .requires_hashdigest ('md5' )
13121310 @support .skip_if_sanitizer ("TSAN doesn't support threads after fork" , thread = True )
13131311 def test_fork_asyncio_subprocess (self ):
0 commit comments