Skip to content

Race send-loop write against cancel and deadline (#54 follow-up) - #59

Merged
lance0 merged 2 commits into
masterfrom
send-loop-write-race
Apr 21, 2026
Merged

lance0 merged 2 commits into
masterfrom
send-loop-write-race

Conversation

@lance0

@lance0 lance0 commented Apr 20, 2026

Copy link
Copy Markdown
Owner

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 stop still fires. Packet trace shows the connection keeps sending data for ~10 seconds on a -t 1sec run and no RST hits the wire (only FINs after drain completes).

Root cause: the earlier SO_LINGER=0 fix from PR #55 never triggers because the send-loop is stuck in stream.write(&buffer).await. Under heavy tc rate limiting the kernel's send buffer fills; write() returns Pending waiting 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, stream never drops, close() is never called, so SO_LINGER=0 is irrelevant.

Fix

Wrap write() in a biased tokio::select! racing against cancel.changed() and sleep_until(deadline):

let write_result = tokio::select! {
    biased;
    _ = cancel.changed() => break,
    _ = sleep_until(deadline), if !is_infinite => break,
    r = stream.write(&buffer) => r,
};

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) and send_data_half (bidir split).

Test plan

  • cargo fmt --check
  • cargo clippy --all-features --all-targets -- -D warnings
  • cargo test --all-features — all 186 tests pass
  • matttbe's reproducer: -P 4 --mptcp -t 1sec in his netns. Need him to verify.

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).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 using tokio::select! in send_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 thread src/tcp.rs
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 thread src/tcp.rs Outdated
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 thread src/tcp.rs
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 thread src/tcp.rs Outdated
Comment on lines +699 to +701
_ = cancel.changed() => {
debug!("Send cancelled during write for stream {}", stats.stream_id);
break;
Comment thread src/tcp.rs Outdated
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.
@lance0
lance0 merged commit 7936775 into master Apr 21, 2026
7 checks passed
@lance0
lance0 deleted the send-loop-write-race branch April 21, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants