Skip to content

WebSocketServer race condition (test_websocket.py) #678

@tollsimy

Description

@tollsimy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions