Repository navigation
Description
Describe the bug
Concurrent vote requests may cause the stored vote_count of a question, answer, or comment to become inconsistent with the actual active vote records.
In VoteService.VoteUp and VoteService.VoteDown, vote activities are committed through VoteRepo.Vote or VoteRepo.CancelVote before GetAndSaveVoteResult recalculates and persists the vote totals.
The current implementation of GetAndSaveVoteResult is:
func (vr *VoteRepo) GetAndSaveVoteResult(ctx context.Context, objectID, objectType string) (
up, down int64, err error) {
up = vr.countVoteUp(ctx, objectID, objectType)
down = vr.countVoteDown(ctx, objectID, objectType)
err = vr.updateVotes(ctx, objectID, objectType, int(up-down))
return
}The vote-count queries and the subsequent vote_count update are separate database operations executed outside the vote transaction.
Because updateVotes writes the calculated total as an absolute value, an earlier request can overwrite a newer result if concurrent requests interleave between counting and updating.
This may leave question.vote_count, answer.vote_count, or comment.vote_count inconsistent with the corresponding active vote activities until a later operation recalculates the total.
Relevant code:
internal/service/content/vote_service.go—VoteUpandVoteDowninternal/repo/activity/vote_repo.go—Vote,GetAndSaveVoteResult, andupdateVotes
To Reproduce
The following is a controlled concurrency scenario derived from the current implementation. It has not yet been verified by running an integration test.
- Create a question with no existing votes and
vote_count = 0. - Have User A upvote the question. Allow the vote transaction to commit and
GetAndSaveVoteResultto calculateup = 1anddown = 0, but pause execution immediately beforeupdateVotes. - Have User B upvote the same question. Allow User B's vote transaction to commit and its
GetAndSaveVoteResultcall to complete, updatingvote_countto2. - Resume User A's request. It writes its previously calculated value of
1to the same question. - Query the active vote activities and compare their total with the question's stored
vote_count.
With this execution order, the resulting state would be:
Active upvote activities: 2
Active downvote activities: 0
Expected question.vote_count: 2
Actual question.vote_count: 1
The same race condition may affect answers and comments because updateVotes uses the same read-compute-write approach for those object types.
Expected behavior
The stored vote_count should always reflect the difference between active upvotes and active downvotes after concurrent vote operations have completed.
A stale calculation from an earlier request should not be able to overwrite a more recent vote total.
Vote activity changes and the corresponding aggregate count should be synchronized appropriately, for example through database transaction boundaries and locking, or atomic updates based on actual vote-state transitions.
Screenshots
Not applicable. This is a backend concurrency issue.
Platform
- Device: N/A
- OS: N/A (server-side database operation)
- Browser and version: N/A
- Version:
devbranch, commit3dc1df18b43aebad2040c10ea412d6160ba4d65f
Activity
lhk0504 commentedon Oct 15, 2026
Hi, @LinkinStars could you please take a look at this issue when you have time?
I'd also be interested in working on a fix if the issue is confirmed. Thanks!
ChangkeunJ commentedon Oct 20, 2026
I reproduced this locally and can confirm the race.
VoteService.VoteUp/VoteDownrun the vote transaction first and then callGetAndSaveVoteResult, which counts the active vote activities and writes the total as an absolute value (up - down). If another vote commits between the count and the write, the slower request writes last and stores its stale total, sovote_countdrifts away from the real number of votes.I wrote a regression test that hammers one question with concurrent votes (a fresh question per round, 32 voters per round). On current master the stored
vote_countregularly ends up below the real count, e.g. 19 or 26 instead of 32.The fix itself is small: instead of writing the precomputed total,
updateVotesrecounts the activities inside the UPDATE statement itself:so the stored value always reflects the committed state at write time, no matter how the requests interleave. The up/down counts returned to the caller still come from the existing count helpers.
@lhk0504 I saw you were interested in working on this. I already had the reproduction and patch together while verifying the report, so I went ahead and opened a PR with it. Happy to adjust the approach if the maintainers prefer a different direction.