File tree 2 files changed +9
-12
lines changed
2 files changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -928,14 +928,10 @@ async def handler(request: WebSocketRequest):
928
928
await trio .sleep (.1 )
929
929
930
930
931
- @pytest .mark .xfail (
932
- reason = 'send_message() API oversight for closing-in-process case' ,
933
- raises = None if WS_PROTO_VERSION < (1 , 2 , 0 ) else LocalProtocolError ,
934
- strict = True )
935
931
async def test_remote_close_local_message_race (nursery , autojump_clock ):
936
932
"""as remote initiates close, local attempts message (issue #175)
937
933
938
- This exposes multiple problems in the trio-websocket API and implementation:
934
+ This exposed multiple problems in the trio-websocket API and implementation:
939
935
* send_message() silently fails if a close is in progress. This was
940
936
likely an oversight in the API, since send_message() raises `ConnectionClosed`
941
937
only in the already-closed case, yet `ConnectionClosed` is defined to cover
@@ -957,9 +953,7 @@ async def handler(request: WebSocketRequest):
957
953
await client .send_message ('foo' )
958
954
await client ._for_testing_peer_closed_connection .wait ()
959
955
with pytest .raises (ConnectionClosed ):
960
- await client .send_message ('bar' ) # wsproto < 1.2.0: silently ignored
961
- # wsproto >= 1.2.0: raises LocalProtocolError
962
- # desired: raises ConnectionClosed
956
+ await client .send_message ('bar' )
963
957
964
958
965
959
@fail_after (DEFAULT_TEST_MAX_DURATION )
Original file line number Diff line number Diff line change @@ -932,7 +932,7 @@ async def send_message(self, message):
932
932
933
933
:param message: The message to send.
934
934
:type message: str or bytes
935
- :raises ConnectionClosed: if connection is already closed.
935
+ :raises ConnectionClosed: if connection is closed, or being closed
936
936
'''
937
937
if self ._close_reason :
938
938
raise ConnectionClosed (self ._close_reason )
@@ -1096,10 +1096,13 @@ async def _handle_close_connection_event(self, event):
1096
1096
1097
1097
:param wsproto.events.CloseConnection event:
1098
1098
'''
1099
- if self ._for_testing_peer_closed_connection :
1100
- self ._for_testing_peer_closed_connection .set ()
1101
- await trio .sleep (0 )
1102
1099
if self ._wsproto .state == ConnectionState .REMOTE_CLOSING :
1100
+ # Set _close_reason in advance, so that send_message() will raise
1101
+ # ConnectionClosed during the close handshake.
1102
+ self ._close_reason = CloseReason (event .code , event .reason or None )
1103
+ if self ._for_testing_peer_closed_connection :
1104
+ self ._for_testing_peer_closed_connection .set ()
1105
+ await trio .sleep (0 )
1103
1106
await self ._send (event .response ())
1104
1107
await self ._close_web_socket (event .code , event .reason or None )
1105
1108
self ._close_handshake .set ()
You can’t perform that action at this time.
0 commit comments