From f2251388ed3af0a564c0fbac9abe4934ef4dbf72 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Thu, 28 Nov 2024 12:00:31 +0100 Subject: [PATCH] Add mutex sync to SessionFlusher aggregates (#2469) --- CHANGELOG.md | 2 ++ sentry-ruby/lib/sentry/session_flusher.rb | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d11bda1..6a88e9d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ - Fix `send_default_pii` handling in rails controller spans [#2443](https://github.com/getsentry/sentry-ruby/pull/2443) - Fixes [#2438](https://github.com/getsentry/sentry-ruby/issues/2438) - Fix `RescuedExceptionInterceptor` to handle an empty configuration [#2428](https://github.com/getsentry/sentry-ruby/pull/2428) +- Add mutex sync to `SessionFlusher` aggregates [#2469](https://github.com/getsentry/sentry-ruby/pull/2469) + - Fixes [#2468](https://github.com/getsentry/sentry-ruby/issues/2468) ## 5.21.0 diff --git a/sentry-ruby/lib/sentry/session_flusher.rb b/sentry-ruby/lib/sentry/session_flusher.rb index f992a3b20..8f0510832 100644 --- a/sentry-ruby/lib/sentry/session_flusher.rb +++ b/sentry-ruby/lib/sentry/session_flusher.rb @@ -10,6 +10,7 @@ def initialize(configuration, client) @pending_aggregates = {} @release = configuration.release @environment = configuration.environment + @mutex = Mutex.new log_debug("[Sessions] Sessions won't be captured without a valid release") unless @release end @@ -18,7 +19,6 @@ def flush return if @pending_aggregates.empty? @client.capture_envelope(pending_envelope) - @pending_aggregates = {} end alias_method :run, :flush @@ -42,11 +42,15 @@ def init_aggregates(aggregation_key) end def pending_envelope - envelope = Envelope.new + aggregates = @mutex.synchronize do + aggregates = @pending_aggregates.values + @pending_aggregates = {} + aggregates + end + envelope = Envelope.new header = { type: "sessions" } - payload = { attrs: attrs, aggregates: @pending_aggregates.values } - + payload = { attrs: attrs, aggregates: aggregates } envelope.add_item(header, payload) envelope end