|
5 | 5 | sync, multiproc, and async test files. |
6 | 6 | """ |
7 | 7 |
|
| 8 | +import asyncio |
| 9 | +import logging |
| 10 | +import threading |
| 11 | + |
| 12 | +import grpc |
| 13 | + |
| 14 | +_logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def start_async_server(loop, start_server_coro): |
| 18 | + """Start an async gRPC server on the given event loop and wait until it is ready. |
| 19 | +
|
| 20 | + Args: |
| 21 | + loop: The asyncio event loop running in a background thread. |
| 22 | + start_server_coro: An awaitable that starts the server and returns |
| 23 | + a tuple of (grpc.aio.Server, sock_path). |
| 24 | +
|
| 25 | + Returns: |
| 26 | + The grpc.aio.Server instance. |
| 27 | + """ |
| 28 | + future = asyncio.run_coroutine_threadsafe(start_server_coro, loop=loop) |
| 29 | + server, sock_path = future.result(timeout=10) |
| 30 | + |
| 31 | + # Block until the server is accepting connections |
| 32 | + while True: |
| 33 | + try: |
| 34 | + with grpc.insecure_channel(sock_path) as channel: |
| 35 | + f = grpc.channel_ready_future(channel) |
| 36 | + f.result(timeout=10) |
| 37 | + if f.done(): |
| 38 | + break |
| 39 | + except grpc.FutureTimeoutError as e: |
| 40 | + _logger.error("error trying to connect to grpc server") |
| 41 | + _logger.error(e) |
| 42 | + |
| 43 | + return server |
| 44 | + |
| 45 | + |
| 46 | +def create_async_loop(): |
| 47 | + """Create a new asyncio event loop running in a daemon thread. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + The running event loop. |
| 51 | + """ |
| 52 | + loop = asyncio.new_event_loop() |
| 53 | + |
| 54 | + def _run(lp): |
| 55 | + asyncio.set_event_loop(lp) |
| 56 | + lp.run_forever() |
| 57 | + |
| 58 | + thread = threading.Thread(target=_run, args=(loop,), daemon=True) |
| 59 | + thread.start() |
| 60 | + return loop |
| 61 | + |
| 62 | + |
| 63 | +def teardown_async_server(loop, server): |
| 64 | + """Gracefully shut down an async gRPC server and its event loop. |
| 65 | +
|
| 66 | + Stops the gRPC server, cancels any remaining tasks, then stops the loop. |
| 67 | + This prevents 'Task was destroyed but it is pending!' warnings. |
| 68 | + """ |
| 69 | + |
| 70 | + async def _shutdown(): |
| 71 | + await server.stop(grace=1) |
| 72 | + # Cancel any lingering tasks on this loop, excluding the current |
| 73 | + # _shutdown task itself to avoid recursive cancel chains. |
| 74 | + current = asyncio.current_task() |
| 75 | + tasks = [t for t in asyncio.all_tasks(loop) if not t.done() and t is not current] |
| 76 | + for task in tasks: |
| 77 | + task.cancel() |
| 78 | + # Await each cancelled task individually so a RecursionError in one |
| 79 | + # deeply-nested cancel chain does not prevent the others from being |
| 80 | + # reaped, and does not propagate up to the caller. |
| 81 | + for task in tasks: |
| 82 | + try: |
| 83 | + await task |
| 84 | + except (asyncio.CancelledError, RecursionError, Exception): |
| 85 | + pass |
| 86 | + |
| 87 | + try: |
| 88 | + future = asyncio.run_coroutine_threadsafe(_shutdown(), loop=loop) |
| 89 | + future.result(timeout=10) |
| 90 | + except Exception as e: |
| 91 | + _logger.error("error during async server teardown: %s", e) |
| 92 | + finally: |
| 93 | + loop.call_soon_threadsafe(loop.stop) |
| 94 | + |
8 | 95 |
|
9 | 96 | def collect_responses(method): |
10 | 97 | """Collect all responses from a grpc_testing stream method until exhausted. |
|
0 commit comments