Skip to content

Memory leak in _sendAug when timeout === 0 #16

Description

@thantsintoe

There is a memory leak in the _sendAug function in shardus-net (TypeScript side).

When timeout === 0 (meaning "no response expected" / fire-and-forget), the function still creates a 5-minute setTimeout that is never cleared.

Code Location

} else {
  const timer = setTimeout(() => {
    resolve({ success: false, error: 'Request timed out 2' })
  }, 300 * 1000) // 5 minutes
}

Root Cause

  • A setTimeout is created but the returned timer ID is not stored.
  • No clearTimeout() is ever called for this case.
  • The callback closes over resolve, augData, and other variables.
  • These timers accumulate over time, especially under high message throughput.

Impact

  • Gradual memory growth over time.
  • Increased GC pressure.
  • Potential Out-of-Memory crashes in long-running nodes.
  • Worse under high load (common in shardus-core multi-send scenarios).

Suggested Fix

  1. When timeout === 0, do not create any timeout (true fire-and-forget).
  2. Or, if a safety timeout is desired, store the timer ID and ensure it gets cleared when the send completes.
  3. Consistently store and clear all timers in the success path.

Example of clean handling:

if (timeout !== 0) {
  const timer = setTimeout(...);
  responseUUIDMapping[augData.UUID] = {
    callback: (...) => {
      clearTimeout(timer);
      onResponse(...);
    }
  };
} 
// else → no timer created

Additional Notes

  • The comment says: "a timeout of 0 means no return message is expected." — the code should respect this.
  • This affects both single and multi-send paths using _sendAug.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions