Skip to content

Spurious error: "A worker process has failed to exit gracefully and has been force exited." #11354

Closed as not planned

Description

@octogonz

🐛 Bug Report

Our CI builds have been failing sporadically with this error from Jest:

"A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks."

--detectOpenHandles does not detect any problems. The error is reported randomly by various projects that build independently in a large monorepo, which suggests that this is not actually caused by a bad test.

It occurs more frequently on Windows machines, and on machines with heavier load. But it has also been reported on individual developer MacBooks.

To Reproduce

You can trivially force the warning to be reported by reducing this constant to 0:

https://github.com/facebook/jest/blob/64d5983d20a628d68644a3a4cd0f510dc304805a/packages/jest-worker/src/base/BaseWorkerPool.ts#L20

Investigation

The FORCE_EXIT_DELAY constant is used by this code:

https://github.com/facebook/jest/blob/64d5983d20a628d68644a3a4cd0f510dc304805a/packages/jest-worker/src/base/BaseWorkerPool.ts#L104-L116

500 ms seems way too small for a machine that is under heavy load. My theory is that if we increase the timeout, then the IPC message will eventually be received, and the warnings will go away. I will report back after I have tested that.

jest-haste-map may be the root cause

While debugging this, I noticed that the jest-haste-map library has an obvious flaw where it creates a potentially unlimited number of promises while crawling the disk:

https://github.com/facebook/jest/blob/4fa17e455662dc7918eecdb857dd946e9ce2dc53/packages/jest-haste-map/src/index.ts#L659-L678

For example, I observed that Promise.all() may create 50+ promises that are all crawling the filesystem in parallel. If the build orchestrator is building 8 projects in parallel, and each projects is potentially creating 50 Jest promises, that could obviously thrash the disk to the point where the FORCE_EXIT_DELAY limit is exceeded.

This is just a speculation -- I did not have time to try fixing this to see whether it resolves the problem or not.

Either way, it would be a good idea to apply some throttling to _buildHasteMap() to limit the parallelism. @cpojer @SimenB

envinfo

  System:
    OS: Windows 10 10.0.19041
    CPU: (8) x64 AMD Ryzen 7 3700X 8-Core Processor
  Binaries:
    Node: 12.20.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.14.10 - C:\Program Files\nodejs\npm.CMD

