handoff: Give no longer leaves the handed socket's listener blocking (#4107)
A restart that hands the gateway over reads the copy of its listening socket (ListenerFile) to send it along (Give). The copy shares the listener's open file description (TCPListener.File dups the fd), and os.File.Fd puts such a description in blocking mode: a socket's File hands out expecting its Fd to be read, and SetBlocking then takes the O_NONBLOCK flag off the description the copy and the listener share. The gateway's own listener went blocking with it: its accept slept in the syscall once nothing came queued, where Close cannot reach it — the pollDesc evict wakes I/O waiting in the netpoller, not a syscall, and the listener's own FD still counted as non-blocking, so Close waited on its condition variable for that accept to return. stopAccepting waited there, ListenAndServe never returned, and the ending magpie hung: under a full-suite load TestRestartHandsTheGatewayOver sat 43 minutes with the handover goroutine in semacquire inside gateway.(*Server).stopAccepting (the 2701s timeout, #4107). Alone it passed, as the accept only sleeps there when it comes around with no connection queued, which a quiet machine rarely gives. Give now takes the number to send from SyscallConn, which reads it without the blocking switch. Red before, green after: TestGiveKeepsTheListenerNonblocking — a listener handed over with Give keeps its O_NONBLOCK (sub-second, deterministic; on the old Give it fails "Give left the listener in blocking mode (os.File.Fd on the copy)"). Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package handoff
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Give reads the number of f to send it along (unix.UnixRights), and f is
|
||||
// the copy of a listening socket (ListenerFile, from TCPListener.File)
|
||||
// that shares the listener's open file description. os.File.Fd puts such
|
||||
// a description in blocking mode (the net package hands a socket's File
|
||||
// out expecting that), which left the gateway's own listener blocking: its
|
||||
// accept then slept in the syscall once nothing connected, past Close,
|
||||
// so a handover's end never finished (the full-suite hang of
|
||||
// TestRestartHandsTheGatewayOver, #4107). The listener keeps its
|
||||
// non-blocking mode across Give.
|
||||
func TestGiveKeepsTheListenerNonblocking(t *testing.T) {
|
||||
ln, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
f, err := ln.File()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
la := &net.UnixAddr{Name: filepath.Join(t.TempDir(), "s"), Net: "unix"}
|
||||
ul, err := net.ListenUnix("unix", la)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ul.Close()
|
||||
client, err := net.DialUnix("unix", nil, la)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
server, err := ul.AcceptUnix()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
c := &Conn{c: server, r: bufio.NewReader(server)}
|
||||
|
||||
nonblocking := func() bool {
|
||||
t.Helper()
|
||||
rc, err := ln.SyscallConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
set := false
|
||||
var ferr error
|
||||
rc.Control(func(p uintptr) {
|
||||
flags, err := unix.FcntlInt(p, unix.F_GETFL, 0)
|
||||
if err != nil {
|
||||
ferr = err
|
||||
return
|
||||
}
|
||||
set = flags&unix.O_NONBLOCK != 0
|
||||
})
|
||||
if ferr != nil {
|
||||
t.Fatal(ferr)
|
||||
}
|
||||
return set
|
||||
}
|
||||
if !nonblocking() {
|
||||
t.Fatal("the listener isn't in non-blocking mode to begin with")
|
||||
}
|
||||
if err := c.Give(f, Meta{Addr: ln.Addr().String(), PID: os.Getpid()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !nonblocking() {
|
||||
t.Fatal("Give left the listener in blocking mode (os.File.Fd on the copy): its accept sleeps in the syscall past Close, and the handover never finishes (#4107)")
|
||||
}
|
||||
}
|
||||
@@ -154,11 +154,25 @@ func (o *Offer) Accept(ctx context.Context) (*Conn, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Give sends the listening socket f, with m.
|
||||
// Give sends the listening socket f, with m. The number for the send
|
||||
// comes from SyscallConn, not Fd: f is a listener's copy sharing the
|
||||
// listener's open file description (ListenerFile), and Fd puts such a
|
||||
// description in blocking mode (a socket's File hands out expecting its
|
||||
// Fd to be read) — the gateway's own accept would then sleep in the
|
||||
// syscall once nothing connected, past Close, and the handover would
|
||||
// never finish (#4107).
|
||||
func (c *Conn) Give(f *os.File, m Meta) error {
|
||||
b, _ := json.Marshal(m)
|
||||
b = append(b, '\n')
|
||||
n, oobn, err := c.c.WriteMsgUnix(b, unix.UnixRights(int(f.Fd())), nil)
|
||||
rc, err := f.SyscallConn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var fd int
|
||||
if err := rc.Control(func(p uintptr) { fd = int(p) }); err != nil {
|
||||
return err
|
||||
}
|
||||
n, oobn, err := c.c.WriteMsgUnix(b, unix.UnixRights(fd), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user