Skip to content

OPDATA-6745: Fix memory leak in Requester#805

Merged
dskloetc merged 4 commits into
mainfrom
kloet/fix-oom
Jun 5, 2026
Merged

OPDATA-6745: Fix memory leak in Requester#805
dskloetc merged 4 commits into
mainfrom
kloet/fix-oom

Conversation

@dskloetc
Copy link
Copy Markdown
Contributor

@dskloetc dskloetc commented Jun 3, 2026

OPDATA-6745

Description

There is a memory leak in Requester.

If requests come in frequently enough that the requester queue is never empty, even if it stays at a small size, the recursive processNext function will chain Promise objects forever. Only when the request queue is empty, does the promise stack unravel to release the memory.

This happens in por-address-list, the-network-firm, tiingo and to a lesser extent finnhub-secondary because they have multiple endpoint making multiple requests. This means that even though one endpoint waits for all its requests to finish before sending a new batch, there is always a batch from another endpoint still in the queue to keep the promise chain from unraveling.

This PR fixes the memory leak by refactoring the Requester, in particular the queueing process.

Instead of dropping requests in a queue and separately handling the queue, the code is more procedural with each "thread" taking care of its own request and a queue that only coordinates how requests wait on each other to satisfy the rate limiter.

Changes

  1. Add turn-queue.ts. This coordinates the different "threads" waiting on the rate limited sequentially.
  2. Add tests for TurnQueue.
  3. Refactor Requester using the TurnQueue.
  4. Update 2 tests that were observing bugs and 1 test that seemed to be observing a bug but has behavior we want to keep.

Testing

Tested locally with the-network-firm with all 24 feeds from RDD.
We'll confirm that it fixes the memory leak when we test in staging after merging.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

NPM Publishing labels 🏷️

🟢 This PR has valid version labels and will cause a patch bump.

@dskloetc dskloetc changed the title Fix memory leak in Requester. Fix memory leak in Requester Jun 3, 2026
@dskloetc dskloetc force-pushed the kloet/fix-oom branch 2 times, most recently from 53046de to 4d26405 Compare June 3, 2026 14:58
@dskloetc dskloetc marked this pull request as ready for review June 3, 2026 15:02
@dskloetc dskloetc requested a review from a team as a code owner June 3, 2026 15:02
@dskloetc dskloetc marked this pull request as draft June 3, 2026 15:02
@dskloetc dskloetc added the patch label Jun 3, 2026
@dskloetc dskloetc marked this pull request as ready for review June 3, 2026 16:08
@dskloetc dskloetc changed the title Fix memory leak in Requester OPDATA-6745: Fix memory leak in Requester Jun 3, 2026
Comment thread src/util/turn-queue.ts
Comment thread src/util/requester.ts Outdated
Comment thread src/util/turn-queue.ts Outdated
Comment thread src/util/requester.ts
Comment thread src/util/requester.ts
@dskloetc dskloetc requested a review from mmcallister-cll June 5, 2026 10:33
@dskloetc dskloetc merged commit df86c38 into main Jun 5, 2026
15 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 5, 2026

🚀 Successfully created version bump PR: #813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants