From: Jason A. Donenfeld Date: Mon, 5 May 2025 13:09:09 +0000 (+0200) Subject: tun/netstack: cleanup network stack at closing time X-Git-Tag: 0.0.20250515~8 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=45916071ba13c8f6ec44dfc46bda0449d2d54a9f;p=thirdparty%2Fwireguard-go.git tun/netstack: cleanup network stack at closing time Colin's commit went a step further and protected tun.incomingPacket with a lock on shutdown, but let's see if the tun.stack.Close() call actually solves that on its own. Suggested-by: kshangx Suggested-by: Colin Adler Signed-off-by: Jason A. Donenfeld --- diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 04f6986..a7aec9e 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -43,6 +43,7 @@ type netTun struct { ep *channel.Endpoint stack *stack.Stack events chan tun.Event + notifyHandle *channel.NotificationHandle incomingPacket chan *buffer.View mtu int dnsServers []netip.Addr @@ -70,7 +71,7 @@ func CreateNetTUN(localAddresses, dnsServers []netip.Addr, mtu int) (tun.Device, if tcpipErr != nil { return nil, nil, fmt.Errorf("could not enable TCP SACK: %v", tcpipErr) } - dev.ep.AddNotify(dev) + dev.notifyHandle = dev.ep.AddNotify(dev) tcpipErr = dev.stack.CreateNIC(1, dev.ep) if tcpipErr != nil { return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr) @@ -167,13 +168,14 @@ func (tun *netTun) WriteNotify() { func (tun *netTun) Close() error { tun.stack.RemoveNIC(1) + tun.stack.Close() + tun.ep.RemoveNotify(tun.notifyHandle) + tun.ep.Close() if tun.events != nil { close(tun.events) } - tun.ep.Close() - if tun.incomingPacket != nil { close(tun.incomingPacket) }