WordPress.org

Make WordPress Core

#51747 closed enhancement (duplicate)

wp_schedule_event fails if cron job is unfortunately running.

Reported by: Takahashi_Fumiki Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.4
Component: Cron API Keywords: needs-patch
Focuses: performance Cc:

Description

Some feature(e.g. scheduled posts) requires cron parameter to be stored in wp_options as single array.
But if wp cron job is running, this array may be overridden.
Writing in an easier format...

//wp-cron.php recognize cron jobs like below.
[
 'a',
 'b',
 'c',
 'z'
]
// 
foreach( $jobs as $job ) {
  // Oh, c should be done.
  // Reschedule or unschedule events.
  wp_reschedule_event( $job->event_name, $job->args );
  // Execute.
  $job->exec();
}
// Result corn jobs.
[
 'a',
 'b',
 'z',
]

If someone tries scheduling a new event simultaneously, it fails.

// Si
// in wp-admin/edit.php or /wp-json/wp/v2/posts
// with wp_insert_post or something,
// expected cron jobs.
[
  'a',
  'b',
  'c',
  'please_publish_my_new_post',
  'z,'
]

In this case, the actual result will be...

[
 'a',
 'b',
 'z'
]

Thus, the please_publish_my_new_post hook never happens, and the post will be marked as Missed schedule.

How to reproduce

I made a plugin to reproduce this. https://github.com/kuno1/nabeatsu-of-the-world

https://raw.githubusercontent.com/kuno1/nabeatsu-of-the-world/main/screenshot.png

This plugin will:

  • Register WP-Cron job runs every minute. The job sometimes take very long time(40 sec) for execution.
  • On front end, schedule new post in every 10 seconds.
  • You will get 360 scheduled posts in 1 hour and each of them should be published in 5 minutes from their creation.
  • But some of them are "Missed schedule"

Solution

  • Scheduling and execution should be split. Scheduling matters.

I'm working on a new patch.

Note

  • In some ticket( e.g. #49520, #15148 ), changing cron storage approach.
  • This frequently occurs in large sites which hold by many writers and posted many articles.
  • Some core jobs(e.g. removing old transients, empty trash) may also increase the probability.
  • Site with many customers with recurring payment(e.g. ) also have high probability.

Change History (2)

#1 @hellofromTonya
13 months ago

  • Version changed from trunk to 5.4

#2 @peterwilsoncc
12 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the report.

I'm going to close this as a duplicate if #51716 as although the issue described is subtly different, the root cause is the same: updating or using the cron option in two separate processes at the same time can cause the update in one of the processes to fail.

I'll add an note to the earlier ticket noting the issue described on this ticket.

Note: See TracTickets for help on using tickets.