Skip to content
9 changes: 8 additions & 1 deletion internal/engine/iouring/close_accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestFinishCloseCountsAMissingConnState(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
fd, other := socketPairFDs(t)
defer func() { _ = unix.Close(other) }()
fdID := fdIdentity1242(t, fd)
// Non-blocking so the close path's recv drain can never park on
// a socketpair that has no data and a peer still open.
_ = unix.SetNonblock(fd, true)
Expand Down Expand Up @@ -95,7 +96,13 @@ func TestFinishCloseCountsAMissingConnState(t *testing.T) {
if got := w.activeConns.Load(); got != 0 {
t.Errorf("activeConns = %d, want 0", got)
}
if !tc.nilState {
// The nil-state path ends in the close; the live one may stop short
// at the ring this unit Worker does not have. Not closed twice:
// the number is free once the first close is made (celeris#1242).
if tc.nilState && fdID.names(fd) {
t.Error("finishClose left the descriptor open")
}
if fdID.names(fd) {
_ = unix.Close(fd)
}
})
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/iouring/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (w *Worker) RegisterConn(fd int, onRecv func([]byte), onClose func(error))
}
if refused != nil {
w.driverMu.Unlock()
_ = unix.Close(opFD)
closeFD(opFD)
return refused
}
if w.driverConns == nil {
Expand Down Expand Up @@ -790,7 +790,7 @@ func closeDriverFDs(conns []*driverConn, sem chan struct{}) {

// closeFD is the close(2) of one engine descriptor; its error is dropped, as
// everywhere the engine closes a duplicate it has no use for any more.
func closeFD(fd int) { _ = unix.Close(fd) }
func closeFD(fd int) { _ = closeFDErr(fd) }

// closeUnderBound runs closeFn(fd) holding a token of sem: it waits for one,
// and gives it back when closeFn returns. A goroutine waiting for a token
Expand Down
8 changes: 7 additions & 1 deletion internal/engine/iouring/driver_unregister_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,11 @@ func TestDriverFailureCloseCompletionSparesConnOnReusedNumber(t *testing.T) {
n.fd, n.peer = nonblockSocketPair(t)
defer n.closePeer()
if n.fd != a.fd {
// A's number is free (closed above): dup3 over a number that is open
// closes whatever took it, so only a free one is taken (celeris#1242).
if fdOpen704(a.fd) {
t.Fatalf("PREMISE: descriptor %d, the number of A, was taken by another open", a.fd)
}
if err := unix.Dup3(n.fd, a.fd, unix.O_CLOEXEC); err != nil {
t.Fatalf("dup3: %v", err)
}
Expand All @@ -662,10 +667,11 @@ func TestDriverFailureCloseCompletionSparesConnOnReusedNumber(t *testing.T) {
select {
case err := <-n.closed:
t.Errorf("N: onClose(%v) fired, with N never unregistered: a close completion of A's reached it (celeris#707)", err)
_ = unix.Close(n.fd)
default:
// Closes n.fd and its peer.
n.unregisterAndWait(t, wl)
}
_ = unix.Close(n.fd)
q.d.unregisterAndWait(t, wl)
p.d.unregisterAndWait(t, wl)
}
Expand Down
40 changes: 40 additions & 0 deletions internal/engine/iouring/fd_close_seam_1242.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build linux

package iouring

import (
"sync/atomic"

"golang.org/x/sys/unix"
)

// fdCloseSeam is a test seam (celeris#1242): when set, every close(2) the
// engine makes of a descriptor it owns (closeFD, closeFDErr) goes through it
// instead of unix.Close, on whichever goroutine closes. A test makes it take
// the number out of use in the same step as the close (dup3 of a placeholder
// over it, so no other open can get the number in between) and then reports a
// second close of a number nothing could have re-opened, which is a stale
// close in a real process: the number may by then name another connection or
// a sibling's listener. Several workers call it, so a test keeps its state
// atomic. Nil in production, where each close costs one load of this
// variable. An atomic.Pointer, as ringWaitSeam is, because a worker an
// earlier test left running may still read it when a later test sets or
// restores it (celeris#1229).
var fdCloseSeam atomic.Pointer[func(fd int) error]

// closeFDErr is close(2) of one descriptor number the engine owns, through
// fdCloseSeam. Every close of an engine-owned descriptor in this package goes
// through here (or closeFD, which drops the error): a bare unix.Close is
// invisible to the seam test of celeris#1242. Two closes are outside it, by
// design. The wake eventfd's handle (internal/wakefd) retires the number
// under its lock before it closes it (celeris#655) and so cannot close it
// twice. A hijacked connection's descriptor is closed through the *os.File
// that net.FileConn was given (hijackConn): routing that close through
// closeFD would leave the file's finalizer to close the number again at a
// later GC, after it has been reused, which is the bug this seam hunts.
func closeFDErr(fd int) error {
if h := fdCloseSeam.Load(); h != nil {
return (*h)(fd)
}
return unix.Close(fd)
}
8 changes: 4 additions & 4 deletions internal/engine/iouring/fd_lifetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (w *Worker) closedFDNamed(cs *connState) bool {
// release closes it when cs goes, or earlier, once only a SEND_ZC
// notification is still owed (celeris#798). Worker thread only.
func (w *Worker) releaseKeptFD(e *pendingReleaseEntry) {
_ = unix.Close(int(e.fd))
closeFD(int(e.fd))
e.holdsFD = false
w.closeFDOwed--
}
Expand All @@ -558,7 +558,7 @@ func keptFD(fd int, owed bool) int {
// drainPendingRelease closes at the last owed op's terminal CQE.
func closeUnlessOwed(fd int, owed bool) {
if !owed {
_ = unix.Close(fd)
closeFD(fd)
}
}

Expand Down Expand Up @@ -728,7 +728,7 @@ func (w *Worker) endOwedOpsAtShutdown() {
w.staleConnCQE(c, fd, ud)
case udAccept:
if c.Res >= 0 && !w.fixedFiles {
_ = unix.Close(int(c.Res))
closeFD(int(c.Res))
}
// No more completions from this accept: it is over, and
// has let go of the listen socket's file. A multishot
Expand Down Expand Up @@ -777,7 +777,7 @@ func (w *Worker) endOwedOpsAtShutdown() {
}
for i := range w.pendingRelease {
if e := &w.pendingRelease[i]; e.holdsFD {
_ = unix.Close(int(e.fd))
closeFD(int(e.fd))
e.holdsFD = false
}
}
Expand Down
Loading
Loading