Skip to content

fix(ocpp): run charger callbacks after the slot lock is released - #1892

Merged
frahlg merged 2 commits into
masterfrom
e2-ocpp-no-callbacks-under-slot-lock
Oct 27, 2026
Merged

frahlg merged 2 commits into
masterfrom
e2-ocpp-no-callbacks-under-slot-lock

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Item E2 of the stable plan.

Problem

boundCall held a charger's slot lock while its message handler ran (socket_sessions.go:242-253). On BootNotification that handler calls identityReported, which main.go wires to registerOCPPDevice, a SQLite write (identity.go:102, main.go:1494-1496). guardedServer.Write takes 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 exceeded after 1 s. After 20 queued sends, Server.Command blocks 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

  • boundCall guards state only. Callback work a handler queues through socketSessions.later runs after the unlock. identityReported, vehicleIdentified and chargingNeeds go through it. Outside a bound call they still run at once.
  • attemptLimit sends SetChargingProfile from a goroutine, so a command always returns on its context or its timeout.
  • Each charger holds at most one unanswered charging profile. A one-place slot admits a profile from submission until ocpp-go calls back (answer, its own 30 s timeout, write error or disconnect), or until the send fails. Without it, a stalled pump kept one profile per control tick and replayed every obsolete limit, one round trip each, before the current one, so a fuse cut waited behind them. A command that cannot take the slot before its deadline returns ctx.Err(), which core reads as unconfirmed, not as a refusal (isCommandRefusal).

Tests

  • TestStalledBootCallbackDoesNotBlockCommands is 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.
  • TestStalledPumpDoesNotReplayObsoleteProfiles stalls 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 returns context.DeadlineExceeded, at most one stalled profile reaches the charger, and the command after the stall lands at 6 A.
  • TestStalledVehicleCallbackCanCommandItsCharger and TestLaterRunsAfterTheSlotIsFree come from review: a stalled callback can command its own charger, and work queued through later runs once, after unlock.
  • cd go && go test -race -count=5 ./internal/ocpp/: ok
  • go test -race ./internal/ocpp/... ./cmd/ftw/ ./test/e2e/: ok
  • go vet ./internal/ocpp/ ./cmd/ftw/ and go build ./...: ok

Risk

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

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]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T13:30:22.624518Z 7d5fc2f PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread go/internal/ocpp/control.go Outdated
Comment on lines +294 to +299
go func() {
if version == Version201 {
sent <- s.sendProfileV201(alias, amps, numberPhases, connectorID, done)
return
}
sent <- s.sendProfileV16(alias, amps, numberPhases, connectorID, done)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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]>
@frahlg
frahlg merged commit 0d54327 into master Oct 27, 2026
23 checks passed
@frahlg
frahlg deleted the e2-ocpp-no-callbacks-under-slot-lock branch November 11, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant