Shutdown returns from poller without unblocking Enroll, and failed Enroll leaks duplicated socket fd
Summary
Two related lifecycle issues affect Client.EnrollContext / EventLoop.enroll during shutdown or failed registration:
Poller shutdown drops tasks without unblocking Enroll:
Pollingreturns onErrEngineShutdownbefore clearingwakeupCalland before draining the queued tasks. A subsequentTriggertreats the failed compare-and-swap as success, andEnrollblocks indefinitely on<-connOpened, which is never closed. Furthermore,inShutdownwas only stored after waiting for event loops to close.Duplicated socket file descriptor leak:
socket.Duphands ownership of the duplicated descriptor toEnroll, but error returns before poller registration andeventloop.closeon an unopened connection drop the descriptor without callingunix.Close. This leads to file descriptor exhaustion (EMFILE).
Proposed Fix
- Publish
inShutdownbefore waking pollers, failTriggeroncePollinghas retired, and awaitDone()channel. - Manage duplicated file descriptor with
deferuntil registration takes ownership, and close viareleaseUnopenedif unopened.
A complete tested fix is submitted concurrently in a Pull Request.
Source: panjf2000/gnet