Fix client dispatcher deadlock on SendRequest during stop#6
Open
andig wants to merge 1 commit into
Open
Conversation
DefaultClientDispatcher.SendRequest did a blocking send on the requestChannel notification channel while the caller (chargePoint.SendRequestAsync via the callback queue) holds the callbacks mutex. Once the message pump has stopped draining the channel, that send blocks forever, so the mutex is never released and the concurrent clearCallbacks on teardown deadlocks. The send could also panic on a closed channel after Stop. The notification channel is only a wake-up doorbell; the request enqueued in requestQueue is the source of truth and the pump re-checks the queue on every wake. Make the doorbell send non-blocking and add a stopped signal so SendRequest bails out once the dispatcher is stopping instead of blocking or sending on a closed channel. Adds a regression test that hangs without the fix.
Coverage Report for CI Build 27122869506Coverage increased (+0.01%) to 82.919%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DefaultClientDispatcher.SendRequestdoes a blocking send on therequestChannelnotification channel:The caller chain holds the callback-queue mutex across this send (
chargePoint.SendRequestAsync→callbackqueue.TryQueueholdscallbacksMutex.Lock()while runningtry()→SendRequest). Once the message pump has stopped drainingrequestChannel(during/after teardown), this send blocks forever, so the callbacks mutex is never released and the concurrentclearCallbackson stop (asyncCallbackHandler←<-stopC) deadlocks on the same mutex. The send can also panic on a closed channel afterStop.This surfaced as a 10-minute hang in a downstream test suite (charge point connect/send/stop cycling), with one goroutine blocked at
dispatcher.goonrequestChannel <- truewhile holding the callbacks mutex, and another blocked acquiring that mutex inclearCallbacks.requestChannelis only a wake-up doorbell — the request enqueued inrequestQueueis the source of truth, and the pump re-checks the queue on every wake (paced byreadyForDispatch/responses, not one dispatch per doorbell). So a dropped doorbell is harmless. This change:select { case d.requestChannel <- true: default: }), soSendRequestnever blocks;stoppedsignal channel (mirroring the server dispatcher'sstoppedC), and bails out ofSendRequestwhen stopping to avoid sending on the closed channel;Stop's write lock) sorequestChannelcannot be closed mid-send.Adds a regression test (
TestClientSendRequestAfterStop) that blocks/fails without the fix and passes with it. Existing./ocppj/...suite stays green.