Skip to content

Instantly share code, notes, and snippets.

@SeanSobey
Created November 19, 2017 16:47
Show Gist options
  • Save SeanSobey/94495ac7ffb88380ab1ab2eeb3773fdc to your computer and use it in GitHub Desktop.
Save SeanSobey/94495ac7ffb88380ab1ab2eeb3773fdc to your computer and use it in GitHub Desktop.
Debug node timer leaks
const activeTimeoutIds = new Map();
const _setTimeout = setTimeout;
setTimeout = function (callback, ms, ...args) {
const timeoutId = _setTimeout(() => {
callback();
activeTimeoutIds.delete(timeoutId);
}, ms, ...args);
const stackTrace = (new Error()).stack;
activeTimeoutIds.set(timeoutId, 'ms: ' + ms + ', trace:' + stackTrace);
return timeoutId;
};
const _clearTimeout = clearTimeout;
clearTimeout = function (timeoutId) {
_clearTimeout(timeoutId);
activeTimeoutIds.delete(timeoutId);
}
function getTimeouts() {
return Array.from(activeTimeoutIds.values());
}
const _setInterval = setInterval;
const activeIntervalIds = new Map();
setInterval = function (callback, ms, ...args) {
const timeoutId = _setInterval(callback, ms, ...args);
const stackTrace = (new Error()).stack;
activeIntervalIds.set(timeoutId, 'ms: ' + ms + ', trace:' + stackTrace);
return timeoutId;
};
const _clearInterval = clearInterval;
clearInterval = function (timeoutId) {
_clearInterval(timeoutId);
activeIntervalIds.delete(timeoutId);
}
function getIntervals() {
return Array.from(activeIntervalIds.values());
}
console.log('timeouts', Array.from(getTimeouts()));
console.log('intervals', Array.from(getIntervals()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment