fix(messaging): converge mark-read receipt on unique-index race#1617
Merged
Conversation
markRead's upsertReadReceipt did findOne-then-insert (check-then-act), so a concurrent mark-read or the best-effort `delivered` write could win the UNIQUE(notification_id, user_id, channel) index between the read and the write, throwing `UNIQUE constraint failed` when clicking a notification. Catch the unique violation and fall back to flipping the now-present row to `read`. Add a cross-driver isUniqueViolation helper (SQLite / Postgres 23505 / MySQL ER_DUP_ENTRY) and a regression test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
问题
点开消息通知时服务端报
UNIQUE constraint failed: sys_notification_receipt.notification_id, sys_notification_receipt.user_id, sys_notification_receipt.channel,mark-read 失败。根因
MessagingService.upsertReadReceipt用先查后写(check-then-act):先findOne找(notification_id, user_id, channel)的 receipt,落空就insert一条state:'read'。这中间存在竞态窗口——只要有第二个写入者抢先建了那一行,第二个insert就撞上UNIQUE(notification_id, user_id, channel)索引:deliveredreceipt 写入是异步的,可能与点击并发或刚好在其后落库。修复
upsertReadReceipt:insert 撞唯一索引时不再抛出,而是重新 findOne 把已存在的那行翻成read(输了竞态就回退成 update)。isUniqueViolation()(SQLite / Postgres23505/ MySQLER_DUP_ENTRY),只对真正的唯一约束冲突回退,不吞其它错误。markRead survives a unique-index race on the receipt insert。验证
pnpm vitest run src/messaging-service.test.ts→ 28 passedpnpm build通过🤖 Generated with Claude Code