Skip to content
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

Use transaction to update data when joining challenge #15253

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

SYN-tactic
Copy link

@SYN-tactic SYN-tactic commented Jun 19, 2024

Fixes #8337

Changes

This PR fixes an issue where the member count listed on a challenge's page (which is taken from the challenge's document in the database) can be incorrect.
This issue occurred because of two separate database calls that occur when a user joins a challenge. In particular, the challenge.addToUser(user) method and the challenge.save() methods are what conspired to cause this issue.

Since challenge.addToUser(user) actually modifies the user document as a separate database call, if this were to succeed and subsequently the challenge.save() were to fail, there would be a mismatch in the participants of the challenge (see this comment for reference).

In looking into this, I noticed that this issue was probably mitigated by PR #11502, where the author added a check to make sure the User document had actually been modified before proceeding with saving the challenge. Prior to that, it was likely that challenge.addToUser(user) would sometimes fail but subsequently challenge.save() succeeded, leading to the original issue.

My solution includes all the relevant database calls in a single transaction, so that if any query should fail then no part of the User or Challenge documents will be modified. I've maintained the current functionality as much as possible so as to minimize the impact this change has. The primary change that is happening here is to wrap several database calls into a a single mongoose session/transaction.


Habitica user UUID: d419ade0-fb52-4fec-890b-905a75a4fa1c

@SYN-tactic SYN-tactic changed the title Add transaction to challenge join Use transaction to update data when joining challenge Jun 20, 2024
@SYN-tactic SYN-tactic marked this pull request as ready for review June 20, 2024 21:25
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.

Challenge member count can be wrong
1 participant