fix(tun): close TUN gracefully on client shutdown - #1675
Open
ruib-7f79mj44pz wants to merge 1 commit into
Open
Conversation
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.
Problem
On Windows, after exiting the Hysteria client in TUN mode, starting it again can connect to the server successfully but then stall for approximately 15 seconds before failing to create the TUN interface.
A representative log excerpt is shown below, with relative timestamps and unrelated connection metadata and terminal color codes omitted:
The
TUN listeningmessage is logged beforeServer.Serveis called, so it does not confirm that interface creation succeeded.The existing client shutdown path neither signals the TUN service to stop nor waits for its cleanup. The TUN stack uses
context.Background(), and its deferred interface and stack cleanup only runs afterRun()returns. The process can therefore exit before that cleanup executes, potentially interfering with subsequent startup.Changes
runClientthroughclientTUNandServer.Serveto the TUN stack.context.AfterFuncto close the TUN interface on cancellation and unblockRun(). Coordinate the callback with deferred interface cleanup usingsync.Once, and stop the callback registration whenServeexits.clientTUNto return, including its deferred cleanup, before completing signal-driven shutdown or callinglogger.Fatalafter a mode failure.Run()returning while the service context is canceled as normal shutdown.context.Canceled. Other TCP/UDP errors remain logged.Validation
git diff --cached --checkpassed.go test ./...passed when run from theappdirectory.Repeated manual startup and shutdown tests in TUN mode on Windows confirmed that:
context canceledwarnings were observed during shutdown.Manual runtime validation was limited to Windows. The TUN package currently reports
[no test files]; this PR does not add automated TUN shutdown tests.