Fix account reset cancel race condition#11329
Merged
mitchellhenke merged 6 commits intomainfrom Oct 9, 2024
Merged
Conversation
changelog: Bug Fixes, Account Reset, Fix race condition where concurrent requests to cancel could result in an exception
f4fdf55 to
b817900
Compare
zachmargolis
approved these changes
Oct 9, 2024
Comment on lines
+75
to
+79
| notify_user_of_cancellation = instance_double(AccountReset::NotifyUserOfRequestCancellation) | ||
| expect(AccountReset::NotifyUserOfRequestCancellation).to receive(:new). | ||
| with(user). | ||
| and_return(notify_user_of_cancellation) | ||
| expect(notify_user_of_cancellation).to receive(:call) |
Contributor
There was a problem hiding this comment.
maybe in a future refactor we can unify NotifyUserOfRequestCancellation into the same class, which would let us stub methods on the subject instead of having to stub entire other classes
Contributor
Author
There was a problem hiding this comment.
Ah, good point, and what better time than now. Moved in c17e501
aduth
approved these changes
Oct 9, 2024
Comment on lines
+36
to
+41
| if result == 1 | ||
| NotifyUserOfRequestCancellation.new(user).call | ||
| true | ||
| else | ||
| false | ||
| end |
Contributor
There was a problem hiding this comment.
Does it matter to have a return value here? Doesn't look like we're using it.
Suggested change
| if result == 1 | |
| NotifyUserOfRequestCancellation.new(user).call | |
| true | |
| else | |
| false | |
| end | |
| NotifyUserOfRequestCancellation.new(user).call if result == 1 |
Contributor
Author
There was a problem hiding this comment.
No, this was kind of a leftover from some of the refactoring. Slight conflict with the changes from the suggestion here so I've added 6cebebc54479445ee0e5ab59211d7f3370e339a0 to incorporate the suggestion.
f3e3673 to
93926d7
Compare
Co-authored-by: Andrew Duthie <1779930+aduth@users.noreply.github.com>
93926d7 to
a06a2ab
Compare
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.
🛠 Summary of changes
Rarely, when a pending account reset cancellation is submitted, a race condition can be triggered that results in an exception. This is due to separate select queries. The first is used for validation, and the latter's to execute the update query to cancel. If another request runs the database update in the time between the first and second select, the result for the second query will be nil, and the
#updatecall will raise an exception. NewRelic LinkThis set of changes involves a slight refactor to bring the select and update queries together in the same class. To address the race condition,
update_allis used, and the resulting number of updates is used to determine whether a change was applied and if notifications should be sent.