Skip to content

fix(dkg): prune dkg sessions older than the retention window#858

Merged
0xHansLee merged 2 commits into
dkg/devfrom
fix-prune-old-dkg-sessions
Jul 23, 2026
Merged

fix(dkg): prune dkg sessions older than the retention window#858
0xHansLee merged 2 commits into
dkg/devfrom
fix-prune-old-dkg-sessions

Conversation

@0xHansLee

Copy link
Copy Markdown
Contributor

Problem

Local DKG sessions (the in-memory map and on-disk session_*.json files) were never pruned and grew unbounded. The existing CleanupExpiredSessions was dead code and would have deleted the active completed session.

Fix

Add PruneOldSessions, wired into round activation, deleting sessions for rounds older than activeRound - sessionRetentionRounds (= 10). Node-local only (no KVStore/consensus access); the round-based criterion is deterministic with no wall-clock dependence. Retention of 10 rounds comfortably exceeds the decrypt path's 2-round lookback.

Tests

  • TestStateManager_PruneOldSessions, _RetainedRoundStillUsable, _NoopBelowHorizon

issue: none

@0xHansLee 0xHansLee changed the title fix(dkg): prune DKG sessions older than the retention window fix(dkg): prune dkg sessions older than the retention window Jul 21, 2026
@yingyangxu2026

Copy link
Copy Markdown

Two things. The commit message says the window keeps "the previous sessionRetentionRounds (3) rounds," but the constant is actually 10 — the message needs updating. Separately (pre-existing, not introduced here): a crash-orphaned session_<round>.json.tmp is never cleaned up — loadSessions globs session_*.json and the prune's os.Remove targets the non-tmp name, so those files linger indefinitely.

@0xHansLee 0xHansLee self-assigned this Jul 22, 2026
DKG sessions were created per round but never deleted in production, so
session_*.json files and the in-memory sessions map grew unbounded and
processDecryptQueue scanned every session each tick.

Replace the dead, phase-based CleanupExpiredSessions with a round-based
PruneOldSessions that reuses DeleteSession to remove both the in-memory
entry and the disk file, and call it when a round activates in
handleDKGComplete. Retention keeps the active round plus the previous
sessionRetentionRounds (10) rounds, a generous margin above the decrypt
path's 2-round window. Node-local state only; the round-based criterion
keeps it deterministic.
A crash between saveSession's write and rename leaves an orphaned
session_<round>.json.tmp behind. loadSessions globs only the non-tmp
name and DeleteSession/PruneOldSessions never target the .tmp, so these
partial writes linger on disk indefinitely.

Sweep orphaned .tmp files during loadSessions (startup, before the node
serves requests, so no concurrent writer). Also defensively remove a
stray .tmp for the round in DeleteSession.
@0xHansLee
0xHansLee force-pushed the fix-prune-old-dkg-sessions branch from 9ea9f58 to bd69818 Compare July 22, 2026 08:52
@0xHansLee
0xHansLee merged commit 23da8f8 into dkg/dev Jul 23, 2026
6 checks passed
@0xHansLee
0xHansLee deleted the fix-prune-old-dkg-sessions branch July 23, 2026 06:58
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