Skip to content

Memory Leak Suspected in y-redis during Repeated WebSocket Connections and Disconnections #24

@totorofly

Description

@totorofly

Checklist

Describe the bug
I am observing a potential memory leak in the y-redis module that becomes apparent during repeated connections and disconnections to the same room ID. Memory usage continuously increases with each connect and disconnect cycle, especially when handling large ProseMirror documents over WebSocket connections. This eventually leads to an out-of-memory error in Node.js.

To Reproduce

  1. Set up a WebSocket server with y-redis using the registerYWebsocketServer function.
  2. Implement a setInterval to log memory usage every 10 seconds:
setInterval(() => {
    const memoryUsage = process.memoryUsage();
    const memoryInMB = {
      rss: (memoryUsage.rss / 1024 / 1024).toFixed(2) + " MB",
      heapTotal: (memoryUsage.heapTotal / 1024 / 1024).toFixed(2) + " MB",
      heapUsed: (memoryUsage.heapUsed / 1024 / 1024).toFixed(2) + " MB",
      external: (memoryUsage.external / 1024 / 1024).toFixed(2) + " MB",
    };
    console.log(`【${new Date().toLocaleString()}】`, "###connections counts###", connections.size, "memory:", memoryInMB);
    // @ts-ignore
    heapdump.writeSnapshot((err, filename) => {
      console.log("Heap dump written to", filename);
    });
}, 10000);
  1. Use a ProseMirror document larger than 1MB.
  2. Open and close a WebSocket connection from the frontend multiple times to the same room ID.
  3. Observe the memory usage as it increases with each connect/disconnect cycle.

Expected behavior
I expected that the memory would stabilize after initial allocations given that the connections are being properly closed on the frontend.

Observed Behavior

The heapUsed metric starts at a few tens of megabytes and continuously climbs with each cycle of connecting and disconnecting to the same room ID. This increase does not stabilize or decrease, even after connections are properly closed, leading to a progressive memory buildup. Eventually, the memory usage surpasses 4GB, resulting in a Node.js out-of-memory error and the termination of the program.

Additional context

I have not yet pinpointed the exact cause but am suspecting issues in how resources are handled upon disconnection in the y-redis setup. I am currently unsure how to further investigate and identify the specific root cause of this memory leak, so I am reporting this issue to seek guidance and possibly get more insights into what might be causing this behavior. Any assistance on how to proceed with diagnosing this problem would be greatly appreciated.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions