Skip to content

Commit 8bffd5b

Browse files
Alexia-Soareclaude
andcommitted
fix: let real database errors surface when marking an action failed
The guard caught every Exception, so a database error inside `mark_failure()` was swallowed together with the race it targets. The store throws the same InvalidArgumentException for both; only `$wpdb->last_error` tells them apart. Catch that type only and rethrow when the database reported an error. Tests: one-shot guard in the query interceptor, so an injected UPDATE cannot re-enter it; a pattern that accepts quoted ids; a test that breaks the UPDATE and expects the exception to surface. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 87c183c commit 8bffd5b

2 files changed

Lines changed: 89 additions & 13 deletions

File tree

‎patches/action-scheduler-1369-mark-failures.patch‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
--- a/classes/ActionScheduler_QueueCleaner.php
22
+++ b/classes/ActionScheduler_QueueCleaner.php
3-
@@ -222,7 +222,12 @@
3+
@@ -204,6 +204,7 @@
4+
* @param int $time_limit The number of seconds to allow an action to run before it is considered to have failed. Default 300 (5 minutes).
5+
*/
6+
public function mark_failures( $time_limit = 300 ) {
7+
+ global $wpdb;
8+
$timeout = apply_filters( 'action_scheduler_failure_period', $time_limit );
9+
10+
if ( $timeout < 0 ) {
11+
@@ -222,7 +223,15 @@
412
);
513

614
foreach ( $actions_to_reset as $action_id ) {
715
- $this->store->mark_failure( $action_id );
816
+ try {
917
+ $this->store->mark_failure( $action_id );
10-
+ } catch ( Exception $e ) {
11-
+ // Deleted or changed by another process between the query and the update: nothing left to mark.
18+
+ } catch ( InvalidArgumentException $e ) {
19+
+ if ( ! empty( $wpdb->last_error ) ) {
20+
+ throw $e;
21+
+ }
22+
+ // No database error: another process deleted or already failed this action.
1223
+ continue;
1324
+ }
1425
do_action( 'action_scheduler_failed_action', $action_id, $timeout );
1526
}
1627
}
1728
--- a/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php
1829
+++ b/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php
19-
@@ -132,7 +132,11 @@
30+
@@ -131,8 +131,16 @@
31+
* @return void
2032
*/
2133
private function handle_action_error( $action_id, $e, $context, $valid_action ) {
34+
+ global $wpdb;
2235
if ( $valid_action ) {
2336
- $this->store->mark_failure( $action_id );
2437
+ try {
2538
+ $this->store->mark_failure( $action_id );
26-
+ } catch ( Exception $ignored ) {
27-
+ // Already marked failed by the queue cleaner, or deleted by another process: nothing left to mark.
39+
+ } catch ( InvalidArgumentException $mark_failure_error ) {
40+
+ if ( ! empty( $wpdb->last_error ) ) {
41+
+ throw $mark_failure_error;
42+
+ }
43+
+ // No database error: the cleaner already marked it failed, or another process deleted it.
2844
+ }
2945
/**
3046
* Runs when action execution fails.

‎tests/test-action-scheduler-mark-failure.php‎

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,55 @@ private function seed_stale_running_action() {
8282
* @param callable $other_process Runs once, right before that UPDATE.
8383
*/
8484
private function mark_failures_racing( $action_id, callable $other_process ) {
85-
global $wpdb;
86-
$table = $wpdb->actionscheduler_actions;
87-
$filter = static function ( $sql ) use ( $action_id, $table, $other_process ) {
88-
if ( 0 === stripos( ltrim( $sql ), 'UPDATE' ) && false !== strpos( $sql, $table ) && preg_match( '/action_id` = (\d+)/', $sql, $m ) && (int) $m[1] === $action_id ) {
85+
$this->intercept_mark_failure_update(
86+
$action_id,
87+
static function ( $sql ) use ( $other_process ) {
8988
$other_process();
89+
return $sql;
90+
}
91+
);
92+
( new ActionScheduler_QueueCleaner( $this->store ) )->mark_failures( 60 );
93+
}
94+
95+
/**
96+
* Run `$intercept` once, on the UPDATE that marks `$action_id` failed, and
97+
* use its return value as the SQL to execute. Removed after the test.
98+
*
99+
* @param int $action_id Action whose UPDATE is intercepted.
100+
* @param callable $intercept Receives the SQL, returns the SQL to run.
101+
*/
102+
private function intercept_mark_failure_update( $action_id, callable $intercept ) {
103+
global $wpdb;
104+
$table = $wpdb->actionscheduler_actions;
105+
$done = false;
106+
// Queries issued inside $intercept re-enter this filter: run it once only.
107+
$filter = static function ( $sql ) use ( $action_id, $table, $intercept, &$done ) {
108+
if ( ! $done && 0 === stripos( ltrim( $sql ), 'UPDATE' ) && false !== strpos( $sql, $table ) && preg_match( '/action_id`?\s*=\s*\'?(\d+)/', $sql, $m ) && (int) $m[1] === $action_id ) {
109+
$done = true;
110+
return $intercept( $sql );
90111
}
91112
return $sql;
92113
};
93114
add_filter( 'query', $filter );
94-
try {
95-
( new ActionScheduler_QueueCleaner( $this->store ) )->mark_failures( 60 );
96-
} finally {
115+
$this->filters_to_remove[] = $filter;
116+
}
117+
118+
/**
119+
* Query filters added by intercept_mark_failure_update().
120+
*
121+
* @var callable[]
122+
*/
123+
private $filters_to_remove = array();
124+
125+
/**
126+
* Remove the query filters even when a test throws.
127+
*/
128+
public function tear_down() {
129+
foreach ( $this->filters_to_remove as $filter ) {
97130
remove_filter( 'query', $filter );
98131
}
132+
$this->filters_to_remove = array();
133+
parent::tear_down();
99134
}
100135

101136
/**
@@ -170,4 +205,29 @@ static function () use ( $wpdb, $action_id ) {
170205

171206
$this->assertSame( ActionScheduler_Store::STATUS_FAILED, $this->status_of( $action_id ) );
172207
}
208+
209+
/**
210+
* The guard is for the race only. A real database error while marking the
211+
* action failed must still surface, as it did before the patch.
212+
*/
213+
public function test_mark_failures_still_throws_on_a_database_error() {
214+
global $wpdb;
215+
$stale = $this->seed_stale_running_action();
216+
217+
// Break the UPDATE itself: the store gets `false`, not zero rows.
218+
$this->intercept_mark_failure_update(
219+
$stale,
220+
static function ( $sql ) use ( $wpdb ) {
221+
return str_replace( $wpdb->actionscheduler_actions, 'no_such_table', $sql );
222+
}
223+
);
224+
$wpdb->suppress_errors( true );
225+
226+
$this->expectException( InvalidArgumentException::class );
227+
try {
228+
( new ActionScheduler_QueueCleaner( $this->store ) )->mark_failures( 60 );
229+
} finally {
230+
$wpdb->suppress_errors( false );
231+
}
232+
}
173233
}

0 commit comments

Comments
 (0)