Repository navigation
fix(ocpp): run charger callbacks after the slot lock is released - #1892
Conversation
boundCall held a charger's slot lock while the BootNotification handler called identityReported, which main.go wires to a SQLite device write. Every write to that charger takes the same lock, so one stalled write blocked the SDK's single request pump, then every charger's commands, and once 20 requests queued, Server.Command past its deadline. Callback work a handler queues now runs after the unlock. The charging profile send runs on its own goroutine, so a command always returns by its context. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7d5fc2f02c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| go func() { | ||
| if version == Version201 { | ||
| sent <- s.sendProfileV201(alias, amps, numberPhases, connectorID, done) | ||
| return | ||
| } | ||
| sent <- s.sendProfileV16(alias, amps, numberPhases, connectorID, done) |
There was a problem hiding this comment.
Bound outstanding profile sends when the SDK queue stalls
When the SDK request queue stays full, cancellation now returns from attemptLimit but leaves this goroutine blocked inside sendProfileV16 or sendProfileV201, neither of which receives the context. The periodic loadpoint dispatcher can therefore continue creating another blocked sender on each retry, turning the SDK's bounded queue into an unbounded goroutine backlog. If the queue recovers, those abandoned sends also continue submitting obsolete charging limits before current commands can get through. Bound outstanding sends and discard expired work before submission rather than detaching an uncancellable send for every attempt.
Useful? React with 👍 / 👎.
attemptLimit sent each profile from a new goroutine that ignored ctx. While the request pump was stalled, every control tick left another profile queued, and once the pump cleared the charger received every obsolete limit, one round trip each, before the current one. A fuse cut waited behind them. A one-place slot per charger now admits a single profile from submission until the library calls back (answer, its own timeout, write error or disconnect), or until the send fails. A command that cannot take the slot before its deadline returns ctx.Err(), so core reads it as unconfirmed, not as a refusal. TestStalledPumpDoesNotReplayObsoleteProfiles fails without this (20 goroutines left behind, 41 profiles delivered) and passes with it. Also adds the reviewer's tests for a re-entrant vehicle callback and for work queued through later during a boundCall. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Item E2 of the stable plan.
Problem
boundCallheld a charger's slot lock while its message handler ran (socket_sessions.go:242-253). On BootNotification that handler callsidentityReported, which main.go wires toregisterOCPPDevice, a SQLite write (identity.go:102, main.go:1494-1496).guardedServer.Writetakes the same lock, and ocpp-go has one request pump per server.Scenario: two chargers, "carport" and "garage". The device write for garage's second boot stalls. The pump blocks writing to garage, so carport's next command fails with
context deadline exceededafter 1 s. After 20 queued sends,Server.Commandblocks 2 s past its 50 ms deadline, because the profile send ignored ctx. The control loop calls it on every tick, so it freezes for as long as the write stalls.Change
boundCallguards state only. Callback work a handler queues throughsocketSessions.laterruns after the unlock.identityReported,vehicleIdentifiedandchargingNeedsgo through it. Outside a bound call they still run at once.attemptLimitsends SetChargingProfile from a goroutine, so a command always returns on its context or its timeout.ctx.Err(), which core reads as unconfirmed, not as a refusal (isCommandRefusal).Tests
TestStalledBootCallbackDoesNotBlockCommandsis the audit's repro test (caa-85), renamed. It fails on master (command 20 still blocked 2 s after its 50 ms deadline) and passes here.TestStalledPumpDoesNotReplayObsoleteProfilesstalls the pump and sends 40 carport commands with 30 ms deadlines. Without the slot it fails: 20 goroutines left behind and 41 profiles delivered. With it, every stalled tick returnscontext.DeadlineExceeded, at most one stalled profile reaches the charger, and the command after the stall lands at 6 A.TestStalledVehicleCallbackCanCommandItsChargerandTestLaterRunsAfterTheSlotIsFreecome from review: a stalled callback can command its own charger, and work queued throughlaterruns once, after unlock.cd go && go test -race -count=5 ./internal/ocpp/: okgo test -race ./internal/ocpp/... ./cmd/ftw/ ./test/e2e/: okgo vet ./internal/ocpp/ ./cmd/ftw/andgo build ./...: okRisk
Medium, small diff. A callback now runs after the handler's state changes are visible to other goroutines, not inside the lock. Callbacks on one charger still run in message order, on the goroutine that handled the message, before the reply goes back.
A send that blocks in the SDK leaves at most one goroutine per charger, until the pump drains. When the stall ends, that charger may first receive the one limit that was waiting, then the current limit on the next tick. A charger that never answers holds its slot until ocpp-go's 30 s timeout. The per-client queue already made later profiles wait that long, so this adds no delay.
🤖 Generated with Claude Code