Skip to content

Commit 3af7cf2

Browse files
authored
Fix exception when disconnected by another client (#117)
1 parent 44003cb commit 3af7cf2

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

saltyrtc/server/protocol.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def add_pending(self, client: 'PathClient') -> None:
124124
:class:`PathClient`.
125125
"""
126126
if not self.attached:
127-
raise ValueError('Patch has been detached!')
127+
raise ValueError('Path has been detached!')
128128
self._pending.add(client)
129129

130130
def has_client(self, client: 'PathClient') -> bool:
@@ -139,7 +139,7 @@ def has_client(self, client: 'PathClient') -> bool:
139139
:class:`PathClient`.
140140
"""
141141
if not self.attached:
142-
raise ValueError('Patch has been detached!')
142+
raise ValueError('Path has been detached!')
143143

144144
# Note: No need to check for an unassigned ID since the server's ID will never
145145
# be available in the slots.
@@ -169,7 +169,7 @@ def get_initiator(self) -> 'PathClient':
169169
:class:`PathClient`.
170170
"""
171171
if not self.attached:
172-
raise ValueError('Patch has been detached!')
172+
raise ValueError('Path has been detached!')
173173
if self._initiator is None:
174174
raise KeyError('No initiator present')
175175
return self._initiator
@@ -190,7 +190,7 @@ def set_initiator(self, initiator: 'PathClient') -> Optional['PathClient']:
190190
Return the previously set initiator or `None`.
191191
"""
192192
if not self.attached:
193-
raise ValueError('Patch has been detached!')
193+
raise ValueError('Path has been detached!')
194194

195195
# Remove initiator from 'pending' set
196196
self._pending.remove(initiator)
@@ -221,7 +221,7 @@ def get_responder(self, id_: ResponderAddress) -> 'PathClient':
221221
:class:`PathClient`.
222222
"""
223223
if not self.attached:
224-
raise ValueError('Patch has been detached!')
224+
raise ValueError('Path has been detached!')
225225
return self._responders[id_]
226226

227227
def get_responder_ids(self) -> Iterable[ResponderAddress]:
@@ -232,7 +232,7 @@ def get_responder_ids(self) -> Iterable[ResponderAddress]:
232232
:class:`PathClient`.
233233
"""
234234
if not self.attached:
235-
raise ValueError('Patch has been detached!')
235+
raise ValueError('Path has been detached!')
236236
return self._responders.keys()
237237

238238
def add_responder(self, responder: 'PathClient') -> ResponderAddress:
@@ -253,7 +253,7 @@ def add_responder(self, responder: 'PathClient') -> ResponderAddress:
253253
Return the assigned slot identifier.
254254
"""
255255
if not self.attached:
256-
raise ValueError('Patch has been detached!')
256+
raise ValueError('Path has been detached!')
257257

258258
# Calculate slot id
259259
id_ = len(self._responders) + 0x02
@@ -294,7 +294,7 @@ def remove_client(self, client: 'PathClient') -> None:
294294
:class:`PathClient`.
295295
"""
296296
if not self.attached:
297-
raise ValueError('Patch has been detached!')
297+
raise ValueError('Path has been detached!')
298298

299299
if client.state == ClientState.restricted:
300300
# Client has never been authenticated, so it should be in

saltyrtc/server/server.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,14 @@ async def handle_client(self) -> None:
540540
# We can safely ignore this since clients will be removed immediately
541541
# from the path in case they are being dropped by another client.
542542
pass
543-
self._server.paths.clean(path)
543+
except ValueError:
544+
# We can also safely ignore this since a client may have already removed
545+
# itself from the path.
546+
pass
547+
548+
# Clean the path (if still attached)
549+
if path.attached:
550+
self._server.paths.clean(path)
544551

545552
# Done! Raise the result
546553
raise result
@@ -930,9 +937,6 @@ def get(self, initiator_key: InitiatorPublicPermanentKey) -> Path:
930937
return self.paths[initiator_key]
931938

932939
def clean(self, path: Path) -> None:
933-
if not path.attached:
934-
self._log.error('Path {} has already been detached', path.number)
935-
return
936940
if path.empty:
937941
path.attached = False
938942
try:

tests/test_server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ async def handle_client(self):
945945
assert path.empty
946946
with pytest.raises(ValueError) as exc_info:
947947
path.has_client(initiator_path_client)
948-
assert 'Patch has been detached!' in str(exc_info.value)
948+
assert 'Path has been detached!' in str(exc_info.value)
949949

950950
# Responder handshake
951951
responder, r = await client_factory(responder_handshake=True)
@@ -964,7 +964,7 @@ async def handle_client(self):
964964
assert path.empty
965965
with pytest.raises(ValueError) as exc_info:
966966
path.has_client(responder_path_client)
967-
assert 'Patch has been detached!' in str(exc_info.value)
967+
assert 'Path has been detached!' in str(exc_info.value)
968968

969969
# Release initiator protocol instance
970970
responder_connected_future.set_result(None)

0 commit comments

Comments
 (0)