From 547713bc958cd7fa22891779a79f91c810a79a09 Mon Sep 17 00:00:00 2001 From: wangzhuowei Date: Wed, 31 Jan 2024 19:57:50 +0800 Subject: [PATCH] fix: connection leak when poller close connection but onRequest callback just finished --- connection_onevent.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/connection_onevent.go b/connection_onevent.go index 6f055f37..edb73133 100644 --- a/connection_onevent.go +++ b/connection_onevent.go @@ -210,7 +210,7 @@ func (c *connection) onProcess(isProcessable func(c *connection) bool, process f } process(c) } - // Handling callback if connection has been closed. + // handling callback if connection has been closed. if closedBy != none { // if closed by user when processing, it "may" needs detach needDetach := closedBy == user @@ -223,7 +223,17 @@ func (c *connection) onProcess(isProcessable func(c *connection) bool, process f return } c.unlock(processing) - // Double check when exiting. + // Note: Poller's closeCallback call will try to get processing lock failed but here already neer to unlock processing. + // So here we need to check connection state again, to avoid connection leak + // double check close state + if c.status(closing) != 0 && c.lock(processing) { + // poller will get the processing lock failed, here help poller do closeCallback + // fd must already detach by poller + c.closeCallback(false, false) + panicked = false + return + } + // double check isProcessable if isProcessable(c) && c.lock(processing) { goto START }