Skip to content

fix(relay): never panic delivering to a closed receiver#344

Open
wolfy-j wants to merge 1 commit into
mainfrom
fix/relay-closed-channel
Open

fix(relay): never panic delivering to a closed receiver#344
wolfy-j wants to merge 1 commit into
mainfrom
fix/relay-closed-channel

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Problem

The relay Mailbox delivers to receiver channels it does not own — the attached process owns its channel and closes it on teardown, while Detach/the Attach cancel only remove the map entry. A delivery racing that close hit panic: send on closed channel in Mailbox.deliver and killed the worker goroutine (process crash under load). The same hazard existed on shutdown: NewMailbox closed the job queues, so an in-flight Send could send on a closed queue.

Fix

  • deliverTo performs the receiver-channel send under a recover — a closed receiver means the process is gone, so the package is dropped and the worker survives.
  • Stop closing the job queues on shutdown; workers now exit on ctx.Done() instead of ranging a closed channel, so Send can't race a closed queue either.

Test

TestMailbox_ClosedReceiverDoesNotKillWorker reproduces the crash: a send to a closed receiver must not stop a subsequent delivery to a live receiver on the same worker. Full package go test -race + go vet clean.

Surfaced in production under heavy process churn (containers starting/dying → mailboxes opening/closing).

The attached process owns its receiver channel and closes it on teardown
(Detach only removes the map entry), so a delivery racing that close hit
'send on closed channel' and killed the mailbox worker. Guard the receiver send
with recover (a closed receiver = gone process, drop the package) and stop
closing the job queues on shutdown (workers exit on ctx.Done) so Send can't race
a closed queue either. Test: a closed-receiver send must not stop a live one.
@wolfy-j wolfy-j requested a review from skhaz June 15, 2026 02:33
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.

2 participants