From 41162618d4a78c193d91fb9525eb7d2763f17587 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Jan 2025 12:25:40 -1000 Subject: [PATCH] fix: race in test_tcp_connection_with_forwarding (#350) --- tests/test_tcp_address.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_tcp_address.py b/tests/test_tcp_address.py index 5c20f09c..711e9d5b 100644 --- a/tests/test_tcp_address.py +++ b/tests/test_tcp_address.py @@ -2,7 +2,7 @@ import os import pytest - +from contextlib import suppress from dbus_fast import Message from dbus_fast._private.address import parse_address from dbus_fast.aio import MessageBus @@ -24,6 +24,8 @@ async def test_tcp_connection_with_forwarding(event_loop): else: path = addr_zero_options["path"] + tasks: list[asyncio.Task] = [] + async def handle_connection(tcp_reader, tcp_writer): unix_reader, unix_writer = await asyncio.open_unix_connection(path) closables.append(tcp_writer) @@ -43,8 +45,8 @@ async def handle_write(): break tcp_writer.write(data) - asyncio.run_coroutine_threadsafe(handle_read(), event_loop) - asyncio.run_coroutine_threadsafe(handle_write(), event_loop) + tasks.append(asyncio.create_task(handle_read())) + tasks.append(asyncio.create_task(handle_write())) server = await asyncio.start_server(handle_connection, host, port) closables.append(server) @@ -74,3 +76,8 @@ async def handle_write(): for c in closables: c.close() + + for t in tasks: + t.cancel() + with suppress(asyncio.CancelledError): + await t