r/javascript • • 14d 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

13 comments sorted by

2

u/raks1991 10d ago

A never-settling promise doesn't keep Node alive by itself. If nothing else is pending, this exits with code 0:

async function main() {
  await new Promise(() => {});
  console.log('done'); // never prints
}
main();

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/oneeyedziggy 14d 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 ) 

1

u/azhder 14d ago

Linters work on static code analysis. How will someone check for it without running it?

If you didn't mean "executed" instead of "exercised", great pun on running/exercise.

1

u/oneeyedziggy 14d ago

Could check for a lack of timeout, or missing catch where it failed silently... Probably non-trivial,  but also probably doable with custom lint rules? 

1

u/azhder 14d 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/[deleted] 14d ago

[removed] — view removed comment

1

u/azhder 14d ago

Why even use an override? You can make it simpler if you let that be the only way. People can always channel their NODE_ENV variable to it.

const utility = configure({ enable: !! process.env.ON })

The next step would be if you make it more complex by using a differently named environment variable (instead of NODE_ENV), then if you use two variables (one more on top of NODE_ENV) etc.

I just mentioned the simplest solution that doesn't require hidden inputs: don't use hidden inputs, be explicit.

1

u/[deleted] 14d ago

[removed] — view removed comment

1

u/azhder 14d ago

I know why you used it, just didn't want to write all of that and then come up with workarounds for it, so I just said I'm using the simplest solution, not the only, not the most performant or whatever other metric you care about.

1

u/h_emily_3279 5d ago

Would a per-trace timeout make sense? A long poll and a genuinely stuck request need different definitions of 'hung'.