[2.x] fix: notification + email duplicates from queue race in NotificationSyncer#4647
Merged
Conversation
…yncer NotificationSyncer::sync() decides who's a "new recipient" before the actual INSERT happens (in a queued SendNotificationsJob). When sync() fires twice in quick succession (Posted then Revised, etc.) before either job runs, both reads see no row and both jobs queue the same recipients — producing duplicate notifications and duplicate emails. Fix the two halves at the job level (RC-safe, no architectural shuffle): - SendNotificationsJob re-runs matchingBlueprint at INSERT time and only inserts recipients not already present. Idempotent regardless of how many times sync() queues the same job. - SendEmailNotificationJob takes a short-lived atomic cache lock keyed by (blueprint type, recipient id). The first job to run claims it and sends; later identical jobs no-op. 600s TTL outlives normal mail latency. Falls back to non-atomic send for cache stores that don't implement LockProvider — same behaviour as before this fix. Add three regression tests in MatchingBlueprintTest's neighbouring SyncerRaceTest: - SendNotificationsJob is idempotent across repeated runs - SendNotificationsJob still inserts new recipients alongside existing (i.e. a partial-match batch isn't skipped wholesale) - SendEmailNotificationJob is idempotent across repeated runs Fixes #4622. A cleaner architectural fix — moving the INSERT itself out of the queued job and into NotificationSyncer::sync() — would remove the bug class entirely, but is beyond the scope of an RC bug fix. Leaving that as a future cleanup.
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 #4622. Under a queued driver, mentioned users (and other notification recipients) receive multiple notifications and multiple emails for the same logical event when `Posted` is followed quickly enough by `Revised` (or any back-to-back blueprint dispatch).
Root cause
`NotificationSyncer::sync()` decides who's a "new recipient" before the actual INSERT happens — the INSERT lives in a queued `SendNotificationsJob`. With FIFO worker processing:
The race window is the entire interval between `sync()` and the queued `SendNotificationsJob` actually running — typically 30s–2min on a moderately loaded queue, well within normal user behaviour for an edit shortly after posting.
Fix
Two-part guard at the job level:
`SendNotificationsJob::handle()` — re-runs `matchingBlueprint` filtered by recipient id at INSERT time. Only inserts recipients without an existing row. Idempotent regardless of how many sync() calls queued the same job.
`SendEmailNotificationJob::handle()` — takes a short-lived atomic cache lock keyed by `(blueprint type, recipient id)`. First job to claim the lock sends the email; later identical jobs no-op. 600s TTL outlives normal mail latency. Falls back to non-atomic send if the cache store doesn't implement `LockProvider` (custom drivers) — same behaviour as before.
The `Notification` row alone isn't a sufficient signal for the email-side dedup, because `EmailJob_A` runs after `SendNotificationsJob_A` has inserted the row, and we want `EmailJob_A` to send the email — only `EmailJob_B` should be suppressed. The atomic lock distinguishes "first email job for this (blueprint, user)" from "later one."
Tests
Three integration tests in `framework/core/tests/integration/notification/SyncerRaceTest.php`:
All three failed against the un-fixed code on sqlite (the race manifests at the application level, not the DB level — sqlite reproduces it just like MySQL).
Why not a deeper architectural fix
Option 2 from the issue reporter — moving the INSERT itself into `NotificationSyncer::sync()` (synchronous) and keeping only email queued — would remove the bug class entirely. That's a better long-term shape but it's a bigger change with timing-semantics implications for any code observing the insert. Out of scope for an RC bug fix; worth revisiting post-2.0.
Relationship to #4643
#4643 (merged in #4645) was a separate bug: the dedup query itself didn't work on MySQL for non-null-data blueprints. That fix made dedup work for `postMentioned`, `newPost`, etc.; this fix closes the residual race for null-data blueprints (`userMentioned`, `postLiked`) plus any non-null-data blueprint that races faster than the queue can drain.