Fix logic on disabling afer_commit callbacks #32807
Conversation
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. |
record = TopicWithCallbacks.create! | ||
callback_called = false | ||
record.after_commit_block { callback_called = true } | ||
record.committed!(should_run_callbacks: false) |
kamipo
May 4, 2018
Member
Since commited!
is an internal API, it is preferable to test the fix by this PR from public API.
Since commited!
is an internal API, it is preferable to test the fix by this PR from public API.
bdurand
May 4, 2018
Author
Contributor
Update PR with a test case using only the public API and more clearly demonstrating the use case where I see the bug.
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?) |
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.
To be clear, this is not really the same bug as I reported in #29747, even though it involves the same line of code. |
Fix logic on disabling afer_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.
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.