Skip to content

Instantly share code, notes, and snippets.

@legraphista
Last active May 22, 2018 12:10
Show Gist options
  • Save legraphista/67500d8dd798b2de0cdfdd2ada5aa947 to your computer and use it in GitHub Desktop.
Save legraphista/67500d8dd798b2de0cdfdd2ada5aa947 to your computer and use it in GitHub Desktop.
debug a node that's not terminating
const async_hooks = require('async_hooks');
let indent = 0;
const fs = require('fs');
const map = new Map();
async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
const eid = async_hooks.executionAsyncId();
const indentStr = ' '.repeat(indent);
fs.writeSync(
1,
`${indentStr}${type}(${asyncId}):` +
` trigger: ${triggerAsyncId} execution: ${eid}\n`);
map.set(asyncId, {type, triggerAsyncId, resource, stack : new Error().stack});
},
before(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}before: ${asyncId}\n`);
indent += 2;
},
after(asyncId) {
indent -= 2;
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}after: ${asyncId}\n`);
},
destroy(asyncId) {
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`);
map.delete(asyncId);
},
promiseResolve(asyncId){
const indentStr = ' '.repeat(indent);
fs.writeSync(1, `${indentStr}resolve: ${asyncId}\n`);
map.delete(asyncId);
}
}).enable();
setTimeout(() => {
debugger;
console.log(map);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment