Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: close file descriptor after OnClose() #622

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}

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
Loading