3
3
import textwrap
4
4
import importlib
5
5
import sys
6
- from test .support import os_helper , SHORT_TIMEOUT
6
+ from test .support import os_helper , SHORT_TIMEOUT , busy_retry
7
7
from test .support .script_helper import make_script
8
8
9
9
import subprocess
@@ -361,7 +361,7 @@ def test_async_global_awaited_by(self):
361
361
import random
362
362
import sys
363
363
from string import ascii_lowercase, digits
364
- from test.support import socket_helper
364
+ from test.support import socket_helper, SHORT_TIMEOUT
365
365
366
366
HOST = '127.0.0.1'
367
367
PORT = socket_helper.find_unused_port()
@@ -386,7 +386,7 @@ async def echo_client(message):
386
386
assert message == data.decode()
387
387
writer.close()
388
388
await writer.wait_closed()
389
- await asyncio.sleep(10 )
389
+ await asyncio.sleep(SHORT_TIMEOUT )
390
390
391
391
async def echo_client_spam(server):
392
392
async with asyncio.TaskGroup() as tg:
@@ -426,16 +426,34 @@ async def main():
426
426
with open (fifo , "r" ) as fifo_file :
427
427
response = fifo_file .read ()
428
428
self .assertEqual (response , "ready" )
429
- all_awaited_by = get_all_awaited_by (p .pid )
429
+ for _ in busy_retry (SHORT_TIMEOUT ):
430
+ try :
431
+ all_awaited_by = get_all_awaited_by (p .pid )
432
+ except RuntimeError as re :
433
+ # This call reads a linked list in another process with
434
+ # no synchronization. That occasionally leads to invalid
435
+ # reads. Here we avoid making the test flaky.
436
+ msg = str (re )
437
+ if msg .startswith ("Unknown error reading memory" ):
438
+ continue
439
+ elif msg .startswith ("Unhandled frame owner" ):
440
+ continue
441
+ raise # Unrecognized exception, safest not to ignore it
442
+ else :
443
+ break
430
444
# expected: a list of two elements: 1 thread, 1 interp
431
445
self .assertEqual (len (all_awaited_by ), 2 )
432
446
# expected: a tuple with the thread ID and the awaited_by list
433
447
self .assertEqual (len (all_awaited_by [0 ]), 2 )
434
448
entries = all_awaited_by [0 ][1 ]
449
+ # expected: at least 1000 pending tasks
435
450
self .assertGreaterEqual (len (entries ), 1000 )
451
+ # the first three tasks stem from the code structure
436
452
self .assertIn (('Task-1' , []), entries )
437
453
self .assertIn (('server task' , [[['main' ], 'Task-1' , []]]), entries )
438
454
self .assertIn (('echo client spam' , [[['main' ], 'Task-1' , []]]), entries )
455
+ # the final task will have some random number, but it should for
456
+ # sure be one of the echo client spam horde
439
457
self .assertEqual ([[['echo_client_spam' ], 'echo client spam' , [[['main' ], 'Task-1' , []]]]], entries [- 1 ][1 ])
440
458
except PermissionError :
441
459
self .skipTest (
0 commit comments