awslabs / awslabs/aws-crt-python
WebSocketServer race condition (test_websocket.py)
- Dominant language
- Python
- Stars
- 100
- Forks
- 58
- Avg merge
- 13h 51m
- Merged PRs (30d)
- 2
Description
### Describe the bug
`test_manage_read_window` test fails with `AttributeError: 'WebSocketServer' object has no attribute '_current_connection'` due to a race condition.
The `_current_connection` attribute is never initialized in `WebSocketServer.__init__()`, while `send_async()` method immediately tries to access `_current_connection` without waiting for a client to connect
There's a race between when the client connects and when the server's `_run_connection()` method sets `_current_connection`.
Failure log:
```
======================================================================
FAIL: test_manage_read_window (test.test_websocket.TestClient.test_manage_read_window)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/builddir/build/BUILD/python-awscrt-0.27.2-build/awscrt-0.27.2/test/test_websocket.py", line 544, in test_manage_read_window
server.send_async(msg)
~~~~~~~~~~~~~~~~~^^^^^
File "/builddir/build/BUILD/python-awscrt-0.27.2-build/awscrt-0.27.2/test/test_websocket.py", line 184, in send_async
while self._current_connection is None:
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WebSocketServer' object has no attribute '_current_connection'. Did you mean: '_run_connection'?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/builddir/build/BUILD/python-awscrt-0.27.2-build/awscrt-0.27.2/test/test_websocket.py", line 534, in test_manage_read_window
with WebSocketServer(self.host, self.port) as server:
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
File "/builddir/build/BUILD/python-awscrt-0.27.2-build/awscrt-0.27.2/test/test_websocket.py", line 145, in __exit__
assert (self._server_thread.is_alive() == False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
----------------------------------------------------------------------
```
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Expected Behavior
Test `test_manage_read_window` always pass.
### Current Behavior
Test `test_manage_read_window` can fail due to race condition.
### Reproduction Steps
The bug shows up or not depending on the timing between the main test thread and the asyncio server thread. The race condition becomes more likely under system load, in CI environments, on multi-core systems, or when there are network/scheduling delays that affect asyncio task execution order. The timing window is typically very small, making the failure intermittent and difficult to reproduce consistently.
### Possible Solution
Initialize `_current_connection = None` in constructor and wait for connection establishment in `send_async()`.
### Additional Information/Context
_No response_
### aws-crt-python version used
0.27.6
### Python version used
Python 3.14.0rc2
### Operating System and version
Fedora Rawhide
Contributor guide
Assessment
This issue has not been assessed yet.