55import  sys 
66import  socket 
77from  unittest .mock  import  ANY 
8- from  test .support  import  os_helper , SHORT_TIMEOUT , busy_retry ,  requires_gil_enabled 
8+ from  test .support  import  os_helper , SHORT_TIMEOUT , busy_retry 
99from  test .support .script_helper  import  make_script 
1010from  test .support .socket_helper  import  find_unused_port 
1111
@@ -406,7 +406,6 @@ async def main():
406406            self .assertEqual (stack_trace , expected_stack_trace )
407407
408408    @skip_if_not_supported  
409-     @requires_gil_enabled ("gh-133359: occasionally flaky on AMD64" ) 
410409    @unittest .skipIf (sys .platform  ==  "linux"  and  not  PROCESS_VM_READV_SUPPORTED , 
411410                     "Test only runs on Linux with process_vm_readv support" ) 
412411    def  test_async_global_awaited_by (self ):
@@ -447,6 +446,8 @@ async def echo_client(message):
447446                assert message == data.decode() 
448447                writer.close() 
449448                await writer.wait_closed() 
449+                 # Signal we are ready to sleep 
450+                 sock.sendall(b"ready") 
450451                await asyncio.sleep(SHORT_TIMEOUT) 
451452
452453            async def echo_client_spam(server): 
@@ -456,8 +457,10 @@ async def echo_client_spam(server):
456457                        random.shuffle(msg) 
457458                        tg.create_task(echo_client("".join(msg))) 
458459                        await asyncio.sleep(0) 
459-                     # at least a 1000 tasks created 
460-                     sock.sendall(b"ready") 
460+                     # at least a 1000 tasks created. Each task will signal 
461+                     # when is ready to avoid the race caused by the fact that 
462+                     # tasks are waited on tg.__exit__ and we cannot signal when 
463+                     # that happens otherwise 
461464                # at this point all client tasks completed without assertion errors 
462465                # let's wrap up the test 
463466                server.close() 
@@ -489,8 +492,10 @@ async def main():
489492                p  =  subprocess .Popen ([sys .executable , script_name ])
490493                client_socket , _  =  server_socket .accept ()
491494                server_socket .close ()
492-                 response  =  client_socket .recv (1024 )
493-                 self .assertEqual (response , b"ready" )
495+                 for  _  in  range (1000 ):
496+                     expected_response  =  b"ready" 
497+                     response  =  client_socket .recv (len (expected_response ))
498+                     self .assertEqual (response , expected_response )
494499                for  _  in  busy_retry (SHORT_TIMEOUT ):
495500                    try :
496501                        all_awaited_by  =  get_all_awaited_by (p .pid )
@@ -527,12 +532,7 @@ async def main():
527532                expected_stack  =  [[['_aexit' , '__aexit__' , 'echo_client_spam' ], ANY ]]
528533                tasks_with_stack  =  [task  for  task  in  entries  if  task [2 ] ==  expected_stack ]
529534                self .assertGreaterEqual (len (tasks_with_stack ), 1000 )
530- 
531-                 # the final task will have some random number, but it should for 
532-                 # sure be one of the echo client spam horde (In windows this is not true 
533-                 # for some reason) 
534-                 if  sys .platform  !=  "win32" :
535-                     self .assertEqual ([[['_aexit' , '__aexit__' , 'echo_client_spam' ], ANY ]], entries [- 1 ][2 ])
535+                 self .assertEqual ([[['_aexit' , '__aexit__' , 'echo_client_spam' ], ANY ]], entries [- 1 ][2 ])
536536            except  PermissionError :
537537                self .skipTest (
538538                    "Insufficient permissions to read the stack trace" )
0 commit comments