From 5c7e705edf6a6061bcf9170506c538cd5feb47c6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 20 Nov 2026 14:15:54 +0000 Subject: [PATCH] net/memnet: don't arm deadline timers on a closed Pipe Setting a deadline on a Pipe starts a goroutine that wakes blocked readers or writers when it passes, and only a later SetDeadline or Close cancels it. But Close only cancels the timers it finds, so setting a deadline after Close started a goroutine nothing would ever cancel, which lived until the deadline passed. An http.Server does exactly that if it's closed between accepting a conn and serving it: conn.serve sets the TLS handshake deadlines on the already-closed conn, then fails the handshake. Under synctest, the leftover goroutines make the test fail with "main bubble goroutine has exited but blocked goroutines remain", as seen in a lansport test in tailscale/tb on Windows CI. A closed Pipe has no blocked readers or writers left to wake, so don't arm a timer for it. The deadline itself is still recorded. Updates tailscale/corp#51705 Signed-off-by: Brad Fitzpatrick Change-Id: I772ca698fc81f7389542e87d108eec0c04b98df1 --- net/memnet/pipe.go | 7 ++++++- net/memnet/pipe_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/net/memnet/pipe.go b/net/memnet/pipe.go index 8caca57df..6f0fa4e45 100644 --- a/net/memnet/pipe.go +++ b/net/memnet/pipe.go @@ -160,8 +160,13 @@ func (p *Pipe) Close() error { return nil } +// deadlineTimer arranges to wake blocked readers and writers at t, returning +// a func that cancels that, or nil if there's nothing to cancel. It must be +// called with p.mu held. func (p *Pipe) deadlineTimer(t time.Time) func() { - if t.IsZero() { + // A closed pipe has no one left to wake, and nothing would ever cancel + // a timer armed now: Close cancels only the ones it finds. + if t.IsZero() || p.closed { return nil } if t.Before(time.Now()) { diff --git a/net/memnet/pipe_test.go b/net/memnet/pipe_test.go index ebd9dd8c2..96c062841 100644 --- a/net/memnet/pipe_test.go +++ b/net/memnet/pipe_test.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "testing" + "testing/synctest" "time" ) @@ -115,3 +116,22 @@ func TestLimit(t *testing.T) { t.Error(err) } } + +// TestDeadlineAfterClose checks that setting a deadline on a closed pipe +// doesn't leave a timer goroutine behind. An http.Server can do that if it's +// closed between accepting a conn and starting its TLS handshake, which sets +// the handshake deadlines. synctest.Test fails if a goroutine started in its +// bubble is still blocked when it returns. +func TestDeadlineAfterClose(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + p := NewPipe("p1", 1<<16) + p.Close() + dl := time.Now().Add(time.Hour) + if err := p.SetReadDeadline(dl); err != nil { + t.Fatal(err) + } + if err := p.SetWriteDeadline(dl); err != nil { + t.Fatal(err) + } + }) +}