fix: race-safe add_processed_item via atomic upsert (#2626)#2627
Merged
Conversation
Concurrent Action Scheduler workers processing the same source item raced between the UPDATE and the bare INSERT in add_processed_item. The loser hit the flow_source_item unique key and wpdb->insert() logged a hard 'Duplicate entry' DB error to debug.log on every collision (~2,809 entries/day on the events site) even though the duplicate was caught and treated as success afterward. Replace the UPDATE-then-INSERT pair with a single atomic INSERT ... ON DUPLICATE KEY UPDATE keyed on flow_source_item. A duplicate is now a normal no-op update (no logged DB error), an existing in-flight claim is still converted to final processed state, and dedup behavior is unchanged: exactly one row per source item. Fixes #2626
Contributor
Homeboy Results —
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2626.
ProcessedItems->add_processed_item()raced between itsUPDATEand a bareINSERT, so concurrent Action Scheduler workers processing the same source item collided on theflow_source_itemunique key.wpdb->insert()logged a hardDuplicate entryDB error todebug.logon every collision (~2,809 entries/day on the events site, blog 7) even though the code caught the duplicate string and returnedtrueafterward.The unique key was correctly preventing dupes — the bug was that the bare INSERT logged a hard DB error on every race instead of treating "already processed" as a normal no-op.
Fix
Replaced the
UPDATE-then-bare-INSERTpair with a single atomicINSERT ... ON DUPLICATE KEY UPDATEkeyed onflow_source_item:processed.claimedrow → converted to finalprocessedstate (newjob_id/processed_timestamp,claim_expires_atcleared) — same end state as the old UPDATE path.Duplicate entryDB error.A check-then-insert would not be sufficient (the race is between check and insert); this is a single atomic statement. Dedup behavior is unchanged: exactly one row per
(flow_step_id, source_type, item_identifier).Verification
php -lclean on the changed file.composer lint(phpcs) on the changed file exits0.php tests/processed-item-claims-smoke.php→ 21 assertions, 0 failures.claimedrow → converted toprocessed, claim clearedSpans the affected handlers (
universal_web_scraper,dice_fm,ticketmaster,vision_flyer) since the fix is in the shared write path.Fixes #2626