Files
OpenFrontIO/src/server/PollingLoop.ts
T
Himansu Rawal e1d31ef1ee fix: replace setInterval with recursive setTimeout in Master.ts to pr… (#2869)
If this PR fixes an issue, link it below. If not, delete these two
lines.
Resolves #2868 

## Description:

This PR addresses a critical memory leak in the Master server process
(causing ~30GB RAM usage).

The issue was caused by `setInterval` calling `fetchLobbies()` every
100ms. When `fetchLobbies` took longer than 100ms to complete (due to
network latency or load), requests would pile up indefinitely, creating
a massive queue of pending Promises and open sockets.

I have refactored the polling logic into a generic `startPolling`
utility (in `src/server/PollingLoop.ts`) that uses a recursive
`setTimeout` pattern. This ensures that the next `fetchLobbies` call is
only scheduled *after* the previous one has completed (successfully or
failed), preventing any request pile-up.

## Please complete the following:

- [x] I have added screenshots for all UI updates (N/A - backend only)
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file (N/A - no user facing text)
- [x] I have added relevant tests to the test directory
(`tests/PollingLoop.test.ts`)
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

codimo
2026-01-14 09:50:43 -08:00

25 lines
750 B
TypeScript

import { logger } from "./Logger";
const log = logger.child({ comp: "polling" });
/**
* Starts a polling loop that executes the given async task effectively recursively using setTimeout.
* This guarantees that the next execution only starts after the previous one has completed (or failed),
* preventing request pile-ups.
*
* @param task The async function to execute.
* @param intervalMs The delay in milliseconds before the next execution.
*/
export function startPolling(task: () => Promise<void>, intervalMs: number) {
const runLoop = () => {
task()
.catch((error) => {
log.error("Error in polling loop:", error);
})
.finally(() => {
setTimeout(runLoop, intervalMs);
});
};
runLoop();
}