getsentry / getsentry/sentry-ruby

sentry-rails triggers "Prematurely executing load hooks" warning for :action_dispatch_response on Rails 8.2

Aperta
#2,912 6 commenti 1 reazione 1 assegnatario Rivendicata da @sl0thentr0py Vedi su GitHub
Improvement Ruby sentry-rails
Lingua principale
Ruby
Stelle
987
Fork
541
Merge medio
17h 40m
PR unite (30g)
19

Descrizione

### Issue Description

`sentry-rails` triggers a "Prematurely executing load hooks" deprecation warning on boot with Rails 8.2 (edge). The warning is about `:action_dispatch_response` being loaded before application initialization completes.

The root cause is in `Sentry::Railtie#extend_controller_methods` (`sentry-rails/lib/sentry/rails/railtie.rb:95`). Inside the `ActiveSupport.on_load :action_controller` block, the line:

```ruby
ActionController::Live.send(:prepend, Sentry::Rails::Overrides::StreamingReporter)
```

forces eager loading of `ActionController::Live`, which requires `action_controller/metal/live.rb`, which in turn requires `action_dispatch/http/response.rb`. That file calls `ActiveSupport.run_load_hooks(:action_dispatch_response, ...)`, triggering the `:action_dispatch_response` load hook prematurely during the `after_initialize` phase.

Rails 8.2 added detection for this pattern and emits the warning.

## Full Stack Trace

```
/activesupport/lib/active_support/lazy_load_hooks.rb:97:in 'Module#class_eval'
/activesupport/lib/active_support/lazy_load_hooks.rb:97:in 'block in ActiveSupport::LazyLoadHooks#execute_hook'
/activesupport/lib/active_support/lazy_load_hooks.rb:87:in 'ActiveSupport::LazyLoadHooks#with_execution_control'
/activesupport/lib/active_support/lazy_load_hooks.rb:92:in 'ActiveSupport::LazyLoadHooks#execute_hook'
/activesupport/lib/active_support/lazy_load_hooks.rb:78:in 'block in ActiveSupport::LazyLoadHooks#run_load_hooks'
/activesupport/lib/active_support/lazy_load_hooks.rb:77:in 'ActiveSupport::LazyLoadHooks#run_load_hooks'
/actionpack/lib/action_dispatch/http/response.rb:603:in ''
/actionpack/lib/action_dispatch/http/response.rb:10:in ''
/action_controller/metal/live.rb:5:in ''
/sentry-rails-6.5.0/lib/sentry/rails/railtie.rb:95:in 'block in Sentry::Railtie#extend_controller_methods'
/sentry-rails-6.5.0/lib/sentry/rails/railtie.rb:92:in 'ActiveSupport::LazyLoadHooks#on_load'
/sentry-rails-6.5.0/lib/sentry/rails/railtie.rb:46:in 'block in '
```

### Reproduction Steps

1. Create a Rails 8.2 (edge/alpha) application with `sentry-rails` 6.5.0
2. Configure Sentry in an initializer (any config, even minimal)
3. Boot the application in an environment where `Sentry.init` is called (e.g. production, staging) — `rails server`, `rails console`, or any rake task
4. Observe the warning in output:

```
:action_dispatch_response was loaded before application initialization.
Prematurely executing load hooks will slow down your boot time
and could cause conflicts with the load order of your application.
Please wrap your code with an on_load hook:

ActiveSupport.on_load(:action_dispatch_response) do
# your code here
end
```

The warning only appears when `Sentry.initialized?` is true, since the `extend_controller_methods` call in `config.after_initialize` is guarded by that check (railtie.rb:41). Environments where `Sentry.init` is not called (e.g. development) do not trigger the warning.

### Expected Behavior

No premature load hook warning on boot. The `ActionController::Live` prepend should be deferred to avoid triggering early loading of `ActionDispatch::Response`.

### Actual Behavior

The deprecation warning is emitted on every boot in environments where Sentry is initialized (server start, console, rake tasks in production/staging).

## Suggested Fix

Move the `ActionController::Live` prepend into its own lazy load hook to avoid eagerly loading it inside `:action_controller`. For example:

```ruby
def extend_controller_methods
require "sentry/rails/controller_methods"
require "sentry/rails/controller_transaction"
require "sentry/rails/overrides/streaming_reporter"

ActiveSupport.on_load :action_controller do
include Sentry::Rails::ControllerMethods
include Sentry::Rails::ControllerTransaction
end

ActiveSupport.on_load :action_controller do
ActionController::Live.send(:prepend, Sentry::Rails::Overrides::StreamingReporter)
end
end
```

Or alternatively, guard the prepend with a lazy reference that doesn't trigger autoloading until `ActionController::Live` is actually used.

### Ruby Version

4.0.2

### SDK Version

sentry-ruby 6.5.0, sentry-rails 6.5.0

### Integration and Its Version

Rails 8.2.0.alpha (edge, commit 4df808965b4a)

### Sentry Config

```ruby
Sentry.init do |config|
config.dsn = "..."
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
config.traces_sample_rate = 0.1
config.environment = Rails.env
config.enabled_environments = %w[production staging]
end
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.