Skip to content

fix(tun): close TUN gracefully on client shutdown - #1675

Open
ruib-7f79mj44pz wants to merge 1 commit into
HyNetworks:masterfrom
ruib-7f79mj44pz:fix/gracefully-close-tun
Open

fix(tun): close TUN gracefully on client shutdown#1675
ruib-7f79mj44pz wants to merge 1 commit into
HyNetworks:masterfrom
ruib-7f79mj44pz:fix/gracefully-close-tun

Conversation

@ruib-7f79mj44pz

Copy link
Copy Markdown

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:

+00s INFO  client mode
+01s INFO  connected to server
+01s INFO  TUN listening {"interface": "Hysteria 2"}
+17s FATAL failed to run TUN {"error": "failed to create tun interface: Cannot create a file when that file already exists."}

The TUN listening message is logged before Server.Serve is 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 after Run() returns. The process can therefore exit before that cleanup executes, potentially interfering with subsequent startup.

Changes

  • Pass a cancellable context from runClient through clientTUN and Server.Serve to the TUN stack.
  • Use context.AfterFunc to close the TUN interface on cancellation and unblock Run(). Coordinate the callback with deferred interface cleanup using sync.Once, and stop the callback registration when Serve exits.
  • Cancel the TUN context and wait for clientTUN to return, including its deferred cleanup, before completing signal-driven shutdown or calling logger.Fatal after a mode failure.
  • Treat Run() returning while the service context is canceled as normal shutdown.
  • Suppress TCP/UDP cancellation event logs only when both the reported error and the service-level context indicate context.Canceled. Other TCP/UDP errors remain logged.

Validation

  • git diff --cached --check passed.

  • go test ./... passed when run from the app directory.

  • Repeated manual startup and shutdown tests in TUN mode on Windows confirmed that:

    • Shutdown completed without hanging.
    • The virtual network adapter was removed after each shutdown.
    • Subsequent startups succeeded without the delay and interface-creation error shown above.
    • No context canceled warnings 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.

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