Repository navigation
[Activejob] Enable enqueue_after_transaction_commit by default with ActiveRecord and validate invalid configurations - #55788
Conversation
jeremy
left a comment
There was a problem hiding this comment.
-1 on the AR dep. Would either enqueue immediately if AR is unavailable, gracefully behaving as anyone would expect, or raise an exception indicating that enqueue_after_transaction_commit should be set to false.
3e4def3 to
524895d
Compare
|
@jeremy You're absolutely right — adding ActiveRecord as a hard dependency was too much.
I'd appreciate it if you could take another look. |
48b80ac to
6fe0431
Compare
|
I'm confused that we would re-introduce this config (see: 582f91e), but somehow makes sense in the context of changing the default? 🤔 |
3ec1fdb to
b29af46
Compare
582f91e deprecated setting it to symbol values in job adapters; it continues as an explicit boolean app-controlled setting. (The commit message does imply that the entire setting is deprecated when it was not.) |
|
Unless I'm missing something, the flag itself was deprecated and removed in 8.1, and we'd be re-introducing it in 8.2 / 9.0: rails/activejob/lib/active_job/railtie.rb Lines 33 to 38 in 582f91e |
b29af46 to
790841a
Compare
Ah I see what you mean: global config vs per-job config. This is a bit of a mess; I'll consider. |
Also group Active Job defaults in the 8.2 new framework defaults template. # Conflicts: # railties/lib/rails/application/configuration.rb # railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_8_2.rb.tt
Also cover explicit false override for new apps.
# Conflicts: # activejob/CHANGELOG.md
790841a to
15235c5
Compare
|
@zzak, revised to avoid re‑introducing global config—for now. I think it's an awkward mistake, though. You can see how much worse the code is. Not going to foist that upon anyone. Apps need an app-wide toggle for this. I aim to un-deprecate, particularly considering it wasn't removed on schedule. |
|
Yeah, if you want to change the default, old apps need a way to maintain the existing behavior without any changes so a global config is kind of the only option. 🤔 I was just pointing out that bit of context. |
4d82226 to
e6612e4
Compare
jeremy
left a comment
There was a problem hiding this comment.
Un-deprecated the global config toggle, added docs, switched to versioned config (load_defaults "8.2" with the new framework defaults initializer), and gated config on AR load (rather than presence of the constant).
…flip it to true by default (rails#55788) Jobs are now enqueued after transaction commit. This fixes that jobs would surprisingly run against uncommitted and rolled-back records. New Rails 8.2 apps (and apps upgrading to `config.load_defaults "8.2"`) have `config.active_job.enqueue_after_transaction_commit = true` by default. Uncomment the setting in `config/initializers/new_framework_defaults_8_2.rb` to opt in. Un-deprecate the global `config.active_job.enqueue_after_transaction_commit` toggle for app-wide overrides. It was deprecated in Rails 8.0 (when the symbol values were removed) and made non-functional in 8.1. It now works as a boolean config again. Co-authored-by: Jeremy Daer <[email protected]>
…on_commit` See rails/rails#55788 This setting has gone through different stages, and we must account for them: - Rails 7.1: Method not available (no deferred enqueue) - Rails 7.2: :default (defers to adapter's method, which returns true in Solid Queue's case) - Rails 8.0-8.1: false (no deferred enqueue, the setting was deprecated) - Rails 8.2: true (deferred enqueue enabled by default)
…on_commit` See rails/rails#55788 This setting has gone through different stages, and we must account for them: - Rails 7.1: Method not available (no deferred enqueue) - Rails 7.2: :default (defers to adapter's method, which returns true in Solid Queue's case) - Rails 8.0-8.1: false (no deferred enqueue, the setting was deprecated) - Rails 8.2: true (deferred enqueue enabled by default)
…on_commit` See rails/rails#55788 This setting has gone through different stages, and we must account for them: - Rails 7.1: Method not available (no deferred enqueue) - Rails 7.2: :default (defers to adapter's method, which returns true in Solid Queue's case) - Rails 8.0-8.1: false (no deferred enqueue, the setting was deprecated) - Rails 8.2: true (deferred enqueue enabled by default)
Motivation / Background
Until now, the default value of
enqueue_after_transaction_commithas been false.This meant that jobs enqueued inside a transaction were dispatched to the queue immediately and could still run even if the transaction was rolled back. In practice, this is not a rare edge case—jobs that depend on uncommitted data often end up running prematurely, breaking application consistency.
With this change, the default is switched to true only when ActiveRecord is defined. This ensures that jobs are enqueued only after the surrounding transaction has successfully committed, preventing jobs tied to rolled-back work from being executed unnecessarily.
As DHH also stated in #53375
Yes, by default, all jobs should enqueue after the commit.I strongly agree with this position. At the same time, this change does not remove flexibility: if immediate enqueueing is still desired, it can be explicitly enabled by setting
self.enqueue_after_transaction_commit = falseon a per-job basis.Detail
enqueue_after_transaction_commitis now set to true only when ActiveRecord is available.enqueue_after_transaction_commit = truewhen ActiveRecord is not defined will now raise an error, since such a configuration would not have meaningful semantics without transaction hooks.Why this change is needed (what problem it solves)
With the current behavior on main, jobs can be enqueued before the surrounding database transaction has successfully committed. For example:
In this case, UserCreationNotifyJob may be enqueued while the user record is still uncommitted. If the transaction is rolled back, the job can still run and attempt to operate on data that does not actually exist. This leads to subtle and easily overlooked inconsistencies.
Until Rails 7.2, most adapters—Sidekiq, Resque, and Solid Queue—effectively behaved as if enqueue_after_transaction_commit = true by default. As a result, many applications implicitly relied on jobs being enqueued only after the transaction had fully committed. With the current default on main, however, jobs may run earlier than expected, and this can introduce hard-to-debug issues.
In our application, we currently mitigate this by explicitly setting enqueue_after_transaction_commit = true on almost all jobs. But this shifts responsibility for a fundamental safety guarantee from the framework to individual applications, increases boilerplate, and risks inconsistencies when someone forgets to set it.
This issue is not theoretical or rare. For example, at a recent Ruby/Rails conference in Japan ("Kaigi on Rails"), there was a well-received talk titled “Never enqueue async jobs inside a transaction—seriously, never!”, which highlighted exactly this class of problems. The talk resonated widely, which shows how many developers have been impacted by premature job execution.
Additional information
When ActiveRecord is not defined, transaction callbacks are not available, and therefore enabling
enqueue_after_transaction_commitwould result in misleading or inconsistent behavior. Explicit validation has been added to surface this misuse early and clearly.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]