Skip to content

Commit

Permalink
fix: race in test_tcp_connection_with_forwarding (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 7, 2025
1 parent 1f7f52d commit 4116261
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tests/test_tcp_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 4116261

Please sign in to comment.