Files
tailscale/net/memnet/conn_test.go
Yunus Emre Karaman a6baab333a net/memnet: implement CloseRead and CloseWrite
tcpproxy propagates a backend's close to the client by calling CloseWrite
on the source connection. connHalf did not implement it, so the call was a
no-op and the client never saw EOF. TestTCPRoundRobinHandler only unblocked
the proxy's remaining copy goroutine by writing more data after the first
echo. When the backend closed with that data still buffered in the pipe,
the write was discarded and the test hung until the 10 minute timeout.

Implement both halves of the half-close contract on connHalf. A new test
covers buffered data surviving CloseWrite, EOF on the read after it, and
writes failing after CloseRead.

Fixes #17533

Signed-off-by: Yunus Emre Karaman <[email protected]>
2026-11-15 10:35:58 -08:00

59 lines
1.4 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package memnet
import (
"errors"
"io"
"net"
"testing"
"golang.org/x/net/nettest"
)
func TestConn(t *testing.T) {
nettest.TestConn(t, func() (c1 net.Conn, c2 net.Conn, stop func(), err error) {
c1, c2 = NewConn("test", bufferSize)
return c1, c2, func() {
c1.Close()
c2.Close()
}, nil
})
}
func TestConnCloseWriteAndCloseRead(t *testing.T) {
a, b := NewConn("test", bufferSize)
// Half-close is not part of the Conn interface, matching net.Conn.
ac := a.(interface{ CloseWrite() error })
bc := b.(interface{ CloseRead() error })
if _, err := a.Write([]byte("hello")); err != nil {
t.Fatal(err)
}
if err := ac.CloseWrite(); err != nil {
t.Fatal(err)
}
// Buffered data survives the half-close; the next read is EOF.
buf := make([]byte, 8)
if n, err := b.Read(buf); n != 5 || err != nil {
t.Fatalf("Read = %d, %v; want 5, nil", n, err)
}
if _, err := b.Read(buf); !errors.Is(err, io.EOF) {
t.Fatalf("Read after CloseWrite = %v; want io.EOF", err)
}
if _, err := a.Write([]byte("x")); err == nil {
t.Fatal("Write after CloseWrite succeeded")
}
if err := bc.CloseRead(); err != nil {
t.Fatal(err)
}
if _, err := b.Read(buf); !errors.Is(err, io.EOF) {
t.Fatalf("Read after CloseRead = %v; want io.EOF", err)
}
if _, err := a.Write([]byte("x")); err == nil {
t.Fatal("Write after peer CloseRead succeeded")
}
}