Skip to content

Commit

Permalink
tests: fix async/test_connect.py deadlock in Python 3.12
Browse files Browse the repository at this point in the history
There was a breaking change in `wait_closed()` in Python 3.12 that
caused some tests from test_connect to hang indefinitely. The reason for
that was the fact that `wait_closed()` was waiting for all handlers to
finish their execution, while the handler expected `stop_event` to be
set. `stop_event` was set only *after* `wait_closed()` would have
finished and returned the control, which caused a deadlock.

This commit fixes the deadlock by setting `stop_event` right before
calling for `conn.disconnect()`. This makes sense, because at that
moment handling more new requests is not needed as connection is going
to be closed.
The old call for `stop_event.set()` is kept for the unlikely case when
there was a connection error and the execution did not reach
`stop_event.set()` call in `try` block. If it was reached, this call
will be a noop.

[1]: python/cpython#104344

Signed-off-by: Mikhail Koviazin <[email protected]>
  • Loading branch information
mkmkme committed Jul 11, 2024
1 parent 7783e0b commit a480f64
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/test_asyncio/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ async def _handler(reader, writer):
await aserver.start_serving()
try:
await conn.connect()
stop_event.set()
await conn.disconnect()
except ConnectionError:
finished.set()
raise
finally:
stop_event.set()
stop_event.set() # Set stop_event in case of a connection error
aserver.close()
await aserver.wait_closed()
await finished.wait()
Expand Down

0 comments on commit a480f64

Please sign in to comment.