|
| 1 | +import asyncio |
| 2 | +import ctypes.util |
| 3 | +import logging |
| 4 | +from concurrent.futures import ThreadPoolExecutor |
| 5 | +from threading import Thread |
| 6 | +from unittest import TestCase |
| 7 | + |
| 8 | +import uvloop |
| 9 | + |
| 10 | + |
| 11 | +class ProcessSpawningTestCollection(TestCase): |
| 12 | + |
| 13 | + def test_spawning_external_process(self): |
| 14 | + """Test spawning external process (using `popen` system call) that |
| 15 | + cause loop freeze.""" |
| 16 | + |
| 17 | + async def run(loop): |
| 18 | + event = asyncio.Event(loop=loop) |
| 19 | + |
| 20 | + dummy_workers = [simulate_loop_activity(loop, event) |
| 21 | + for _ in range(5)] |
| 22 | + spawn_worker = spawn_external_process(loop, event) |
| 23 | + done, pending = await asyncio.wait([spawn_worker] + dummy_workers, |
| 24 | + loop=loop) |
| 25 | + exceptions = [result.exception() |
| 26 | + for result in done if result.exception()] |
| 27 | + if exceptions: |
| 28 | + raise exceptions[0] |
| 29 | + |
| 30 | + return True |
| 31 | + |
| 32 | + async def simulate_loop_activity(loop, done_event): |
| 33 | + """Simulate loop activity by busy waiting for event.""" |
| 34 | + while True: |
| 35 | + try: |
| 36 | + await asyncio.wait_for(done_event.wait(), |
| 37 | + timeout=0.1, loop=loop) |
| 38 | + except asyncio.TimeoutError: |
| 39 | + pass |
| 40 | + |
| 41 | + if done_event.is_set(): |
| 42 | + return None |
| 43 | + |
| 44 | + async def spawn_external_process(loop, event): |
| 45 | + executor = ThreadPoolExecutor() |
| 46 | + try: |
| 47 | + call = loop.run_in_executor(executor, spawn_process) |
| 48 | + await asyncio.wait_for(call, loop=loop, timeout=3600) |
| 49 | + finally: |
| 50 | + event.set() |
| 51 | + executor.shutdown(wait=False) |
| 52 | + return True |
| 53 | + |
| 54 | + BUFFER_LENGTH = 1025 |
| 55 | + BufferType = ctypes.c_char * (BUFFER_LENGTH - 1) |
| 56 | + |
| 57 | + def run_echo(popen, fread, pclose): |
| 58 | + fd = popen('echo test'.encode('ASCII'), 'r'.encode('ASCII')) |
| 59 | + try: |
| 60 | + while True: |
| 61 | + buffer = BufferType() |
| 62 | + data = ctypes.c_void_p(ctypes.addressof(buffer)) |
| 63 | + |
| 64 | + # -> this call will freeze whole loop in case of bug |
| 65 | + read = fread(data, 1, BUFFER_LENGTH, fd) |
| 66 | + if not read: |
| 67 | + break |
| 68 | + except Exception: |
| 69 | + logging.getLogger().exception('read error') |
| 70 | + raise |
| 71 | + finally: |
| 72 | + pclose(fd) |
| 73 | + |
| 74 | + def spawn_process(): |
| 75 | + """Spawn external process via `popen` system call.""" |
| 76 | + |
| 77 | + stdio = ctypes.CDLL(ctypes.util.find_library('c')) |
| 78 | + |
| 79 | + # popen system call |
| 80 | + popen = stdio.popen |
| 81 | + popen.argtypes = (ctypes.c_char_p, ctypes.c_char_p) |
| 82 | + popen.restype = ctypes.c_void_p |
| 83 | + |
| 84 | + # pclose system call |
| 85 | + pclose = stdio.pclose |
| 86 | + pclose.argtypes = (ctypes.c_void_p,) |
| 87 | + pclose.restype = ctypes.c_int |
| 88 | + |
| 89 | + # fread system call |
| 90 | + fread = stdio.fread |
| 91 | + fread.argtypes = (ctypes.c_void_p, ctypes.c_size_t, |
| 92 | + ctypes.c_size_t, ctypes.c_void_p) |
| 93 | + fread.restype = ctypes.c_size_t |
| 94 | + |
| 95 | + for iteration in range(1000): |
| 96 | + t = Thread(target=run_echo, |
| 97 | + args=(popen, fread, pclose), |
| 98 | + daemon=True) |
| 99 | + t.start() |
| 100 | + t.join(timeout=10.0) |
| 101 | + if t.is_alive(): |
| 102 | + raise Exception('process freeze detected at {}' |
| 103 | + .format(iteration)) |
| 104 | + |
| 105 | + return True |
| 106 | + |
| 107 | + loop = uvloop.new_event_loop() |
| 108 | + proc = loop.run_until_complete(run(loop)) |
| 109 | + self.assertTrue(proc) |
0 commit comments