c8: Running tests with coverage causes CPU to hit 100% after tests finish

  • Version: v18.14.0
  • Platform: windows-latest, ubuntu-latest and macos-latest GitHub CI images

When running tests with coverage in CI, after the test run completes successfully, CPU spikes to 100% and the process hangs.

It happened in this build: https://github.com/ipfs/aegir/actions/runs/4225847659/jobs/7338650232 and sometimes happens locally too, though it’s a lot more frequent in CI.

This started happening with node 18, it didn’t seem to happen with node 16.

The command being run is similar to:

c8 --reporter json --report-dir .coverage --temp-directory /tmp/some-random-string --clean mocha test/node.{js,cjs,mjs} test/**/*.spec.{js,cjs,mjs} dist/test/node.{js,cjs,mjs} dist/test/**/*.spec.{js,cjs,mjs} --ui bdd --require source-map-support/register --timeout=60000 --bail

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 3
  • Comments: 20 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I wonder if this could be related to https://github.com/nodejs/node/issues/49344 we should keep an eye and see if the problem continues once patched.

The workaround that helped me is to enable the exit mode in Mocha (--exit on command line or "exit": true in the config).

Seems that there is some mutual issue in handling the exit between c8 and Mocha running as a child process. I suspected that there is some loop or wait process on the Mocha side waiting for something to finish after waiting for the tests.

I was unable to attach the debugger to the Mocha child process, Node seems to be in the phase when it does not accept debugger connections anymore.

Then I tried wtfnode in attempt to dump the remaining handlers. The only way of using wtfnode that worked for me is to insert wtf.dump() in the the Mocha’s exitMochaLater handler. It gave basically empty result for me:

[WTF Node?] open handles:
- File descriptors: (note: stdio always exists)
  - fd 2 (tty) (stdio)
  - fd 1 (tty) (stdio)
  

If I’m not missing anything, then I guess what happens is that Mocha’s process gets stalled waiting until the stdout pipe is read just before exiting, while the c8 process perhaps is not reading enough from this pipe to let Mocha’s Node process finish. The workaround helps by making Mocha flush and exit the process explicitly after completing the tests. See the flag description for the dangers of the exit mode: https://mochajs.org/#-exit

I’m using macOS, btw, and the issue is occurring ~10% of time for me locally.