Skip to content

fix: replace copy_bidirectional with select!+copy to prevent TCP conn…#460

Open
cuber wants to merge 1 commit intorathole-org:mainfrom
cuber:fix/tcp-connection-leak
Open

fix: replace copy_bidirectional with select!+copy to prevent TCP conn…#460
cuber wants to merge 1 commit intorathole-org:mainfrom
cuber:fix/tcp-connection-leak

Conversation

@cuber
Copy link

@cuber cuber commented Mar 5, 2026

Connection leak

copy_bidirectional uses TCP half-close: when one side sends FIN, it shuts down the write side of the other stream but waits indefinitely for the reverse direction to also finish. If the remote end (e.g. xray SS-2022) never closes its side after receiving a half-close, both the visitor (CLOSE-WAIT) and data channel (FIN-WAIT-2) connections leak permanently.

Replace with tokio::select! over two io::copy tasks. When either direction finishes (EOF or error), both write sides are immediately shut down and the cancelled copy task is dropped, ensuring full connection cleanup regardless of peer behavior.

Also explicitly shutdown unforwarded visitors in the server connection pool when data channels are unavailable.

@cuber cuber force-pushed the fix/tcp-connection-leak branch 8 times, most recently from 8edc3fa to e3bf43e Compare March 9, 2026 10:36
…ection leak

When using copy_bidirectional, if one side sends FIN, it shuts down the
write side of the other stream but waits indefinitely for the reverse
direction to finish. If the remote endpoint doesn't close after receiving
a half-close, connections remain permanently in CLOSE-WAIT/FIN-WAIT-2.

Replace with tokio::select! over two io::copy tasks so both directions
are terminated immediately when either side finishes, ensuring full
connection cleanup regardless of peer behavior. Also explicitly shutdown
unforwarded visitors in the server connection pool.

Also bump rust-toolchain to 1.82 and fix clippy lints.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cuber cuber force-pushed the fix/tcp-connection-leak branch from e3bf43e to 8feb26b Compare March 9, 2026 16:34
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.

1 participant