Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race of sub.seq during replay -> live tail cutover #43

Closed

Conversation

uniphil
Copy link

@uniphil uniphil commented Feb 5, 2025

Prevent a data race that leads to out-of-order events being delivered during cutover from replay to live-tailing, partially resolving #42.

emitToSubscriber reads and writes sub.seq, but does not itself lock the subscriber's sub.lk. It can't, because its main caller, the server Emit function, takes that lock across its call to emitToSubscriber.

The other caller is the replay goroutine, which runs concurrently, and did not take the lock, leaving sub.seq exposed to races when both it and Emit ran concurrently (for ~0.5s during cutover).

This change makes the replay goroutine take the lock before calling emitToSubscriber, protecting sub.seq from this race.


I've verified locally that jetstream still works and cuts over, and created a pair of repro scripts that (try to) trigger the data race: https://gist.github.com/uniphil/346acb62088022729394d2324bf2ad8a

With the changes from this PR, the local repro goes from almost always triggering the race to never in all tests so far -- see sample output in the linked gist. I'll verify prod again if this gets merged and deployed 🙂

Prevent a data race that leads to out-of-order events being delivered to
jetstream subscribers during cutover from replay to live-tailing,
partially resolving [bluesky-social#42](bluesky-social#42).

`emitToSubscriber` reads and writes `sub.seq`, but it does not itself
lock the subscriber's `sub.lk`. It can't, because its main caller, the
server `Emit` function, takes that lock across its call.

The other caller is the replay goroutine, which runs concurrently, and
did not take the lock, leaving `sub.seq` exposed to races when both it
and `Emit` ran concurrently (for ~0.5s during cutover).

This change makes the replay goroutine take the lock before calling
`emitToSubscriber`, protecting `sub.seq` from this race.
@@ -215,6 +215,8 @@ func (s *Server) HandleSubscribe(c echo.Context) error {
go func() {
for {
lastSeq, err := s.Consumer.ReplayEvents(ctx, sub.compress, *sub.cursor, playbackRateLimit, func(ctx context.Context, timeUS int64, did, collection string, getEventBytes func() []byte) error {
sub.lk.Lock()
defer sub.lk.Unlock()
return emitToSubscriber(ctx, log, sub, timeUS, did, collection, true, getEventBytes)
Copy link
Author

Choose a reason for hiding this comment

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

hmm. emitToSubscriber with playback = true would do a blocking send on the outbox while holding the subscriber's lock. could this hold up the Emit semaphore when it tries to acquire the sub's lock in the overlap time of cutover?

@uniphil
Copy link
Author

uniphil commented Feb 18, 2025

closing for #45

@uniphil uniphil closed this Feb 18, 2025
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