Skip to content

Cron: Make response flushing optional#12222

Open
aldavigdis wants to merge 2 commits into
WordPress:trunkfrom
aldavigdis:make-response-flush-optional-in-cron
Open

Cron: Make response flushing optional#12222
aldavigdis wants to merge 2 commits into
WordPress:trunkfrom
aldavigdis:make-response-flush-optional-in-cron

Conversation

@aldavigdis

Copy link
Copy Markdown

A client's site has been having difficulties running wp-cron jobs. Within 1 second, the wp-cron process get killed. This usually happens in unsuspecting places such as the middle of an SQL request or REST API call, but almost always within the first 1-2 seconds.

However, the exact same functions can be run as a foreground process in PHP without issue.

This has caused issues with Action Scheduler, WooCommerce lookup tables have become out of sync and my own ERP sync solution for WordPress stopped sync.

Running the site locally in PHP-FPM does not reproduce the issue.

I have traced the issue to wp-cron calling litespeed_finish_request() when that function is available. However in this case, the LSPHP web server seems to kill the resulting child process within a second from spawning it.

This also seems to cause some memory leaks and swapping in the LSPHP web server.

The issue has been mended on the client's site by commenting out the following code block in wp-cron.php:

if ( function_exists( 'fastcgi_finish_request' ) ) {
	fastcgi_finish_request();
} elseif ( function_exists( 'litespeed_finish_request' ) ) {
	litespeed_finish_request();
}

In order to prevent this and similar issues, I suggest adding a new config constant; WP_CRON_FLUSH, defaulting to true to optionally enable flushing wp-cron response data and continuing in the background.

I also suggest WP_CRON_IGNORE_ABORT, defaulting to true to indicate whether or not to call ignore_user_abort( true ) as it may cause similar issues.

It has taken me a while to trace the issue to wp-cron and LSPHP as this had all the symptoms of a memory constrained web server.

I have communicated this issue with the relevant hosting provider as I am uncertain if this is isolated to their hosting packages or if this is more widespread. However, I have concluded that is warrants a patch to the WP Core to make flushing response data and spawning a child process using fastcgi_finish_reques() or litespeed_finish_request() optional.

An added benefit to this is this makes it easier and more straightforward to debug wp-cron jobs, as WP_DEBUG_DISPLAY can be set to true along with WP_CRON_FLUSH to false. In addition, tasks that should take more than a couple of seconds but crash can be identified using browser tools or cron without running something like New Relic to identify issues.

I don't expect this to require unit tests as this concerns specific application servers.

Trac ticket: https://core.trac.wordpress.org/ticket/65474

Use of AI Tools

No AI tools used.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

A client's site has been having difficulties running wp-cron jobs. Within 1 second, the wp-cron process get killed. This usually happens in unsuspecting places such as the middle of an SQL request or REST API call, but almost always within the first 1-2 seconds.

However, the exact same functions can be run as a foreground process in PHP without issue.

This has caused issues with Action Scheduler, WooCommerce lookup tables have become out of sync and my own ERP sync solution for WordPress stopped sync.

Running the site locally in PHP-FPM does not reproduce the issue.

I have traced the issue to wp-cron calling `litespeed_finish_request()` when that function is available. However in this case, the LSPHP web server seems to kill the resulting child process within a second from spawning it.

This also seems to cause some memory leaks and swapping in the LSPHP web server.

The issue has been mended on the client's site by commenting out the following code block in wp-cron.php:i

```php
if ( function_exists( 'fastcgi_finish_request' ) ) {
	fastcgi_finish_request();
} elseif ( function_exists( 'litespeed_finish_request' ) ) {
	litespeed_finish_request();
}
```

In order to prevent this and similar issues, I suggest adding a new config constant; `WP_CRON_FLUSH`, defaulting to true to optionally enable flushing wp-cron response data and continuing in the background.

I also suggest `WP_CRON_IGNORE_ABORT`, defaulting to true to indicate whether or not to call `ignore_user_abort( true )` as it may cause similar issues.

It has taken me a while to trace the issue to wp-cron and LSPHP as this had all the symptoms of a memory constrained web server.

I have communicated this issue with the relevant hosting provider as I am uncertain if this is isolated to their hosting packages or if this is more widespread. However, I have concluded that is warrants a patch to the WP Core to make flushing response data and spawning a child process using `fastcgi_finish_reques()` or `litespeed_finish_request()` optional.

An added benefit to this is this makes it easier and more straightforward to debug wp-cron jobs, as `WP_DEBUG_DISPLAY` can be set to true along with `WP_CRON_FLUSH` to false. In addition, tasks that should take more than a couple of seconds but crash can be identified using browser tools or cron without running something like New Relic to identify issues.

I don't expect this to require unit tests as this concerns specific
application servers.

The issue has been mended by commenting out the following code block in wp-cron.php:
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aldavigdis.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@aldavigdis aldavigdis force-pushed the make-response-flush-optional-in-cron branch from cf21898 to 764ad5d Compare June 18, 2026 23:30
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant