Fix client read loop spinning and stream message reading - #289
Open
GerardGao wants to merge 2 commits into
Open
Conversation
The client read loop ignored all read errors, so a closed connection caused it to busy-spin at 100% CPU calling Read millions of times per second until Close() was called. Exit the loop on read error instead; in-flight transactions are terminated by the existing timeout collector, so the client does not go silent.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #289 +/- ##
==========================================
+ Coverage 66.43% 66.61% +0.18%
==========================================
Files 27 27
Lines 2124 2127 +3
==========================================
+ Hits 1411 1417 +6
+ Misses 700 698 -2
+ Partials 13 12 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The client read a single buffer of 1024 bytes per message, which truncated messages larger than the buffer and desynchronized stream connections when messages were fragmented or coalesced. Detect stream connections via LocalAddr and reassemble complete messages from the length field before decoding; datagram connections now use a buffer that fits the largest possible STUN message.
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.
This PR fixes two issues in the client read loop.
1. Exit read loop on permanent connection error
Client.readUntilClosed ignores read errors, so once the peer closes the connection the loop spins at ~200M Read calls/sec (reproduced: 25M calls in 100ms), pinning a CPU until the client is closed.
The connection is unusable after a read error anyway: the peer may have closed it, or a stream transport may be out of sync. Exiting the loop lets the existing timeout collector terminate in-flight transactions, so the client stays consistent with RFC 5389 RTO semantics.
2. Read large and fragmented messages on streams
The client read a single 1024-byte buffer per message, which:
Datagram connections now use a buffer sized to the largest possible STUN message (20-byte header + 65535-byte body). Stream connections are detected via LocalAddr and messages are reassembled from the 16-bit length field before decoding, handling fragmentation, coalescing and a final message delivered together with EOF.
Tests
Added regression tests covering both fixes:
All modified lines are covered by tests; the full suite, -race, golangci-lint and the wasm build pass.