Skip to content

Fix socket event listener leak, pin Node version, and standardize formatting#22

Open
0xHristo wants to merge 1 commit intowebuniverseio:masterfrom
0xHristo:fix/issue-21
Open

Fix socket event listener leak, pin Node version, and standardize formatting#22
0xHristo wants to merge 1 commit intowebuniverseio:masterfrom
0xHristo:fix/issue-21

Conversation

@0xHristo
Copy link

@0xHristo 0xHristo commented Dec 21, 2025

Memory Leak Fix and Tooling Standardization

Summary

This MR addresses a memory leak in the socket event listener setup, pins the supported Node.js range for test stability, and standardizes formatting using Prettier and VS Code settings.

Fixes #21


Background / Motivation

We have been using this package in production with a keepAlive agent for a long time.
More than a year ago, we started hitting a gradual memory leak that manifested as:

  • Growing numbers of event listeners on the same socket
  • MaxListenersExceededWarning warnings
  • Increasing memory usage over long‑running processes

At the time, this was particularly difficult to diagnose because everything appeared correct at the request level. The root cause turned out to be socket reuse combined with duplicate listener attachment on each request.

We ultimately had to identify, debug, and mitigate the issue ourselves in production by introducing a per‑socket guard to prevent re‑instrumentation.

After running with that fix successfully for a long period of time, it felt appropriate to give back to the community by upstreaming the solution, adding coverage, and improving tooling to help prevent regressions.


What’s Included

  • Add a socket.haveEvents guard to prevent duplicate socket event listeners and cover the behavior with tests.
  • Pin supported Node.js versions to >=15.14.0 <24 in package.json to ensure stable and reproducible test results.
  • Add a Prettier configuration to enforce consistent formatting across the codebase.
  • Add VS Code workspace settings to disable the default JavaScript formatter and ensure Prettier is used on save.

How to Verify

  1. Temporarily remove or comment out the socket.haveEvents guard located at line 16 in index.js, then run the test suite and observe the listener leak.
  2. Re-enable the guard at line 16 in index.js and confirm the leak is resolved.
  3. Run the following commands:
yarn test
yarn format   # optional

All tests should pass, and formatting should remain stable across saves.


Disclaimer

The issue description and this MR description were drafted with the assistance of ChatGPT for clarity and structure.
The technical investigation, reproduction, production mitigation, and final implementation were performed manually.


Notes

This change ensures socket reuse with keep-alive agents remains safe over time and prevents listener accumulation that could otherwise lead to memory leaks and warnings.

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.

Socket event listeners are attached multiple times, causing a memory leak with keepAlive agents

1 participant