Repository navigation
fix(sessions): Lock pending aggregates in SessionFlusher#add_session - #3172
Merged
Merged
Conversation
add_session read and wrote @pending_aggregates without holding @Mutex, while the flush swap replaced the hash under it. A swap between the two lookups made the second one return nil and raise NoMethodError from Hub#end_session, and swaps at other points dropped sessions. Fixes #3038 Co-Authored-By: Johannes Grin Dreadböck <114897+dingsdax@users.noreply.github.com>
sl0thentr0py
approved these changes
Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3038
SessionFlusher#add_sessionread and wrote@pending_aggregatesin two lookups without holding@mutex. Only the flush swap in#pending_envelopetook the lock, so the lock didn't stop anything. If the swap landed between the two lookups, the second one returned nil and raisedNoMethodError: undefined method '[]' for nilfromHub#end_session, which runs in the Rack middleware'sensure. A swap at other points dropped the session silently.add_sessionnow holds@mutexaround both lookups, which matches howTelemetryEventBufferlocks both sides. The new spec starts the flush swap on another thread whileadd_sessionis mid-update. It fails on master with theNoMethodErrorand passes with this change.Proof: reproduction in getsentry/repro#65 (
sentry-ruby/3038/repro.rb). It runs a deterministic interleaving plus a stress run with no hooks: one thread callsadd_sessionwhile another runs the real swap.Before (7.2.0, Ruby 4.0.9):
After (this branch, Ruby 4.0.9):
With
RUBY_THREAD_TIMESLICE=1 STRESS_SECONDS=15, which makes MRI switch threads far more often, 7.2.0 had 286 crashes and 225 lost sessions out of about 8.6M. This branch had 0 and 0 out of about 8.4M. Ruby 3.2.11 shows the same before/after result.via @dingsdax.
--
View Session [Sentry]