From b7aef26f9c3cff7ccd9677b52158cbc326637a43 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Wed, 3 Jul 2024 10:21:53 +0800 Subject: [PATCH] opt: close file descriptor after OnClose() Fixes #621 --- eventloop_unix.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/eventloop_unix.go b/eventloop_unix.go index 71361100d..617cf9162 100644 --- a/eventloop_unix.go +++ b/eventloop_unix.go @@ -244,6 +244,11 @@ func (el *eventloop) close(c *conn, err error) (rerr error) { return // ignore stale connections } + el.connections.delConn(c) + if el.eventHandler.OnClose(c, err) == Shutdown { + rerr = errorx.ErrEngineShutdown + } + // Send residual data in buffer back to the remote before actually closing the connection. for !c.outboundBuffer.IsEmpty() { iov, _ := c.outboundBuffer.Peek(0) @@ -258,22 +263,26 @@ func (el *eventloop) close(c *conn, err error) (rerr error) { } err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd) + var errStr strings.Builder if err0 != nil { - rerr = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v", c.fd, el.idx, err0) + err0 = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v", + c.fd, el.idx, os.NewSyscallError("delete", err0)) + errStr.WriteString(err0.Error()) + errStr.WriteString(" | ") } if err1 != nil { - err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v", c.fd, el.idx, os.NewSyscallError("close", err1)) + err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v", + c.fd, el.idx, os.NewSyscallError("close", err1)) + errStr.WriteString(err1.Error()) + } + if errStr.Len() > 0 { if rerr != nil { - rerr = errors.New(rerr.Error() + " & " + err1.Error()) + el.getLogger().Errorf(strings.TrimSuffix(errStr.String(), " | ")) } else { - rerr = err1 + rerr = errors.New(strings.TrimSuffix(errStr.String(), " | ")) } } - el.connections.delConn(c) - if el.eventHandler.OnClose(c, err) == Shutdown { - rerr = errorx.ErrEngineShutdown - } c.release() return