Skip to content

Commit

Permalink
opt: close file descriptor after OnClose()
Browse files Browse the repository at this point in the history
Fixes #621
  • Loading branch information
panjf2000 committed Jul 3, 2024
1 parent 8a80aaf commit b7aef26
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(" | ")

Check warning on line 271 in eventloop_unix.go

View check run for this annotation

Codecov / codecov/patch

eventloop_unix.go#L268-L271

Added lines #L268 - L271 were not covered by tests
}
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())

Check warning on line 276 in eventloop_unix.go

View check run for this annotation

Codecov / codecov/patch

eventloop_unix.go#L274-L276

Added lines #L274 - L276 were not covered by tests
}
if errStr.Len() > 0 {
if rerr != nil {
rerr = errors.New(rerr.Error() + " & " + err1.Error())
el.getLogger().Errorf(strings.TrimSuffix(errStr.String(), " | "))

Check warning on line 280 in eventloop_unix.go

View check run for this annotation

Codecov / codecov/patch

eventloop_unix.go#L280

Added line #L280 was not covered by tests
} else {
rerr = err1
rerr = errors.New(strings.TrimSuffix(errStr.String(), " | "))

Check warning on line 282 in eventloop_unix.go

View check run for this annotation

Codecov / codecov/patch

eventloop_unix.go#L282

Added line #L282 was not covered by tests
}
}

el.connections.delConn(c)
if el.eventHandler.OnClose(c, err) == Shutdown {
rerr = errorx.ErrEngineShutdown
}
c.release()

return
Expand Down

0 comments on commit b7aef26

Please sign in to comment.