Defer mailer include via ActiveSupport.on_load(:action_mailer)#926
Merged
scambra merged 1 commit intoMay 22, 2026
Merged
Conversation
The to_prepare callback was forcing Devise.mailer (a String#constantize) to resolve at app initialization time, which loaded ActionMailer::Base and — via the default ActionMailer::Base inheritance chain pulling in MailDeliveryJob — ActiveJob::Base before app initialization completed. Rails edge's guard_load_hooks support (rails/rails#56201) reports both loads as 'loaded before application initialization' on every boot. Wrapping the include in on_load(:action_mailer) keeps the include happening on every reload (the on_load callback runs immediately when re-registered after :action_mailer has fired) without forcing the load during the Rails finisher initializer.
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 #925.
config.to_preparein the Engine forcesDevise.mailerto constantize during the Rails finisher initializer, which in turn loadsActionMailer::Base(and, viaMailDeliveryJob,ActiveJob::Base) beforeRails.application.initialized?flips. Rails edge's newguard_load_hookssupport (rails/rails#56201) reports this as a premature load-hook for both:action_mailerand:active_jobon every boot.This change wraps the body of the
to_prepareblock inActiveSupport.on_load(:action_mailer):to_prepareregisters anon_load(:action_mailer)hook. Nothing loadsActionMailer::Baseyet. When something later resolves a mailer (sending an invitation, etc.), the hook fires and the include runs against the loaded mailer class. App init has already completed, so no warning.to_preparecycles (dev reloads).:action_mailerhas already fired, so the freshly-registeredon_loadblock runs immediately, re-applying the include on the reloadedDevise.mailerclass — same behaviour as before.No functional change for stable Rails users; just silences the boot warnings on Rails edge and saves a small amount of boot time by deferring the mailer load.
I didn't add a regression test — wasn't sure how to best exercise railtie load order in the existing harness. Happy to add one if you'd like, or to take suggestions on the right approach.