Skip to content

Commit 3dcd99a

Browse files
committed
todo: handle timeout
1 parent 0679efb commit 3dcd99a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

libp2p/stream_muxer/mplex/mplex_stream.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,24 @@ async def close(self) -> None:
176176
Closing a stream closes it for writing and closes the remote end for
177177
reading but allows writing in the other direction.
178178
"""
179-
# TODO error handling with timeout
180-
181179
async with self.close_lock:
182180
if self.event_local_closed.is_set():
183181
return
184182

185183
flag = (
186184
HeaderTags.CloseInitiator if self.is_initiator else HeaderTags.CloseReceiver
187185
)
188-
# TODO: Raise when `muxed_conn.send_message` fails and `Mplex` isn't shutdown.
189-
await self.muxed_conn.send_message(flag, None, self.stream_id)
186+
187+
try:
188+
with trio.fail_after(5): # timeout in seconds
189+
await self.muxed_conn.send_message(flag, None, self.stream_id)
190+
except trio.TooSlowError:
191+
raise TimeoutError("Timeout while trying to close the stream")
192+
except MuxedConnUnavailable:
193+
if not self.muxed_conn.event_shutting_down.is_set():
194+
raise RuntimeError(
195+
"Failed to send close message and Mplex isn't shutting down"
196+
)
190197

191198
_is_remote_closed: bool
192199
async with self.close_lock:

0 commit comments

Comments
 (0)