Skip to content

[Feature]: Allow Configuring FORCE_EXIT_DELAY for Worker Graceful Exit #15468

Closed as not planned

Description

@devcrev

🚀 Feature Proposal

When running Jest, the following warning may appear:

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 --detectOpenHandles to find leaks.

In some cases, the worker process may not have enough time to exit gracefully due to system load or resource constraints. The issue originates in the BaseWorkerPool.js file, where the FORCE_EXIT_DELAY constant is hardcoded to 500 milliseconds:

...

const FORCE_EXIT_DELAY = 500;

...

      // Schedule a force exit in case worker fails to exit gracefully so
      // await worker.waitForExit() never takes longer than FORCE_EXIT_DELAY
      let forceExited = false;
      const forceExitTimeout = setTimeout(() => {
        worker.forceExit();
        forceExited = true;
      }, FORCE_EXIT_DELAY);
      await worker.waitForExit();
      // Worker ideally exited gracefully, don't send force exit then
      clearTimeout(forceExitTimeout);
      return forceExited;

...

The current hardcoded delay may be insufficient in some environments, causing unnecessary forced exits and misleading warnings.

Motivation

In resource-constrained environments, such as CI/CD pipelines or heavily-loaded systems, workers may require more than 500 milliseconds to exit gracefully. Increasing the FORCE_EXIT_DELAY in these scenarios prevents misleading warnings and unnecessary forced exits, improving the overall testing experience.

The message ...

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 --detectOpenHandles to find leaks.

... can be especially misleading in these cases. A more accurate message would state the time threshold explicitly and provide a way to adjust it, such as:

A worker process has failed to exit gracefully within 500 milliseconds and has been force exited. This could indicate test leaks or insufficient teardown time. Try running with --detectOpenHandles or increase the forced exit threshold with the --forceExitDelay option.

Example

This feature would allow users to configure the FORCE_EXIT_DELAY value either via CLI:

jest --forceExitDelay:2500

... or in jest.config.js:

module.exports = {
    forceExitDelay:2500 // time in ms, so 2.5 seconds here
};

Pitch

The proposed feature improves test reliability in environments where workers may need more time to exit gracefully. Without this feature, users may encounter misleading warnings or forced exits, even in properly functioning setups.

Manually modifying FORCE_EXIT_DELAY in BaseWorkerPool.js has been shown to resolve these issues consistently. Making this configurable would empower users to fine-tune their setups without altering Jest's source code.

Activity

  1. devcrev commented on Feb 6, 2025

    @devcrev
    Author

    Following up on this issue—I've been running my test suites with an increased FORCE_EXIT_DELAY for three weeks now and have not encountered any warnings. My suite consists of 101 test suites, 1258 tests, and 23 snapshots, running in ~27 seconds. This change has resolved the misleading warnings without any adverse effects.

    Would love to hear any thoughts from the maintainers on this. Let me know if there's anything I can provide to help move this forward!

  2. github-actions commented on Mar 8, 2025

    @github-actions

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

  3. devcrev commented on Apr 7, 2025

    @devcrev
    Author

    Still warning free due to an increased FORCE_EXIT_DELAY. Results on a project, i.e., different from the one I reported on Feb 6...

    Seed:        1718925114
    Test Suites: 91 passed, 91 total
    Tests:       732 passed, 732 total
    Snapshots:   0 total
    Time:        28.589 s, estimated 64 s
    
  4. github-actions commented on May 7, 2025

    @github-actions

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

  5. github-actions commented on Jun 6, 2025

    @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.

  6. github-actions commented on Jun 6, 2025

    @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.

  7. github-actions commented on Jul 7, 2025

    @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.

  8. locked as resolved and limited conversation to collaborators on Jul 7, 2025
  9. 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