Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proof of concept for deterministic close race #101

Merged
merged 2 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 0 additions & 110 deletions tests/issue_96.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,23 @@ async def handler(request):
await client.get_message()
assert client.closed
assert client.closed.code == 1009


async def test_close_race(nursery, autojump_clock):
"""server attempts close just as client disconnects (issue #96)"""

async def handler(request):
ws = await request.accept()
await ws.send_message('foo')
await ws._for_testing_peer_closed_connection.wait()
# with bug, this would raise ConnectionClosed from websocket internal task
await trio.aclose_forcefully(ws._stream)

server = await nursery.start(
partial(serve_websocket, handler, HOST, 0, ssl_context=None))

connection = await connect_websocket(nursery, HOST, server.port,
RESOURCE, use_ssl=False)
await connection.get_message()
await connection.aclose()
await trio.sleep(.1)
5 changes: 5 additions & 0 deletions trio_websocket/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ def __init__(self, stream, wsproto, *, path=None,
# Set once a WebSocket closed handshake takes place, i.e after a close
# frame has been sent and a close frame has been received.
self._close_handshake = trio.Event()
# Set immediately upon receiving closed event from peer. Used to
# test close race conditions between client and server.
self._for_testing_peer_closed_connection = trio.Event()

@property
def closed(self):
Expand Down Expand Up @@ -807,6 +810,8 @@ async def _handle_connection_closed_event(self, event):

:param event:
'''
self._for_testing_peer_closed_connection.set()
await trio.sleep(0)
Copy link
Member Author

@belm0 belm0 Jan 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the checkpoint could be eliminated in the common case at the cost of more complexity

def __init__():
   ...
   self._for_testing_peer_closed_connection = None

...

if self._for_testing_peer_closed_connection:
    self._for_testing_peer_closed_connection.set()
    await trio.sleep(0)

await self._write_pending()
await self._close_web_socket(event.code, event.reason or None)
self._close_handshake.set()
Expand Down