Skip to content

[Activejob] Enable enqueue_after_transaction_commit by default with ActiveRecord and validate invalid configurations - #55788

Merged
jeremy merged 11 commits into
rails:mainfrom
mugitti9:activejob-enqueue-after-transaction-commit_default_to_true
Dec 28, 2025
Merged

jeremy merged 11 commits into
rails:mainfrom
mugitti9:activejob-enqueue-after-transaction-commit_default_to_true

Conversation

@mugitti9

@mugitti9 mugitti9 commented Sep 27, 2025 •

Copy link
Copy Markdown
Contributor

Motivation / Background

Until now, the default value of enqueue_after_transaction_commit has 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 = false on a per-job basis.

Detail

  • The default value of enqueue_after_transaction_commit is now set to true only when ActiveRecord is available.
  • A validation has been added: attempting to set enqueue_after_transaction_commit = true when 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:

ActiveRecord::Base.transaction do
  user = User.create!(name: "James")
  UserCreationNotifyJob.perform_later(user:)
end

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_commit would 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:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot Bot added the activejob label Sep 27, 2025
@mugitti9 mugitti9 changed the title Activejob enqueue after transaction commit default to true [Activejob] enqueue after transaction commit default to true Sep 27, 2025

@jeremy jeremy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-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.

@mugitti9
mugitti9 force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch 2 times, most recently from 3e4def3 to 524895d Compare November 23, 2025 04:25
@mugitti9 mugitti9 changed the title [Activejob] enqueue after transaction commit default to true [Activejob] Enable enqueue_after_transaction_commit by default with ActiveRecord and validate invalid configurations Nov 23, 2025
@mugitti9

Copy link
Copy Markdown
Contributor Author

@jeremy
Thanks for your review and for pointing that out!

You're absolutely right — adding ActiveRecord as a hard dependency was too much.
I've updated the implementation based on your suggestion:

  • The default value of enqueue_after_transaction_commit is now set to true only when ActiveRecord is defined.
  • When ActiveRecord is not available, setting enqueue_after_transaction_commit = true doesn’t make sense, so an exception is raised in that case to surface the misconfiguration clearly.

I'd appreciate it if you could take another look.

@mugitti9
mugitti9 marked this pull request as draft November 23, 2025 04:50
@mugitti9
mugitti9 marked this pull request as ready for review November 23, 2025 04:50
@mugitti9
mugitti9 requested a review from jeremy November 23, 2025 04:50
@mugitti9
mugitti9 force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch from 48b80ac to 6fe0431 Compare December 2, 2025 02:19
@github-actions github-actions Bot added the docs label Dec 28, 2025
@zzak

zzak commented Dec 28, 2025

Copy link
Copy Markdown
Member

I'm confused that we would re-introduce this config (see: 582f91e), but somehow makes sense in the context of changing the default? 🤔

@jeremy
jeremy force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch from 3ec1fdb to b29af46 Compare December 28, 2025 01:03
@jeremy

jeremy commented Dec 28, 2025

Copy link
Copy Markdown
Member

I'm confused that we would re-introduce this config (see: 582f91e), but somehow makes sense in the context of changing the default? 🤔

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.)

@zzak

zzak commented Dec 28, 2025

Copy link
Copy Markdown
Member

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:

if app.config.active_job.key?(:enqueue_after_transaction_commit)
ActiveJob.deprecator.warn(<<~MSG.squish)
`config.active_job.enqueue_after_transaction_commit` is deprecated and will be removed in Rails 8.1.
This configuration can still be set on individual jobs using `self.enqueue_after_transaction_commit=`,
but due the nature of this behavior, it is not recommended to be set globally.
MSG

@jeremy
jeremy force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch from b29af46 to 790841a Compare December 28, 2025 01:23
@jeremy

jeremy commented Dec 28, 2025

Copy link
Copy Markdown
Member

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:

if app.config.active_job.key?(:enqueue_after_transaction_commit)
ActiveJob.deprecator.warn(<<~MSG.squish)
`config.active_job.enqueue_after_transaction_commit` is deprecated and will be removed in Rails 8.1.
This configuration can still be set on individual jobs using `self.enqueue_after_transaction_commit=`,
but due the nature of this behavior, it is not recommended to be set globally.
MSG

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
@jeremy
jeremy force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch from 790841a to 15235c5 Compare December 28, 2025 02:11
@jeremy

jeremy commented Dec 28, 2025

Copy link
Copy Markdown
Member

@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.

@zzak

zzak commented Dec 28, 2025

Copy link
Copy Markdown
Member

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.

@jeremy
jeremy force-pushed the activejob-enqueue-after-transaction-commit_default_to_true branch from 4d82226 to e6612e4 Compare December 28, 2025 04:34
@jeremy jeremy added this to the 8.2.0 milestone Dec 28, 2025

@jeremy jeremy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@jeremy
jeremy merged commit a477a32 into rails:main Dec 28, 2025
drymar pushed a commit to drymar/rails that referenced this pull request Jan 5, 2026
…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]>
rosa added a commit to rails/solid_queue that referenced this pull request Jan 5, 2026
…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)
rosa added a commit to rails/solid_queue that referenced this pull request Jan 6, 2026
…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)
sincerydev123 added a commit to sincerydev123/queue that referenced this pull request Jan 26, 2026
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants