Replies: 1 comment
-
Yamux Stream State DiagramstateDiagram-v2
[*] --> Open: Stream Created\n(SYN sent/received)
Open --> HalfClosedSend: Local sends FIN\n(send_closed=True)
Open --> HalfClosedRecv: Remote sends FIN\n(recv_closed=True)
Open --> Reset: RST received/sent\n(reset_received=True)
Open --> FullyClosed: Both send FIN\nsimultaneously
HalfClosedSend --> FullyClosed: Remote sends FIN\n(recv_closed=True)
HalfClosedRecv --> FullyClosed: Local sends FIN\n(send_closed=True)
Reset --> [*]: Stream terminated\n(closed=True)
FullyClosed --> [*]: Stream terminated\n(closed=True)
note right of Open
State: closed=False
send_closed=False
recv_closed=False
reset_received=False
Can read and write
end note
note right of HalfClosedSend
State: closed=False
send_closed=True
recv_closed=False
Can only read
end note
note right of HalfClosedRecv
State: closed=False
send_closed=False
recv_closed=True
Can only write
end note
note right of FullyClosed
State: closed=True
send_closed=True
recv_closed=True
No operations allowed
end note
note right of Reset
State: closed=True
send_closed=True
recv_closed=True
reset_received=True
Immediate termination
end note
State DescriptionsOpen
Half-Closed (Send)
Half-Closed (Receive)
Fully Closed
Reset
State Transitions
Key Implementation Details
Code References
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Yamux Implementation Details and Interop Guide
Overview
This discussion thread serves as a living document for Yamux stream multiplexer implementation details, design decisions, and interop considerations with the Noise module. This is particularly important as Yamux will be used in various interop efforts.
Recent Fixes (PR #1014)
Issue #930: Connection Closure Handling
Problem:
accept_stream()would hang indefinitely when the connection was closed.Solution:
new_stream_send_channelis closed in all shutdown scenarios (start(),close(),_cleanup_on_error())accept_stream()now raisesMuxedConnUnavailablewhen the channel closes, matching Mplex and QUIC behaviortrio.ClosedResourceErrorandtrio.BrokenResourceErrorKey Implementation Points:
start()method closes the channel after the nursery exits, with graceful handling if already closedhandle_incoming()triggers cleanup which closes the channelIssue #931: FIN/RST Flags on WINDOW_UPDATE Frames
Problem: FIN and RST flags were only processed on DATA frames, but the Yamux spec allows them on WINDOW_UPDATE frames.
Solution:
TYPE_WINDOW_UPDATEhandlerKey Implementation Points:
Connection Lifecycle Management
Startup Sequence
Yamux.start()is called, which:handle_incoming()event_startedto signal readinessnew_stream_send_channel(with error handling)Stream Acceptance
accept_stream()waits onnew_stream_receive_channelMuxedConnUnavailableSwarmConn) to properly clean upError Handling
handle_incoming()catches connection errors (RawConnError,OSError,IncompleteReadError,trio.ClosedResourceError,trio.BrokenResourceError)_cleanup_on_error()which:accept_stream())Clean Shutdown
close()sends GO_AWAY frameaccept_stream()Protocol Compliance
Flag Handling
Per the Yamux spec, FIN and RST flags can appear on:
Stream State Management
recv_closed: Set when FIN is received (half-close)send_closed: Set when local end sends FINclosed: Set when both directions are closedreset_received: Set when RST is receivedWindow Management
send_windowInterop Considerations with Noise Module
Connection Lifecycle
Error Propagation
MuxedConnUnavailableis raised when connection closesResource Cleanup
_cleanup_on_error()andclose()Testing Strategies
Unit Tests
Platform-Specific Considerations
Test Coverage
test_yamux_accept_stream_unblocks_on_close: Tests clean closuretest_yamux_accept_stream_unblocks_on_error: Tests error scenarios (skipped on Windows)test_yamux_fin_on_window_update: Tests FIN on WINDOW_UPDATEtest_yamux_rst_on_window_update: Tests RST on WINDOW_UPDATECommon Pitfalls and Troubleshooting
Hanging
accept_stream()Symptom:
accept_stream()never returns after connection closes.Cause: Channel not closed on connection termination.
Solution: Ensure channel is closed in all shutdown paths (
start(),close(),_cleanup_on_error()).Streams Not Closing
Symptom: Streams remain open after remote sends FIN/RST.
Cause: Flags only checked on DATA frames, not WINDOW_UPDATE.
Solution: Check flags on WINDOW_UPDATE frames per Yamux spec.
Resource Leaks
Symptom: Memory/connection leaks during interop.
Cause: Incomplete cleanup on errors.
Solution: Ensure
_cleanup_on_error()clears all data structures and wakes waiting tasks.Race Conditions
Symptom: Intermittent failures during connection closure.
Cause: Channel closure timing issues.
Solution: Use proper exception handling and ensure idempotent operations (e.g., try/except around channel.close()).
Design Decisions
Exception Type:
MuxedConnUnavailableSwarmConnto handle connection loss consistentlyMuxedStreamErrorChannel Closure in Multiple Places
start(): Handles normal exitclose(): Handles explicit close_cleanup_on_error(): Handles error scenariosFlag Processing Order
Future Considerations
Performance Optimization
Additional Protocol Features
Interop Testing
References
Note: This is a living document. Please update with additional findings, interop results, and implementation details as they emerge.
Beta Was this translation helpful? Give feedback.
All reactions