fix: resolve random crashes, memory leaks, and thread explosion

This commit is contained in:
NoLogicAlan committed 2026-05-03 16:30:16 +03:00
1 parent c330a7b605
commit d23c8e51a4
5 files changed
+163 -31

No files matched your search

+6 -2
View File
@@ -363,10 +363,14 @@ export class Utils {
* @returns {Promise<T>}
*/
static timeout(promise, ms, message = "Operation timed out") {
// Always clear the timeout timer once the race settles — whether promise wins or
// the timeout wins. Without this, every successful call leaks a live timer for the
// full `ms` duration, holding the timer handle in the event loop and bloating heap.
let timerId;
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error(message)), ms);
timerId = setTimeout(() => reject(new Error(message)), ms);
});
return Promise.race([promise, timeoutPromise]);
return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timerId));
}
// ═════════════════════════════════════════════════════════════════════════════