r/javascript • u/ksneteto • 20d ago
Got tired of scattering console.log('here 1') to find promises that hang silently, so I built a zero-dep dev tool to trace them
https://github.com/YourDarkstFear/hang-trace
1
Upvotes
2
u/raks1991 16d ago
A never-settling promise doesn't keep Node alive by itself. If nothing else is pending, this exits with code 0:
That matters for hang-trace: the diagnostic timer is unref'd, so in this case Node exits before it fires and the hang never gets reported. Top-level await instead exits with code 13, with a "Detected unsettled top-level await" warning on Node 22+. For a process that really stays stuck,
process.getActiveResourcesInfo()names the resources holding the event loop open.1
u/azhder 20d ago
Good idea.
I've made such wrappers in a previous project, they were primarily for promises that were left for years without even a `.catch` (yup, that much silent), but I didn't make many of them report if they hang - only a single spot where calls to BE were being made. This looks nice, the other half of the problem.
Not so good idea.
The first time I noticed something like `process.env.NODE_ENV === 'production'` I thought it was a nice and polite thing people did for others. Now I'm not so sure. It's bordering "bad design" since it's a hidden input.
I'm not talking about "hidden input" as something not explained in the documentation, the readme file etc. I'm talking in terms of functional programming i.e. the output depends only on the arguments you give it.
If your entire library changes behavior not because of the arguments you have given it, like a configuration object, but an outside environment change... Let's consider this. What if I want it to work if `NODE_ENV` is set as `production` due to reasons (legacy software can be one example)?
1
u/h_emily_3279 11d ago
Would a per-trace timeout make sense? A long poll and a genuinely stuck request need different definitions of 'hung'.
1
u/oneeyedziggy 20d ago
Mostly look for any thens without catches and you're most of the way there... But thanks for making a tool... Probably beats manual search... (though not as good as a blocking lint rule on a husky commit hook... Ifk if that's a thing... I just don't want the path to have to be exercised in order to be found )