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 <bradfitz@tailscale.com>
Change-Id: I772ca698fc81f7389542e87d108eec0c04b98df1
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-11-20 06:32:38 -08:00
1 parent c8ba9da543
commit 5c7e705edf
2 files changed
+26 -1

No files matched your search

+6 -1
View File
@@ -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()) {
+20
View File
@@ -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)
}
})
}