From a6baab333a55d114f81f42678b310e80afed91db Mon Sep 17 00:00:00 2001 From: Yunus Emre Karaman Date: Sun, 15 Nov 2026 20:12:22 +0300 Subject: [PATCH] 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 --- net/memnet/conn.go | 13 +++++++++++++ net/memnet/conn_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/net/memnet/conn.go b/net/memnet/conn.go index 8cab63403..df11c7f34 100644 --- a/net/memnet/conn.go +++ b/net/memnet/conn.go @@ -85,6 +85,19 @@ func (c *connHalf) Close() error { return c.r.Close() } +// CloseWrite closes the write side of the connection. The peer can read +// buffered data, then gets io.EOF. It matches net.TCPConn.CloseWrite, which +// proxies such as tcpproxy rely on to propagate a backend's close. +func (c *connHalf) CloseWrite() error { + return c.w.Close() +} + +// CloseRead closes the read side of the connection. Subsequent reads return +// io.EOF and the peer's writes fail. +func (c *connHalf) CloseRead() error { + return c.r.Close() +} + func (c *connHalf) SetDeadline(t time.Time) error { err1 := c.SetReadDeadline(t) err2 := c.SetWriteDeadline(t) diff --git a/net/memnet/conn_test.go b/net/memnet/conn_test.go index 340c4c6ee..cc535b2d2 100644 --- a/net/memnet/conn_test.go +++ b/net/memnet/conn_test.go @@ -4,6 +4,8 @@ package memnet import ( + "errors" + "io" "net" "testing" @@ -19,3 +21,38 @@ func TestConn(t *testing.T) { }, 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") + } +}