Make WordPress Core

Opened 2 years ago

Closed 18 months ago

Last modified 17 months ago

#56628 closed enhancement (fixed)

Cron memory limit is unnecessarily low

Reported by: iandunn's profile iandunn Owned by: johnbillion's profile johnbillion
Milestone: 6.3 Priority: normal
Severity: normal Version: 2.1
Component: Cron API Keywords: has-patch add-to-field-guide
Focuses: Cc:

Description

Problem

wp-cron.php uses the default WP_MEMORY_LIMIT, which is sometimes low enough that a cron job triggers an out-of-memory error.

In many of those cases, the amount of memory needed is greater than WP_MEMORY_LIMIT, but less than WP_MAX_MEMORY_LIMIT, and all a dev has to do is call wp_raise_memory_limit( 'admin' ) in their cron callback.

That still requires time debugging and fixing, though, and Core could prevent those types of errors from happening in the first place.

Proposed Solution

Since crons often consume extra memory by nature, I think it makes sense to give them the full amount available by default. We already do this with the admin area and image processing.

That could be done by adding a new cron context to wp_raise_memory_limit(), and calling wp_raise_memory_limit( 'cron' ) in wp-cron.php

Attachments (1)

56628.patch (2.2 KB) - added by thakkarhardik 2 years ago.

Download all attachments as: .zip

Change History (7)

#1 @johnbillion
2 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

@thakkarhardik
2 years ago

#2 @thakkarhardik
2 years ago

  • Keywords has-patch added; needs-patch removed

Hi @iandunn , I've added an initial patch for your above suggested changes.

#3 @iandunn
2 years ago

I haven't tested it yet, but the code for 56628.patch looks good, thanks Hardik!

It doesn't look like `wp_raise_memory_limit()` is very testable, so addition tests probably aren't needed here.

Do any component maintainers or committers have an opinion on this ticket? I don't work with the Cron internals much, so I want to make sure I'm not missing something.

#4 @johnbillion
21 months ago

  • Milestone changed from Future Release to 6.3
  • Owner set to johnbillion
  • Status changed from new to accepted

#5 @johnbillion
18 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 55871:

Cron API: Attempt to raise the PHP memory limit for cron event processing.

Since cron events often consume extra memory by nature, it makes sense to give them the full amount available by default. In practice this means the memory will be increased to WP_MAX_MEMORY_LIMIT (which is 256MB by default) during cron event processing if the default memory limit is lower than this value.

The new cron_memory_limit filter can be used to adjust this value if necessary.

Note that this change will not by default affect external means of processing cron events, such as the wp cron command in WP-CLI, server-level crontab events, or any other cron event processing mechanism that bypasses wp-cron.php.

Props iandunn, thakkarhardik

Fixes #56628

#6 @milana_cap
17 months ago

  • Keywords add-to-field-guide added
Note: See TracTickets for help on using tickets.