Skip to content

Commit 8352d19

Browse files
authored
Merge pull request #752 from Minimega12121/todo/handletimeout
todo: handle timeout
2 parents 9b667bd + ceb9f7d commit 8352d19

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

libp2p/stream_muxer/mplex/mplex_stream.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,24 @@ async def close(self) -> None:
247247
Closing a stream closes it for writing and closes the remote end for
248248
reading but allows writing in the other direction.
249249
"""
250-
# TODO error handling with timeout
251-
252250
async with self.close_lock:
253251
if self.event_local_closed.is_set():
254252
return
255253

256254
flag = (
257255
HeaderTags.CloseInitiator if self.is_initiator else HeaderTags.CloseReceiver
258256
)
259-
# TODO: Raise when `muxed_conn.send_message` fails and `Mplex` isn't shutdown.
260-
await self.muxed_conn.send_message(flag, None, self.stream_id)
257+
258+
try:
259+
with trio.fail_after(5): # timeout in seconds
260+
await self.muxed_conn.send_message(flag, None, self.stream_id)
261+
except trio.TooSlowError:
262+
raise TimeoutError("Timeout while trying to close the stream")
263+
except MuxedConnUnavailable:
264+
if not self.muxed_conn.event_shutting_down.is_set():
265+
raise RuntimeError(
266+
"Failed to send close message and Mplex isn't shutting down"
267+
)
261268

262269
_is_remote_closed: bool
263270
async with self.close_lock:

newsfragments/752.internal.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[mplex] Add timeout and error handling during stream close

tests/core/stream_muxer/test_mplex_stream.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
MplexStreamClosed,
99
MplexStreamEOF,
1010
MplexStreamReset,
11+
MuxedConnUnavailable,
1112
)
1213
from libp2p.stream_muxer.mplex.mplex import (
1314
MPLEX_MESSAGE_CHANNEL_SIZE,
@@ -213,3 +214,39 @@ async def test_mplex_stream_reset(mplex_stream_pair):
213214
# `reset` should do nothing as well.
214215
await stream_0.reset()
215216
await stream_1.reset()
217+
218+
219+
@pytest.mark.trio
220+
async def test_mplex_stream_close_timeout(monkeypatch, mplex_stream_pair):
221+
stream_0, stream_1 = mplex_stream_pair
222+
223+
# (simulate hanging)
224+
async def fake_send_message(*args, **kwargs):
225+
await trio.sleep_forever()
226+
227+
monkeypatch.setattr(stream_0.muxed_conn, "send_message", fake_send_message)
228+
229+
with pytest.raises(TimeoutError):
230+
await stream_0.close()
231+
232+
233+
@pytest.mark.trio
234+
async def test_mplex_stream_close_mux_unavailable(monkeypatch, mplex_stream_pair):
235+
stream_0, _ = mplex_stream_pair
236+
237+
# Patch send_message to raise MuxedConnUnavailable
238+
def raise_unavailable(*args, **kwargs):
239+
raise MuxedConnUnavailable("Simulated conn drop")
240+
241+
monkeypatch.setattr(stream_0.muxed_conn, "send_message", raise_unavailable)
242+
243+
# Case 1: Mplex is shutting down — should not raise
244+
stream_0.muxed_conn.event_shutting_down.set()
245+
await stream_0.close() # Should NOT raise
246+
247+
# Case 2: Mplex is NOT shutting down — should raise RuntimeError
248+
stream_0.event_local_closed = trio.Event() # Reset since it was set in first call
249+
stream_0.muxed_conn.event_shutting_down = trio.Event() # Unset the shutdown flag
250+
251+
with pytest.raises(RuntimeError, match="Failed to send close message"):
252+
await stream_0.close()

0 commit comments

Comments
 (0)