mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 17:21:18 -08:00
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 <[email protected]> Change-Id: I772ca698fc81f7389542e87d108eec0c04b98df1
138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package memnet
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
)
|
|
|
|
func TestPipeHello(t *testing.T) {
|
|
p := NewPipe("p1", 1<<16)
|
|
msg := "Hello, World!"
|
|
if n, err := p.Write([]byte(msg)); err != nil {
|
|
t.Fatal(err)
|
|
} else if n != len(msg) {
|
|
t.Errorf("p.Write(%q) n=%d, want %d", msg, n, len(msg))
|
|
}
|
|
b := make([]byte, len(msg))
|
|
if n, err := p.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
} else if n != len(b) {
|
|
t.Errorf("p.Read(%q) n=%d, want %d", string(b[:n]), n, len(b))
|
|
}
|
|
if got := string(b); got != msg {
|
|
t.Errorf("p.Read: %q, want %q", got, msg)
|
|
}
|
|
}
|
|
|
|
func TestPipeTimeout(t *testing.T) {
|
|
t.Run("write", func(t *testing.T) {
|
|
p := NewPipe("p1", 1<<16)
|
|
p.SetWriteDeadline(time.Now().Add(-1 * time.Second))
|
|
n, err := p.Write([]byte{'h'})
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Errorf("missing write timeout got err: %v", err)
|
|
}
|
|
if n != 0 {
|
|
t.Errorf("n=%d on timeout", n)
|
|
}
|
|
})
|
|
t.Run("read", func(t *testing.T) {
|
|
p := NewPipe("p1", 1<<16)
|
|
p.Write([]byte{'h'})
|
|
|
|
p.SetReadDeadline(time.Now().Add(-1 * time.Second))
|
|
b := make([]byte, 1)
|
|
n, err := p.Read(b)
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Errorf("missing read timeout got err: %v", err)
|
|
}
|
|
if n != 0 {
|
|
t.Errorf("n=%d on timeout", n)
|
|
}
|
|
})
|
|
t.Run("block-write", func(t *testing.T) {
|
|
p := NewPipe("p1", 1<<16)
|
|
p.SetWriteDeadline(time.Now().Add(10 * time.Millisecond))
|
|
if err := p.Block(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := p.Write([]byte{'h'}); !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Fatalf("want write timeout got: %v", err)
|
|
}
|
|
})
|
|
t.Run("block-read", func(t *testing.T) {
|
|
p := NewPipe("p1", 1<<16)
|
|
p.Write([]byte{'h', 'i'})
|
|
p.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
|
|
b := make([]byte, 1)
|
|
if err := p.Block(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := p.Read(b); !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Fatalf("want read timeout got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLimit(t *testing.T) {
|
|
p := NewPipe("p1", 1)
|
|
errCh := make(chan error)
|
|
go func() {
|
|
n, err := p.Write([]byte{'a', 'b', 'c'})
|
|
if err != nil {
|
|
errCh <- err
|
|
} else if n != 3 {
|
|
errCh <- fmt.Errorf("p.Write n=%d, want 3", n)
|
|
} else {
|
|
errCh <- nil
|
|
}
|
|
}()
|
|
b := make([]byte, 3)
|
|
|
|
if n, err := p.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
} else if n != 1 {
|
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
|
}
|
|
if n, err := p.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
} else if n != 1 {
|
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
|
}
|
|
if n, err := p.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
} else if n != 1 {
|
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
|
}
|
|
|
|
if err := <-errCh; err != nil {
|
|
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)
|
|
}
|
|
})
|
|
}
|