Skip to contentSkip to conversation

tun/netstack: do not panic when closing with a packet in flight

PATCH New Tim Foerster 1 participant 1 message Oct 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tun/netstack: do not panic when closing with a packet in flight

Close() closes incomingPacket while WriteNotify() may still be parked on a send to it. RemoveNotify() beforehand narrows the window but cannot close it: a WriteNotify() that is already past ep.Read() holds a packet, and when it loses the race the process dies with "send on closed channel" on a goroutine owned by gvisor, which the caller cannot recover. The packet that loses is typically one the stack generates by itself, such as an RST for a refused connection, so the window is not avoidable from outside the package.

Instead of closing incomingPacket, signal shutdown on a dedicated closing channel that WriteNotify() and Read() select on. This releases a pending send instead of turning it into a panic, and Read() still returns os.ErrClosed. Close() is additionally wrapped in a sync.Once, matching the other tun implementations, so that closing twice no longer panics with "close of closed channel".

---
 tun/netstack/tun.go      | 41 ++++++++++++++++++----------
 tun/netstack/tun_test.go | 59 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 15 deletions(-)
 create mode 100644 tun/netstack/tun_test.go
tun/netstack/tun.go +26 -15
index a7aec9e..df12c3c 100644
@@ -19,6 +19,7 @@ import (
 	"regexp"
 	"strconv"
 	"strings"
+	"sync"
 	"syscall"
 	"time"
 
@@ -45,6 +46,8 @@ type netTun struct {
 	events         chan tun.Event
 	notifyHandle   *channel.NotificationHandle
 	incomingPacket chan *buffer.View
+	closeOnce      sync.Once
+	closing        chan struct{}
 	mtu            int
 	dnsServers     []netip.Addr
 	hasV4, hasV6   bool
@@ -63,6 +66,7 @@ func CreateNetTUN(localAddresses, dnsServers []netip.Addr, mtu int) (tun.Device,
 		stack:          stack.New(opts),
 		events:         make(chan tun.Event, 10),
 		incomingPacket: make(chan *buffer.View),
+		closing:        make(chan struct{}),
 		dnsServers:     dnsServers,
 		mtu:            mtu,
 	}
@@ -121,8 +125,10 @@ func (tun *netTun) Events() <-chan tun.Event {
 }
 
 func (tun *netTun) Read(buf [][]byte, sizes []int, offset int) (int, error) {
-	view, ok := <-tun.incomingPacket
-	if !ok {
+	var view *buffer.View
+	select {
+	case view = <-tun.incomingPacket:
+	case <-tun.closing:
 		return 0, os.ErrClosed
 	}
 
@@ -163,22 +169,27 @@ func (tun *netTun) WriteNotify() {
 	view := pkt.ToView()
 	pkt.DecRef()
 
-	tun.incomingPacket <- view
+	select {
+	case tun.incomingPacket <- view:
+	case <-tun.closing:
+	}
 }
 
 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)
-	}
-
-	if tun.incomingPacket != nil {
-		close(tun.incomingPacket)
-	}
+	tun.closeOnce.Do(func() {
+		// Unblock any WriteNotify parked on a send, and any pending Read.
+		// incomingPacket is never closed, as WriteNotify may be mid-send.
+		close(tun.closing)
+
+		tun.stack.RemoveNIC(1)
+		tun.stack.Close()
+		tun.ep.RemoveNotify(tun.notifyHandle)
+		tun.ep.Close()
+
+		if tun.events != nil {
+			close(tun.events)
+		}
+	})
 
 	return nil
 }
tun/netstack/tun_test.go new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.55.0