Skip to content

Exception if session tracking is true and enabled_in_current_env? is false #2470

@esquivalient

Description

@esquivalient

Issue Description

In the Rails project I'm working on, Sentry is disabled during testing. When I upgraded the sentry-ruby gem from 5.10.0 to 5.21.0, I had some tests give the following exception

Minitest::UnexpectedError:         NoMethodError: undefined method `add_session' for nil:NilClass

              Sentry.session_flusher.add_session(session)
                                    ^^^^^^^^^^^^

I'm pretty sure this is what's happening.

  • auto_session_tracking isn't in my Sentry config, but it's set to true because that's the new default in 5.21.0
  • However, this project has Sentry disabled for the test environment. That makes config.session_tracking? return false because enabled_in_current_env? is false
  • so, when
  • lib/sentry/rack/capture_exceptions calls Sentry.with_session_tracking as defined in lib/sentry-ruby.rb
  • lib/sentry-ruby.rb calls get_current_hub.with_session_tracking(&block) as defined in lib/sentry/hub.rb
  • with_session_tracking in lib/sentry/hub.rb bypasses the guard statement and calls Sentry.session_flusher.add_session(session)
  • which causes an exception because session_flusher is nil

The exception doesn't happen if auto_session_tracking is false, because the guard on Hub#with_session_tracking explicitly returns.

Reproduction Steps

  1. Disable Sentry in the environment you're using to reproduce
  2. Enable session tracking, implicitly or explicitly
  3. Do something that causes CaptureExceptions.call to be executed

Expected Behavior

No exceptions about session tracking is Sentry is supposed to be disabled.

Actual Behavior

The undefined method add_session' for nil:NilClass` exception from above.

Ruby Version

ruby 3.1.6p260

SDK Version

5.21.0

Integration and Its Version

Rails

Sentry Config

No response

Metadata

Metadata

Assignees

Labels

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions