Skip to content

fix: don't panic when shutdown races an in-flight publish - #2

Merged
BK1031 merged 1 commit into
mainfrom
bk1031/fix-publish-shutdown-panic
Aug 29, 2026
Merged

BK1031 merged 1 commit into
mainfrom
bk1031/fix-publish-shutdown-panic

Conversation

@BK1031

@BK1031 BK1031 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Seen on the car — every docker compose down ends in:

panic: send on closed channel
relay/mqtt.(*publishQueue).enqueue      mqtt.go:198
relay/mqtt.PublishCloud                 mqtt.go:229
relay/service.PublishData               can.go:65
relay/service.readFrames                socketcan_linux.go:82

The CAN readers have no shutdown path — they block in recv until the process exits — so they keep publishing throughout shutdown. Disconnect closed the queue channel out from under them, and a reader that reached the send first killed the process. At ~200 frames/s that window is hit reliably, so the relay panicked on every stop rather than exiting cleanly.

Fix mirrors what StopDBQueue already does for the identical race on the database queue: close under a write lock, hold the read lock across the send.

Reproduced the race directly (8 writers against a concurrent close, 200 iterations, -race): panics reliably before, clean after. That harness was scratch and isn't committed.

Note this is a clean-shutdown bug only — it does not drop frames while running, and StopDBQueue runs before Disconnect, so the final batch still reaches SQLite before the panic could occur.

The CAN readers have no shutdown path — they block in recv until the
process exits — so they keep calling PublishData throughout shutdown.
Disconnect closed the publish queue's channel out from under them, and a
reader that reached the send first took the process down with "send on
closed channel". On a live bus at ~200 frames/s that window is hit
reliably, so every stop panicked instead of exiting cleanly.

Close under a write lock and hold the read lock across the send, the same
way StopDBQueue already guards the database queue against its identical
race.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 29, 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-08-29T20:24:35.943132Z efadfc9 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.

@BK1031
BK1031 merged commit 038dcf8 into main Aug 29, 2026
2 checks passed
@BK1031
BK1031 deleted the bk1031/fix-publish-shutdown-panic branch August 29, 2026 20:22
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