New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic on disabling afer_commit callbacks #32807

Merged
merged 1 commit into from May 4, 2018

Conversation

Projects
None yet
4 participants
@bdurand
Contributor

bdurand commented May 4, 2018

Commit callbacks are intentionally disabled when errors occur when calling the callback chain in order to reset the internal record state. However, the implicit order of operations on the logic for checking if callbacks are disabled is wrong. The result is that callbacks can be unexpectedly when errors occur in transactions.

Summary

The flag to disable running after_commit callbacks is broken and will be ignored if a record is persisted to the database resulting in callbacks being run in all cases. The current logic to decide if callbacks should be run is:

should_run_callbacks && destroyed? || persisted?

Which is interpreted as:

(should_run_callbacks && destroyed?) || persisted?

But it should be:

should_run_callbacks && (destroyed? || persisted?)

This bug comes up in a case where there is an error during an after_commit call and was reported in #29747.

@rails-bot

This comment has been minimized.

rails-bot commented May 4, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kamipo (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

activerecord/test/cases/transaction_callbacks_test.rb Outdated
record = TopicWithCallbacks.create!
callback_called = false
record.after_commit_block { callback_called = true }
record.committed!(should_run_callbacks: false)

This comment has been minimized.

@kamipo

kamipo May 4, 2018

Member

Since commited! is an internal API, it is preferable to test the fix by this PR from public API.

This comment has been minimized.

@bdurand

bdurand May 4, 2018

Contributor

Update PR with a test case using only the public API and more clearly demonstrating the use case where I see the bug.

@@ -340,7 +340,7 @@ def before_committed! # :nodoc:
# Ensure that it is not called if the object was never persisted (failed create),
# but call it after the commit of a destroyed object.
def committed!(should_run_callbacks: true) #:nodoc:
if should_run_callbacks && destroyed? || persisted?
if should_run_callbacks && (destroyed? || persisted?)

This comment has been minimized.

@kamipo

kamipo May 4, 2018

Member

This bug was accidentally introduced at #16537 (comment).

Fix logic on disabling commit callbacks
Commit callbacks are intentionally disabled when errors occur when calling the callback chain in order to reset the internal record state. However, the implicit order of operations on the logic for checking if callbacks are disabled is wrong. The result is that callbacks can be unexpectedly when errors occur in transactions.

@kamipo kamipo merged commit 5eaec23 into rails:master May 4, 2018

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@naw

This comment has been minimized.

Contributor

naw commented May 14, 2018

To be clear, this is not really the same bug as I reported in #29747, even though it involves the same line of code.

kamipo added a commit that referenced this pull request Jun 4, 2018

Merge pull request #32807 from bdurand/fix_committed_disable_callbacks
Fix logic on disabling afer_commit callbacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment