Skip to content

Concurrent vote updates may overwrite vote_count with stale totals #1646

Description

@lhk0504

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 — VoteUp and VoteDown
  • internal/repo/activity/vote_repo.go — Vote, GetAndSaveVoteResult, and updateVotes

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.

  1. Create a question with no existing votes and vote_count = 0.
  2. Have User A upvote the question. Allow the vote transaction to commit and GetAndSaveVoteResult to calculate up = 1 and down = 0, but pause execution immediately before updateVotes.
  3. Have User B upvote the same question. Allow User B's vote transaction to commit and its GetAndSaveVoteResult call to complete, updating vote_count to 2.
  4. Resume User A's request. It writes its previously calculated value of 1 to the same question.
  5. 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: dev branch, commit 3dc1df18b43aebad2040c10ea412d6160ba4d65f

Activity

  1. added
    bug
    on Oct 15, 2026
  2. added theissue type on Oct 15, 2026
  3. lhk0504 commented on Oct 15, 2026

    @lhk0504
    Author

    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!

  4. ChangkeunJ commented on Oct 20, 2026

    @ChangkeunJ

    I reproduced this locally and can confirm the race.

    VoteService.VoteUp / VoteDown run the vote transaction first and then call GetAndSaveVoteResult, 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, so vote_count drifts 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_count regularly 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, updateVotes recounts the activities inside the UPDATE statement itself:

    UPDATE question SET vote_count =
      ((SELECT COUNT(*) FROM activity WHERE object_id = ? AND activity_type = ? AND cancelled = 0)
     - (SELECT COUNT(*) FROM activity WHERE object_id = ? AND activity_type = ? AND cancelled = 0))
    WHERE id = ?

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions