diff --git a/internal/handoff/give_nonblocking_test.go b/internal/handoff/give_nonblocking_test.go new file mode 100644 index 000000000..0f9c9a0a6 --- /dev/null +++ b/internal/handoff/give_nonblocking_test.go @@ -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)") + } +} diff --git a/internal/handoff/handoff_unix.go b/internal/handoff/handoff_unix.go index 8b928338c..34763a42f 100644 --- a/internal/handoff/handoff_unix.go +++ b/internal/handoff/handoff_unix.go @@ -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 }