Skip to content

Fix account reset cancel race condition#11329

Merged
mitchellhenke merged 6 commits intomainfrom
mitchellhenke/account-reset-cancel-race-condition
Oct 9, 2024
Merged

Fix account reset cancel race condition#11329
mitchellhenke merged 6 commits intomainfrom
mitchellhenke/account-reset-cancel-race-condition

Conversation

@mitchellhenke
Copy link
Contributor

🛠 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 #update call will raise an exception. NewRelic Link

This 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_all is used, and the resulting number of updates is used to determine whether a change was applied and if notifications should be sent.

@mitchellhenke mitchellhenke requested a review from aduth October 9, 2024 16:20
changelog: Bug Fixes, Account Reset, Fix race condition where concurrent requests to cancel could result in an exception
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/account-reset-cancel-race-condition branch from f4fdf55 to b817900 Compare October 9, 2024 16:31
Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

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

LGTM

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point, and what better time than now. Moved in c17e501

Comment on lines +36 to +41
if result == 1
NotifyUserOfRequestCancellation.new(user).call
true
else
false
end
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

@mitchellhenke mitchellhenke Oct 9, 2024

Choose a reason for hiding this comment

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

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.

@mitchellhenke mitchellhenke force-pushed the mitchellhenke/account-reset-cancel-race-condition branch 2 times, most recently from f3e3673 to 93926d7 Compare October 9, 2024 16:55
Mitchell Henke and others added 2 commits October 9, 2024 12:22
Co-authored-by: Andrew Duthie <1779930+aduth@users.noreply.github.com>
@mitchellhenke mitchellhenke force-pushed the mitchellhenke/account-reset-cancel-race-condition branch from 93926d7 to a06a2ab Compare October 9, 2024 17:22
@mitchellhenke mitchellhenke merged commit 3bff320 into main Oct 9, 2024
@mitchellhenke mitchellhenke deleted the mitchellhenke/account-reset-cancel-race-condition branch October 9, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants