Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
devcrev opened this issue Jan 17, 2025 · 0 comments
Open

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

devcrev opened this issue Jan 17, 2025 · 0 comments

Comments

@devcrev
Copy link

devcrev commented Jan 17, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant