fix(mplex): serve buffered data and EOF before reporting the connection down - #2946
Open
gmelodie wants to merge 2 commits into
Open
fix(mplex): serve buffered data and EOF before reporting the connection down#2946gmelodie wants to merge 2 commits into
gmelodie wants to merge 2 commits into
Conversation
gmelodie
force-pushed
the
fix/tor/flaky-test-closewrite
branch
from
August 14, 2026 18:42
cb78103 to
50384f9
Compare
gmelodie
marked this pull request as ready for review
August 14, 2026 19:05
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2946 +/- ##
=======================================
Coverage 82.25% 82.25%
=======================================
Files 178 178
Lines 32466 32473 +7
Branches 12 11 -1
=======================================
+ Hits 26704 26710 +6
- Misses 5762 5763 +1
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LPChannel.readOnceraisedLPStreamConnDownError("Stream Underlying Connection Closed!") whenever the muxer connection was closed, even when the peer's bytes and its EOF marker already sat in the channel buffer. The reader lost data that had already arrived. This is the failure in issue #2763: the Tor stream testcloseWrite immediately after newStreamexpects the server handler to read the mplex EOF as a 0-byte read, and it instead got an unhandledAssertionDefectfromshould not fail: Stream Underlying Connection Closed!.The guard now fires only when the channel holds nothing: no buffered bytes and no pushed EOF. A reader drains what the peer sent, gets the regular 0-byte EOF marker, and only then learns that the connection is down.
Caveat, stated up front: the CI failure did not reproduce locally in repeated runs of the Tor suite. The diagnosis comes from the two stack traces in the issue. Both show the assertion inside the server stream handler on its first
readOnce, reached synchronously while the mplex read loop processes theNewframe, and thes.conn.closedcheck is the only guard on that path that produces this message.Two defects in the relay
bridgeput the connection down at that moment over Tor, because the test stub relays through it:bridgere-armedreadOnceon a stream that had returned EOF. The next pass awaited that already-failed future and left the loop through theLPStreamErrorpath. The relay still ends when either direction reaches EOF, as before; it now ends through the loop condition instead of through an exception, and it drains a direction that finished in the same pass.bridgeraised out of thetryblock and skipped its two trailingcancelAndWaitcalls, so both in-flight reads outlived the frame whose buffers they write into. Adefernow cancels both throughallFutureson every exit path.TorServerStubnow closes each connection withdeferfrom the moment it exists, so a raise in the SOCKS handshake or a cancelled accept loop no longer leaves a socket open.Affected Areas
Transports
Mplex channel read path, and the Tor test stub.
Protocol Logic
bridgeinlibp2p/protocols/connectivity/relay/utils.nim, used by relay v1 and v2.Compatibility & Downstream Validation
No API change and no wire change.
bridgekeeps the same start and end conditions, so a relay peer sees the same connection lifetime.Impact on Library Users
A reader on an mplex channel now receives the data and the EOF that already arrived before the underlying connection went down, instead of an immediate
LPStreamConnDownError. Code that treatsLPStreamConnDownErroras "the peer is gone" keeps working, because the error still arrives once the buffer is empty.The relaxed guard cannot park a reader on a dead connection.
pushedEofis set beforepushEofqueues the marker, and every path that sets it either queues the marker or has already setisEof, in which caseatEof()raisesLPStreamRemoteClosedErrorone line earlier. The read queue holds one item, so a pendingaddLastcompletes as soon as the reader pops.Risk Assessment
bridgeends on the first EOF in either direction, same as before.Testing
(connection down) - should read buffered data and EOFintests/libp2p/muxers/test_mplex.nimpushes data and an EOF into a channel, closes the underlying connection, and checks that the reader gets the bytes and then a 0-byte read. It fails on the old code withLPStreamConnDownError.References
Additional Notes
TorServerStub.startstill dies on the first bad request:self.addrTable[$(address)]raisesKeyErrorfor an unregistered address, and that escapes the accept loop, after which every later test blocks on a stub that stopped accepting. Left out of this PR because keeping the loop alive changes stub semantics for every transport test that uses it.