Repository navigation
Conversation
panjf2000
left a comment
There was a problem hiding this comment.
Please fix the lint issues first.
| efd int // eventfd | ||
| efdBuf []byte // efd buffer to read an 8-byte integer | ||
| wakeupCall int32 | ||
| stopped atomic.Bool // set when Polling returns; Trigger fails afterwards |
| epa *PollAttachment // PollAttachment for waking events | ||
| efdBuf []byte // efd buffer to read an 8-byte integer | ||
| wakeupCall int32 | ||
| stopped atomic.Bool // set when Polling returns; Trigger fails afterwards |
| // | ||
| // Note that asyncTaskQueue is a queue of low-priority whose size may grow large and tasks in it may backlog. | ||
| func (p *Poller) Trigger(priority queue.EventPriority, fn queue.Func, param any) (err error) { | ||
| if p.stopped.Load() { |
There was a problem hiding this comment.
I wonder whether we really need two fields, stopped and done, it looks redundant at first glance. Also, Poller.Close() and the new Poller.retire() seem to overlap, how should we coordinate these two methods?
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Shutdown-state semantics, poller retirement races, and descriptor double-closing remain unresolved.
Review effort: Balanced
Findings: 2
Open (3)
What changed in this PR
Fixes enrollment hangs during shutdown and duplicated socket descriptor leaks.
Changes:
- Adds poller retirement signaling and task cleanup.
- Tightens duplicated-FD ownership and cleanup.
- Adds shutdown and FD lifecycle regression tests.
| File | Description |
|---|---|
pkg/netpoll/poller_shutdown_unix.go |
Adds retirement signaling and queue cleanup. |
pkg/netpoll/poller_shutdown_ultimate_test.go |
Tests optimized poller retirement. |
pkg/netpoll/poller_shutdown_test.go |
Provides shared retirement assertions. |
pkg/netpoll/poller_shutdown_default_test.go |
Tests default poller retirement. |
pkg/netpoll/poller_kqueue_ultimate.go |
Integrates retirement into optimized kqueue. |
pkg/netpoll/poller_kqueue_default.go |
Integrates retirement into default kqueue. |
pkg/netpoll/poller_epoll_ultimate.go |
Integrates retirement into optimized epoll. |
pkg/netpoll/poller_epoll_default.go |
Integrates retirement into default epoll. |
eventloop_unix.go |
Handles registration retirement and unopened descriptors. |
engine_unix.go |
Publishes shutdown earlier. |
client_unix.go |
Prevents enrollment hangs and FD leaks. |
client_shutdown_unix_test.go |
Adds enrollment and descriptor regression tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if !c.opened { | ||
| el.releaseUnopened(c) | ||
| return nil |
| if !p.stopped.CompareAndSwap(false, true) { | ||
| return | ||
| } | ||
| atomic.StoreInt32(&p.wakeupCall, 0) | ||
| p.dropQueue(p.urgentAsyncTaskQueue) |
| // Publish shutdown before waking pollers. Register and Enroll consult this | ||
| // flag; storing it only after Wait left a window where Trigger reported | ||
| // success and the waiter blocked on a task that would never run. | ||
| eng.inShutdown.Store(true) |
3702fef to
e3dfc2d
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #766 +/- ##
==========================================
- Coverage 84.80% 84.20% -0.61%
==========================================
Files 22 22
Lines 2422 2450 +28
==========================================
+ Hits 2054 2063 +9
- Misses 252 264 +12
- Partials 116 123 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| // every Trigger already past the stopped check, and setting stopped under it | ||
| // rejects every later one, so the drain below is final. | ||
| func (p *Poller) retire() { | ||
| p.retireMu.Lock() |
There was a problem hiding this comment.
Why would you replace atomic CAS with RW lock?
There was a problem hiding this comment.
This file seems needless; Let's put these methods in defs_poller.go
|
This PR is marked as stale because it has been open for 21 days with no activity. You should take one of the following actions:
This PR will be automatically closed in 7 days if no further activity occurs. |
|
Updated the PR to The exact application tree was qualified in this fork CI run: all 23 jobs passed, including 18 native Linux/macOS/Windows combinations on Go 1.20 and 1.26. The temporary qualification workflow is not included in this PR. GitHub is holding all seven upstream workflows at |
|
This PR is marked as stale because it has been open for 21 days with no activity. You should take one of the following actions:
This PR will be automatically closed in 7 days if no further activity occurs. |
1. Polling returned on ErrEngineShutdown without draining queued tasks or preventing concurrent Trigger admissions, leaving Enroll blocked on an unclosed channel after Stop. Coordinate Poller admission, retirement, and physical Close under retireMu in defs_poller.go around the done channel and closed flag: Polling retires queues and closes done on exit, Close returns EBUSY while Polling is active and closes underlying descriptors idempotently once Polling has returned or if Polling never started, and Trigger spans queue insertion and wakeup under retireMu while re-arming saturated eventfd edges and resetting wakeupCall on write errors. 2. Track engine shutdown start via stopping before waking pollers while preserving inShutdown as the post-close completion signal, prepare all pollers and listeners before launching reactor goroutines so startup failures clean up descriptors without racing active loops, and transfer duplicated socket ownership in Enroll/enroll through connRegistration until register0 attaches the descriptor (leaving unopened connections untouched in eventloop.close).
Add unit and integration tests covering Poller retirement races, active Close rejection (EBUSY), wakeup ordering and saturated eventfd progress, zero-value Poller safety, connRegistration descriptor ownership across retirement and submission errors, engine startup failure cleanup under EMFILE/EPERM, closed-connection wake ordering, and byteslice pool independence, and adapt the netpoll example to both build variants.
|
Apologies for omitting explicit inline replies when pushing the previous revision. The branch has been squashed down to 2 commits on top of
|
eabca9e to
9b18722
Compare


Description
This PR fixes two related lifecycle issues:
Enrollblocking indefinitely when called during or after engine shutdown.Enrollfails before the connection is opened.Fixes #765
Root Cause Analysis & Solution
Poller retirement and task cleanup:
ErrEngineShutdown,wakeupCallwas left set, and abandoned tasks were neither executed nor returned to the pool.inShutdownis now published before waking pollers.TriggerreturnsErrEngineShutdownoncestoppedis set.Done()channel is signaled upon poller retirement, andawaitPollerRegisterensures waiting callers wake up cleanly rather than hanging.FD Leak Prevention:
socket.Dupis tracked with a singledeferuntil poller registration succeeds.closeexecutes on an unopened connection,releaseUnopenedreleases the descriptor and resetsfd = -1to prevent double-closes.How Has This Been Tested?
Regression tests added:
TestTriggerAfterPollingReturnsinpkg/netpoll/TestEnrollAfterStopReturnsinclient_shutdown_unix_test.goTestCloseUnopenedReleasesFDinclient_shutdown_unix_test.goRan:
Both PASS.