Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ func (m *Memberlist) streamListen() {

// handleConn handles a single incoming stream connection from the transport.
func (m *Memberlist) handleConn(conn net.Conn) {
defer func() {
defer func(conn net.Conn) {
Copy link

@pracucci pracucci Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is safe because the wrapped net.Conn returned by RemoveLabelHeaderFromStream() doesn't override the Close() but if in the future it will do it (unlikely honestly), then this change will trigger a latent bug. Maybe you can save a reference to the original connection instead, and close the wrapped if != nil and fallback to the original one?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the concern. I think the added test would catch this scenario. But I've also addressed it in e2d1a17 — I think it's simpler, and easier to follow like that.

_ = conn.Close()
}()
}(conn) // Capture the original conn, because the code before shadows it.

m.logger.Printf("[DEBUG] memberlist: Stream connection %s", LogConn(conn))

metrics.IncrCounterWithLabels([]string{"memberlist", "tcp", "accept"}, 1, m.metricLabels)
Expand Down
51 changes: 51 additions & 0 deletions net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,54 @@ func TestHandleCommand(t *testing.T) {
m.handleCommand(nil, &net.TCPAddr{Port: 12345}, time.Now())
require.Contains(t, buf.String(), "missing message type byte")
}

func TestHandleConn_NilConnAfterRemoveLabelHeaderFromStream(t *testing.T) {
mockNet := &MockNetwork{}

t1 := mockNet.NewTransport("node1")

m := GetMemberlist(t, func(c *Config) {
c.Transport = t1
})
defer func() {
if err := m.Shutdown(); err != nil {
t.Fatal(err)
}
}()

errConn := &errorReadNetConn{
closed: make(chan struct{}),
}
if err := t1.IngestStream(errConn); err != nil {
t.Fatal(err)
}

// The connection must be successfully closed
<-errConn.closed
}

type errorReadNetConn struct {
net.Conn
closed chan struct{}
}

func (c *errorReadNetConn) LocalAddr() net.Addr {
return &MockAddress{"fake:0", "fake"}
}

func (c *errorReadNetConn) RemoteAddr() net.Addr {
return &MockAddress{"fake:0", "fake"}
}

func (c *errorReadNetConn) SetDeadline(t time.Time) error {
return nil
}

func (c *errorReadNetConn) Read(b []byte) (n int, err error) {
return 0, fmt.Errorf("test read error")
}

func (c *errorReadNetConn) Close() error {
close(c.closed)
return nil
}
Loading