Repository navigation
Race send-loop write against cancel and deadline (#54 follow-up) - #59
Merged
Merged
Conversation
matttbe retested v0.9.8 and the 'Timed out waiting 2s' warning still fires: connections keep pushing data for ~10 seconds on a 1-second test and no RST ever hits the wire. SO_LINGER=0 never takes effect because stream.write().await is blocked — the kernel send buffer fills under tc rate limiting and the deadline check at the top of the loop doesn't run until the write returns. Wrap write() in a biased tokio::select! racing against cancel.changed() and sleep_until(deadline). When either wins, break immediately; the write future is dropped (no partial syscall), stream drops, close() fires with SO_LINGER=0 and peer sees RST. Applied to both send_data (unidirectional and single-socket bidir paths) and send_data_half (split-socket bidir send half).
There was a problem hiding this comment.
Pull request overview
This PR fixes a TCP send-loop shutdown bug where write().await can remain pending under heavy backpressure (e.g., tc rate limiting / MPTCP), preventing the loop from observing cancel/deadline and delaying socket close (so SO_LINGER=0 abortive close never triggers).
Changes:
- Race each TCP
write()against cancellation and the test deadline usingtokio::select!insend_data. - Apply the same race to the bidirectional split-socket sender path in
send_data_half.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+417
to
+422
| let write_result = tokio::select! { | ||
| biased; | ||
| _ = cancel.changed() => { | ||
| debug!("Send cancelled during write for stream {}", stats.stream_id); | ||
| break; | ||
| } |
Comment on lines
+423
to
+427
| _ = async { | ||
| if let Some(until) = deadline.checked_duration_since(tokio::time::Instant::now()) { | ||
| tokio::time::sleep(until).await; | ||
| } | ||
| }, if !is_infinite => { |
Comment on lines
+411
to
+416
| // Race the write against cancel and the deadline so we can interrupt | ||
| // a blocked write() when the test ends or is cancelled. Under heavy | ||
| // rate limiting (e.g. tc drops with MPTCP), the kernel send buffer | ||
| // fills and write() can block for far longer than the configured | ||
| // duration — without this select the loop would never exit, the | ||
| // socket would never close, and SO_LINGER=0 would never take effect. |
Comment on lines
+699
to
+701
| _ = cancel.changed() => { | ||
| debug!("Send cancelled during write for stream {}", stats.stream_id); | ||
| break; |
Comment on lines
+703
to
+707
| _ = async { | ||
| if let Some(until) = deadline.checked_duration_since(tokio::time::Instant::now()) { | ||
| tokio::time::sleep(until).await; | ||
| } | ||
| }, if !is_infinite => { |
…ion tests - Match on cancel.changed() result so sender-dropped vs cancel-signaled paths log distinctly instead of both claiming 'cancelled' - Replace inline 'if let Some(until) = checked_duration_since' async block with tokio::time::sleep_until(deadline) — clearer, same behavior - Two new #[tokio::test]s that force write() into Pending (tiny SNDBUF + non-reading peer) and assert send_data exits promptly on deadline and on cancel. Each wrapped in tokio::time::timeout(3s) so a future regression of the stuck-write issue fails the suite loudly instead of hanging CI.
5 of 6 tasks
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
matttbe retested v0.9.8 on his MPTCP + tc netns setup and reported the
WARN Timed out waiting 2s for N data streams to stopstill fires. Packet trace shows the connection keeps sending data for ~10 seconds on a-t 1secrun and no RST hits the wire (only FINs after drain completes).Root cause: the earlier
SO_LINGER=0fix from PR #55 never triggers because the send-loop is stuck instream.write(&buffer).await. Under heavy tc rate limiting the kernel's send buffer fills;write()returnsPendingwaiting for the socket to become writable again; the deadline check at the top of the loop doesn't run until the write completes. The loop never exits,streamnever drops,close()is never called, soSO_LINGER=0is irrelevant.Fix
Wrap
write()in a biasedtokio::select!racing againstcancel.changed()andsleep_until(deadline):When cancel or deadline wins, we break before the write completes. The write future is dropped (no partial syscall — the write was in
Pending). Stream drops,close()fires,SO_LINGER=0→ RST → peer sees the test is over.Applied to both
send_data(single-socket) andsend_data_half(bidir split).Test plan
cargo fmt --checkcargo clippy --all-features --all-targets -- -D warningscargo test --all-features— all 186 tests pass-P 4 --mptcp -t 1secin his netns. Need him to verify.