Skip to content

fix(store): protect Version() with mutex to prevent race condition - #597

Open
yasinlex wants to merge 1 commit into
canopy-network:mainfrom
yasinlex:fix/race-condition-in-store-version
Open

fix(store): protect Version() with mutex to prevent race condition#597
yasinlex wants to merge 1 commit into
canopy-network:mainfrom
yasinlex:fix/race-condition-in-store-version

Conversation

@yasinlex

@yasinlex yasinlex commented Sep 5, 2026

Copy link
Copy Markdown

Problem

Version() accessed s.version without synchronization:

func (s *Store) Version() uint64 { return s.version }  // No lock!

While Commit() updates s.version while holding the mutex:

s.mu.Lock()
defer s.mu.Unlock()
// ...
s.version = nextVersion  // Updated under lock

This creates a race condition when Version() is called from:

  • RPC handlers (query.go, line 359, 899, 948, 970)
  • FSM state (state.go, line 117)
  • CLI admin commands (admin.go, line 445)

While Commit() is updating the version.

Fix

func (s *Store) Version() uint64 {
    s.mu.Lock()
    defer s.mu.Unlock()
    return s.version
}

Impact

  • Before: Potential data race reading s.version during Commit()
  • After: Thread-safe access to s.version via mutex

The Version() method previously accessed s.version without any
synchronization, while Commit() updates s.version while holding
the mutex. This could lead to race conditions when Version() is
called from RPC handlers or other goroutines while Commit() is
in progress.

Now Version() acquires the mutex before reading s.version, ensuring
thread-safe access.
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.

2 participants