Commit Graph

15 Commits

Author SHA1 Message Date
ginuerzh 37bc81576c fix(tun): prevent leaked goroutines in bidirectional copy on transport failure
transportClient and transportServer each launch two goroutines for
bidirectional copy between the TUN device and the remote connection.
Previously, when one goroutine errored, the function returned
immediately while the sibling goroutine continued running — reading
from and writing to connections that would soon be closed by the
caller's deferred cleanup. In the retry loop of handleClient and
handleServer, stale goroutines accumulated across iterations, racing
with new goroutines for the TUN device and writing to closed pipes.

This caused the "io: read/write on closed pipe" error reported in
go-gost/gost#345, and more importantly, prevented recovery because
subsequent retry iterations operated on a corrupted state.

Fix:
- Derive a per-transport context in transportClient/transportServer
  and check it at the top of each goroutine's loop to allow the
  first-failing goroutine to signal its sibling to exit
- Replace the single-error select with a collectFirstError helper
  that drains both goroutines and includes a timeout guard (5s) to
  prevent deadlock when a goroutine is stuck in a non-cancelable read
- Use a per-iteration context in handleClient for the keepalive
  goroutine so it is properly cancelled when an iteration ends
- Fix handleServer to use errors.Is(err, ErrTun) for consistency
- Fix transportServer's errc buffer from 1 to 2 to prevent a
  goroutine leak when both goroutines error simultaneously
2026-06-20 22:10:09 +08:00
ginuerzh e0915affda use fixed buffer size 2025-02-18 17:38:27 +08:00
ginuerzh 2132268780 Bump golang.zx2c4.com/wireguard 2025-02-11 22:13:57 +08:00
ginuerzh 355c72477a add router handler & connector 2025-02-07 15:44:31 +08:00
ginuerzh c1d0887a9b add port range support for service 2024-07-08 22:38:21 +08:00
ginuerzh 9584bdbf4c rm tunnel from relay 2023-11-14 22:35:46 +08:00
ginuerzh d7b7ac6357 add range port support for forwarder node 2023-11-14 19:41:57 +08:00
ginuerzh cc4310106b fix race condition 2023-10-19 23:47:47 +08:00
ginuerzh fb29d5c80e tun: support multiple IPs 2022-12-22 17:44:30 +08:00
ginuerzh bcfff22669 don't resolve server addr for tun handler 2022-11-25 18:14:47 +08:00
ginuerzh 81b6efc9b8 do not recreate tun device when connecting to server failed 2022-11-11 20:53:34 +08:00
ginuerzh 669e80b780 add auth for tun 2022-10-20 21:08:26 +08:00
ginuerzh bdd239fb09 add list support for auther, admission and bypass 2022-08-23 21:49:49 +08:00
ginuerzh fe6554d8a3 add keepalive for tun 2022-08-22 22:14:09 +08:00
ginuerzh ca414f655d update tun handler 2022-08-21 13:54:52 +08:00