Skip to content

fix: race between publisher close and publish - #1169

Open
alexluong wants to merge 2 commits into
mainfrom
fix/basepublisher-close-race
Open

alexluong wants to merge 2 commits into
mainfrom
fix/basepublisher-close-race

Conversation

@alexluong

@alexluong alexluong commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes a race where a publish can start after Close has returned.

BasePublisher.StartPublish checks closed and then calls active.Add(1). StartClose sets closed and then calls active.Wait(). The check and the Add aren't one step, so a Close can land between them: Wait returns with the counter at 0, the provider closes its client, and the publish goes ahead on the closed client. The race detector reports this as Add concurrent with Wait, and TestClosePublisherDuringConcurrentPublish fails on it now and then under -race.

Outside tests, it needs the registry to evict a cached publisher in the short window between resolving it and starting the publish, so it is rare. When it does happen, the publish errors and the message is retried.

The check and the Add now run under a read lock, and setting closed takes the write lock. A publish either registers before Close starts waiting, or sees closed and returns ErrPublisherClosed. Wait stays outside the lock, so in-flight publishes still finish.

Stress-tested under -race: fails on main, passes with this change.

🤖 Generated with Claude Code

alexluong and others added 2 commits October 25, 2026 03:08
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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