From ae3d27786898c81f7803ad29f33ae4d2dff33f89 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Fri, 13 Feb 2026 09:14:24 +0100 Subject: [PATCH] Fix race condition on notification creation --- app/models/notifier.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/notifier.rb b/app/models/notifier.rb index b50fc6d26..f00987ab8 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -16,12 +16,16 @@ class Notifier if should_notify? # Processing recipients in order avoids deadlocks if notifications overlap. recipients.sort_by(&:id).map do |recipient| - notification = Notification.find_or_initialize_by(user: recipient, card: source.card) - notification.source = source - notification.creator = creator - notification.read_at = nil - notification.unread_count = (notification.unread_count || 0) + 1 - notification.save! + notification = Notification.create_or_find_by(user: recipient, card: source.card) do |n| + n.source = source + n.creator = creator + n.unread_count = 1 + end + + unless notification.previously_new_record? + notification.update!(source: source, creator: creator, read_at: nil, unread_count: notification.unread_count + 1) + end + notification end end