From a4cb23ac736d723c91a399e15febb9489cffa39c Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Thu, 2 May 2024 16:27:27 +0400 Subject: [PATCH] tun: fix race during WriteNotify and Close Signed-off-by: Spike Curtis --- tun/netstack/tun.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index d8e70bb03..41d49fd9f 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -166,7 +166,7 @@ func (tun *netTun) WriteNotify() { } func (tun *netTun) Close() error { - tun.stack.RemoveNIC(1) + tun.stack.Destroy() if tun.events != nil { close(tun.events) @@ -175,6 +175,8 @@ func (tun *netTun) Close() error { tun.ep.Close() if tun.incomingPacket != nil { + // It's safe to close the channel here because tun.stack.Destroy() ensures that no worker + // goroutines could be calling WriteNotify() close(tun.incomingPacket) }