Activity

  1. changed the title [-]Spurious warning: "A worker process has failed to exit gracefully and has been force exited."[/-] [+]Spurious error: "A worker process has failed to exit gracefully and has been force exited."[/+] on Apr 28, 2021
  2. madhur1846 commented on Apr 29, 2021

    @madhur1846

    Why this is opened after merged?

  3. octogonz commented on Apr 29, 2021

    @octogonz
    Author

    If you are referring to microsoft/rushstack#2653, that is a workaround implemented in a project that is external to Jest.

    The Jest work tracked by #11354 is:

    1. Increase FORCE_EXIT_DELAY to a much larger value or make it a configurable option.
    2. Fix the performance issue in jest-haste-map by throttling the promise.
  4. octogonz commented on Apr 29, 2021

    @octogonz
    Author

    My theory is that if we increase the timeout, then the IPC message will eventually be received, and the warnings will go away. I will report back after I have tested that.

    Following up to confirm that patching FORCE_EXIT_DELAY to be 7000 (7 seconds) has mitigated our repro. The CI job that was failing 40% of the time has passed 10 consecutive runs. That's pretty good evidence for this theory.

  5. jconway325 commented on Jun 21, 2021

    @jconway325

    @octogonz , thanks for doing the investigations here.
    Can you please let me know the work around to fix this issue in the normal Node.js project with jest environment?

  6. octogonz commented on Jun 21, 2021

    @octogonz
    Author

    My patch was implemented in the Jest task from the Heft toolchain. I don't know of an easy workaround for regular Jest.

  7. jconway325 commented on Jun 21, 2021

    @jconway325

    Thank you @octogonz for the heads up.

  8. r00b commented on Jul 15, 2021

    @r00b

    I am also able to repro @octogonz's issue and was able to fix it locally by changing FORCE_EXIT_DELAY in BaseWorkerPool within jest-worker.

  9. satyamamit commented on Aug 6, 2021

    @satyamamit

    any ETA for resolving/closing this bug ?

  10. stbenjam commented on Aug 11, 2021

    @stbenjam

    I'm hitting the same, and it's resolved with a higher FORCE_EXIT_DELAY.

    My horrible solution in package.json:

    "test": "sed -i -e 's/const FORCE_EXIT_DELAY = 500;/const FORCE_EXIT_DELAY = 7000;/g' ./node_modules/jest-worker/build/base/BaseWorkerPool.js && react-scripts test # See https://github.com/facebook/jest/issues/11354",
    
  11. stbenjam commented on Aug 11, 2021

    @stbenjam

    While debugging this, I noticed that the jest-haste-map library has an obvious flaw where it creates a potentially unlimited number of promises while crawling the disk

    Rather naive but I added the following to ./node_modules/jest-haste-map/build/index.js:

    if (promises.length > 0) {
      throw new Error(promises.length)
    }

    It is being called since I do hit it if I remove the conditional, but otherwise in my environment I never hit this -- so the unlimited promises theory isn't happening at least for me.

  12. octogonz commented on Aug 14, 2021

    @octogonz
    Author

    in my environment I never hit this

    I measured something like 50 concurrent promises. The behavior probably depends on how it is invoked (jest-haste-map has several modes of operation and filtering options) and how deeply nested is the directory tree that it recurses.

  13. 4 remaining items

  14. tomgransden commented on Jan 1, 2022

    @tomgransden

    I have also been experiencing the same issue:

    • Random failures with "A worker process has failed to exit" error
    • --detectOpenHandles doesn't detect anything
    • --runInBand works fine

    Changing the FORCE_EXIT_DELAY to something higher than 500 fixes the issue.

  15. avidan-chen commented on Jun 23, 2022

    @avidan-chen

    Seeing the same issue:

    Random failures with "A worker process has failed to exit" error
    --detectOpenHandles doesn't detect anything
    --runInBand works fine
    Changing the FORCE_EXIT_DELAY to something higher than 500 fixes the issue.

  16. tjenkinson commented on Aug 15, 2022

    @tjenkinson
    Contributor

    Hey #13139 might fix this. It sounds like the same symptoms.

    Please could you try adding a

    setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],

    to your config and in jest-setup.js put

    afterAll(() => {
      global.gc();
    });

    and then run jest with --expose-gc, so probably node --expose-gc ./node_modules/.bin/jest ?

    Does that fix it?

  17. avidan-chen commented on Aug 20, 2022

    @avidan-chen

    @tjenkinson I am unable to replicate the error at this time. Not sure what changed, whether it's my code, Jest version or other dependencies.

  18. cfry2 commented on Sep 30, 2022

    @cfry2

    Hey #13139 might fix this. It sounds like the same symptoms.

    Please could you try adding a

    setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],

    to your config and in jest-setup.js put

    afterAll(() => {
      global.gc();
    });

    and then run jest with --expose-gc, so probably node --expose-gc ./node_modules/.bin/jest ?

    Does that fix it?

    Oh my god after spending days with this issue, calling gc fixed it for me! Like OP using --detectOpenHandles magically fixes the issue and the issue only seems to occur when running a larger amount of tests. I spent a lot of time trying to pinpoint the issue to a faulty test by running subsets of my tests but the issue would disappear then as well. One thing to note is that I'm running almost 4000 tests so this issue might only present itself in codebases with a lot of tests?

  19. tjenkinson commented on Sep 30, 2022

    @tjenkinson
    Contributor

    Great! I wrote up a bit more here: #13139 (comment)

    I think next the simplest thing is probably increase the FORCE_EXIT_DELAY

  20. orofbrown commented on Jan 30, 2023

    @orofbrown

    Hi, just want to expand on this erorr a bit - I have seen this come up as well when running a medium to large number of tests at once. The reason --detectOpenHandles appears to solve it is because the tests are no longer being run in parallel. The problem is that the memory and/or CPU consumption of Jest is too high when running tests in parallel. It doesn't happen consistently, but it happens often enough to be a problem.

    And this isn't just a problem on platforms like Jenkins. The resource usage of Jest is an obvious problem if I try to do anything else while my unit tests are running on my dev machine as well.

  21. github-actions commented on Jan 30, 2024

    @github-actions

    This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

  22. github-actions commented on Feb 29, 2024

    @github-actions

    This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

  23. github-actions commented on Mar 31, 2024

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
    Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

  24. locked as resolved and limited conversation to collaborators on Mar 31, 2024
  25. SimenB commented on May 7, 2026

    @SimenB
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions