mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-20 16:49:12 +00:00
TUN inbound: End UDP flows and cancel dispatches when the inbound closes (#7350)
https://github.com/XTLS/Xray-core/pull/7350#pullrequestreview-5901009138
This commit is contained in:
@@ -25,7 +25,10 @@ import (
|
||||
|
||||
// Handler is managing object that tie together tun interface, ip stack and dispatch connections to the routing
|
||||
type Handler struct {
|
||||
// ctx is detached from the instance's; cancel ends it, and with it every dispatch started from it,
|
||||
// when the handler closes, since nothing else would
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
config *Config
|
||||
stack Stack
|
||||
tun Tun
|
||||
@@ -96,7 +99,7 @@ func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routin
|
||||
t.sniffingRequest = content.SniffingRequest
|
||||
}
|
||||
|
||||
t.ctx = core.ToBackgroundDetachedContext(ctx)
|
||||
t.ctx, t.cancel = context.WithCancel(core.ToBackgroundDetachedContext(ctx))
|
||||
t.policyManager = pm
|
||||
t.dispatcher = dispatcher
|
||||
|
||||
@@ -265,6 +268,11 @@ func (t *Handler) HandleConnection(conn net.Conn, destination net.Destination) {
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (t *Handler) Close() error {
|
||||
// cancel first so every dispatch in flight unwinds, then close the stack (resets TCP, ends UDP flows)
|
||||
// and the tun
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
return errors.Combine(common.CloseIfExists(t.stack), common.CloseIfExists(t.tun))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user