Commit Graph

24 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 b597467858 add context for conn 2025-08-04 19:29:38 +08:00
ginuerzh 281295d02f fix tun device 2025-07-23 22:44:02 +08:00
ginuerzh 355c72477a add router handler & connector 2025-02-07 15:44:31 +08:00
ginuerzh 69455ace9d add handler recorder 2024-09-14 23:21:25 +08:00
ginuerzh c1d0887a9b add port range support for service 2024-07-08 22:38:21 +08:00
ginuerzh 332a3a1cd0 add p2p option for tun handler 2024-04-26 20:56:56 +08:00
ginuerzh 74639e9c4e add router component 2023-11-19 14:23:21 +08:00
ginuerzh ea585fc25d add reload and plugin support for hop 2023-09-28 21:04:15 +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 05ddda70e3 update tap 2022-11-04 18:48:28 +08:00
ginuerzh 1a1c038fd7 update chain route 2022-09-20 11:48:51 +08:00
ginuerzh a04c8b45f3 update selector 2022-09-04 13:24:32 +08:00
ginuerzh 00f7fa2997 add weight for selector 2022-09-02 17:23:59 +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
ginuerzh 4c1da3e4d4 update tun/tap 2022-08-18 14:50:13 +08:00
ginuerzh f3f3acd4e1 more fine-grained log level 2022-08-18 11:34:57 +08:00
ginuerzh a3346ad246 add pkgs from core 2022-04-04 12:44:35 +08:00
ginuerzh 36947bcbd9 fix imports 2022-03-16 20:06:34 +08:00
ginuerzh 073ad80ce4 remove unnecessary conn 2022-03-15 15:45:22 +08:00
ginuerzh 9397cb5351 initial commit 2022-03-14 20:27:14 +08:00