Skip to content

[BUG] Potential memory leaks when switching to primary after writing expired keys to replica #2952

Description

@li-benson

Describe the bug

Potential memory leaks may occur when a replica node (that has received expired key writes) is switched to a primary node in Valkey. This memory leak is caused by the failure to clean up replicaKeysWithExpire when the node is promoted to primary, which results in this block of memory being unavailable for reuse.

To reproduce

  1. Prepare a Valkey with one primary node and at least one replica node.
  2. Write keys with expiration directly to the writable replica node.
  3. Promote this replica to primary before these keys expire.
  4. Monitor the memory usage after these keys have expired.
  5. Verify that the memory usage does not decrease as expected (even after expired keys should be cleaned up), indicating a memory leak.

Expected behavior

When a writable replica node is promoted to primary, the replicaKeysWithExpire on this node should be properly cleaned up. No abnormal memory leaks should occur, and the memory usage should align with the actual valid key data size.

Additional information

Long-running nodes may experience excessive memory usage, leading to OOM (Out-of-Memory) errors in extreme cases. The memory leak is only triggered when a replica with expired key writes is promoted to primary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions