mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
fix: resolve random crashes, memory leaks, and thread explosion
This commit is contained in:
1 parent
c330a7b605
commit
d23c8e51a4
5 files changed
+163
-31
No files matched your search
+6
-2
@@ -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));
|
||||
}
|
||||
|
||||
// ═════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
Reference in new issue
Block a user