Cancel VPN gateway loops together

This commit is contained in:
2026-05-16 12:29:12 +03:00
parent d170820445
commit da59de7042
3 changed files with 69 additions and 4 deletions
@@ -266,15 +266,18 @@ func (g *Gateway) run(ctx context.Context, tun readWriteCloser) error {
if closer, ok := g.Transport.(packetTransportCloser); ok {
defer closer.Close()
}
runCtx, cancel := context.WithCancel(ctx)
defer cancel()
errCh := make(chan error, 2)
go func() { errCh <- g.copyGatewayToClient(ctx, tun) }()
go func() { errCh <- g.copyClientToGateway(ctx, tun) }()
go func() { errCh <- g.copyGatewayToClient(runCtx, tun) }()
go func() { errCh <- g.copyClientToGateway(runCtx, tun) }()
select {
case <-ctx.Done():
return ctx.Err()
case <-runCtx.Done():
return runCtx.Err()
case err := <-errCh:
cancel()
return err
}
}