Skip to content

fix(dkg): make dkg session field access uniformly mutex safe#860

Open
0xHansLee wants to merge 1 commit into
dkg/devfrom
dkg/fix-dkgsession-mutex-coverage
Open

fix(dkg): make dkg session field access uniformly mutex safe#860
0xHansLee wants to merge 1 commit into
dkg/devfrom
dkg/fix-dkgsession-mutex-coverage

Conversation

@0xHansLee

@0xHansLee 0xHansLee commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Problem

DKGSession carries a sync.RWMutex, but many of its fields were read and written raw across the keeper. The session pointer is shared between the ABCI thread, the async DKG goroutines, and the decrypt worker, so raw access races — most sharply saveSession marshaling the live pointer while another goroutine mutates it, which can tear a slice header and panic the validator.

Fix

  • Route every external DKGSession field access through mutex-protected accessors: getters clone slices, and grouped setters (SetSetupResult, SetCodeCommitmentIfEmpty) write related fields atomically under one lock.
  • saveSession marshals a Snapshot() deep copy taken under RLock instead of the live pointer.
  • Wrap the async DKG goroutines with a panic-recovery guard so a field-access panic degrades to a logged error instead of crashing the process.

Test coverage

go test -race -coverpkg=./client/x/dkg/... ./client/x/dkg/...

Symbol / package Coverage
New DKGSession accessors (getters, HasSetupData, SetSetupResult, SetCodeCommitmentIfEmpty, Snapshot) 100%
keeper package 82.1%

issue: #859

DKGSession.mu guarded only a subset of fields; many others were read and
written as raw fields across goroutines, racing the decrypt worker and the
recovery/finalization paths and risking a torn slice-header read.

- Add Snapshot() and marshal a locked deep copy in saveSession, closing the
  marshal-vs-mutation race for every field at once.
- Route the concurrency-exposed reads (decrypt worker, finalize contract call,
  ResumeDKGService) through RLock getters that return byte-slice copies; add
  SetIndex/SetFinalized locked setters and convert the matching raw writes.
- recover()-wrap the async DKG-service goroutines spawned from
  resumeFailedSession, mirroring the decrypt worker's guard.

Node-local only (StateManager JSON/in-memory, gated by isDKGSvcEnabled with a
gasless context); no KVStore, consensus, or app-hash impact.
@0xHansLee 0xHansLee changed the title fix(dkg): make DKGSession field access uniformly mutex-safe fix(dkg): make dkg session field access uniformly mutex safe Jul 24, 2026
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