Skip to content

Replace unbounded NonceMap with bounded NonceWindow#357

Merged
0pcom merged 1 commit intoskycoin:developfrom
0pcom:fix/nonce-map-memory-leak
Mar 30, 2026
Merged

Replace unbounded NonceMap with bounded NonceWindow#357
0pcom merged 1 commit intoskycoin:developfrom
0pcom:fix/nonce-map-memory-leak

Conversation

@0pcom
Copy link
Copy Markdown
Collaborator

@0pcom 0pcom commented Mar 30, 2026

The NonceMap (map[uint64]struct{}) grew forever on long-lived sessions, accumulating one entry per decrypted message. For the setup-node handling thousands of streams, this leaked megabytes of memory over time.

Replace with NonceWindow: a sliding window using a 1024-bit bitmap (128 bytes) that tracks the highest nonce seen and the last 1024 nonces for out-of-order replay detection. Memory usage is constant regardless of session lifetime.

Since the transport is reliable (TCP via yamux/smux), nonces arrive mostly in order, so a 1024-entry window is more than sufficient. Nonces older than the window are rejected as replays.

The old NonceMap and DecryptWithNonceMap are kept but deprecated for backward compatibility.

The NonceMap (map[uint64]struct{}) grew forever on long-lived sessions,
accumulating one entry per decrypted message. For the setup-node
handling thousands of streams, this leaked megabytes of memory over time.

Replace with NonceWindow: a sliding window using a 1024-bit bitmap
(128 bytes) that tracks the highest nonce seen and the last 1024 nonces
for out-of-order replay detection. Memory usage is constant regardless
of session lifetime.

Since the transport is reliable (TCP via yamux/smux), nonces arrive
mostly in order, so a 1024-entry window is more than sufficient.
Nonces older than the window are rejected as replays.

The old NonceMap and DecryptWithNonceMap are kept but deprecated for
backward compatibility.
@0pcom 0pcom merged commit 2b2ebb3 into skycoin:develop Mar 30, 2026
3 checks passed
@0pcom 0pcom deleted the fix/nonce-map-memory-leak branch March 30, 2026 16:34
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.

1 participant