mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-13 22:47:30 +00:00
net/netutil: fix regression where peerapi would get closed after 1st req
I introduced a bug in 8fe503057d
when unifying oneConnListener
implementations.
The NewOneConnListenerFrom API was easy to misuse (its Close method
closes the underlying Listener), and we did (via http.Serve, which
closes the listener after use, which meant we were close the peerapi's
listener, even though we only wanted its Addr)
Instead, combine those two constructors into one and pass in the Addr
explicitly, without delegating through to any Listener.
Change-Id: I061d7e5f842e0cada416e7b2dd62100d4f987125
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
e31d68d64e
commit
bb94561c96
@@ -8,36 +8,51 @@ package netutil
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// NewOneConnListener returns a net.Listener that returns c on its first
|
||||
// Accept and EOF thereafter. The Listener's Addr is a dummy address.
|
||||
func NewOneConnListener(c net.Conn) net.Listener {
|
||||
return NewOneConnListenerFrom(c, dummyListener{})
|
||||
}
|
||||
|
||||
// NewOneConnListenerFrom returns a net.Listener wrapping ln where
|
||||
// its Accept returns c on the first call and io.EOF thereafter.
|
||||
func NewOneConnListenerFrom(c net.Conn, ln net.Listener) net.Listener {
|
||||
return &oneConnListener{c, ln}
|
||||
// NewOneConnListener returns a net.Listener that returns c on its
|
||||
// first Accept and EOF thereafter.
|
||||
//
|
||||
// The returned Listener's Addr method returns addr if non-nil. If nil,
|
||||
// Addr returns a non-nil dummy address instead.
|
||||
func NewOneConnListener(c net.Conn, addr net.Addr) net.Listener {
|
||||
if addr == nil {
|
||||
addr = dummyAddr("one-conn-listener")
|
||||
}
|
||||
return &oneConnListener{
|
||||
addr: addr,
|
||||
conn: c,
|
||||
}
|
||||
}
|
||||
|
||||
type oneConnListener struct {
|
||||
addr net.Addr
|
||||
|
||||
mu sync.Mutex
|
||||
conn net.Conn
|
||||
net.Listener
|
||||
}
|
||||
|
||||
func (l *oneConnListener) Accept() (c net.Conn, err error) {
|
||||
c = l.conn
|
||||
func (ln *oneConnListener) Accept() (c net.Conn, err error) {
|
||||
ln.mu.Lock()
|
||||
defer ln.mu.Unlock()
|
||||
c = ln.conn
|
||||
if c == nil {
|
||||
err = io.EOF
|
||||
return
|
||||
}
|
||||
err = nil
|
||||
l.conn = nil
|
||||
ln.conn = nil
|
||||
return
|
||||
}
|
||||
|
||||
func (ln *oneConnListener) Addr() net.Addr { return ln.addr }
|
||||
|
||||
func (ln *oneConnListener) Close() error {
|
||||
ln.Accept() // guarantee future call returns io.EOF
|
||||
return nil
|
||||
}
|
||||
|
||||
type dummyListener struct{}
|
||||
|
||||
func (dummyListener) Close() error { return nil }
|
||||
|
Reference in New Issue
Block a user