Notification red dot count can be decreased multiple times for the same notification
Describe the bug
The read state of a notification is stored in the database, while the unread red dot count is kept in the cache (answer:red-dot:<type>:<userID>).
NotificationService.ClearIDUnRead marks a single notification as read in the database and then decreases the cached counter. The database update is guarded by an "is unread" check, but the cache decrement is outside that guard, so it runs on every call:
if notificationInfo.IsRead == schema.NotificationNotRead {
err := ns.notificationRepo.ClearIDUnRead(ctx, userID, id) // idempotent
...
}
...
_ = ns.notificationCommon.DecreaseRedDot(ctx, userID, notificationInfo.Type) // runs every time
Marking an already read notification as read again is a no-op in the database but decreases the cached counter again. When the counter reaches 0, DecreaseRedDot deletes the cache key (internal/service/notification_common/notification.go), so the red dot disappears while the database still contains unread notifications. Any client that calls the endpoint twice for the same notification (a double click, a retry, or clicking a notification that is already read) loses an unread count that never existed.
To Reproduce
Steps to reproduce the behavior:
- Prepare a user with 3 unread notifications (e.g. someone answers/comments on their question) and make sure the notifications page has not been opened since, because opening it deletes the red dot cache key.
GET /answer/api/v1/notification/statusreturnsinbox: 3.PUT /answer/api/v1/notification/read/statewith the id of one unread notification, sent twice (or click the same notification link twice in the UI).GET /answer/api/v1/notification/statusreturnsinbox: 1, whileSELECT count(*) FROM notification WHERE user_id = ? AND is_read = 1returns 2.- Repeat step 3 until the counter reaches 0: the bell icon shows no red dot while unread notifications still exist in the database.
Expected behavior
Only the request that actually changes a notification from unread to read should decrease the cached counter. Marking an already read notification as read again should be a no-op for both the database and the cache.
Screenshots
Not applicable, the inconsistency is visible through the API responses above.
Platform
- Device: N/A
- OS: Not OS-specific
- Browser and version: N/A
- Version: current
main(6744295f) anddev(e97d1957), both contain the same code
Additional context
Affected code: internal/service/notification/notification_service.go (ClearIDUnRead), internal/service/notification_common/notification.go (DecreaseRedDot), internal/repo/notification/notification_repo.go (ClearIDUnRead).
Possible fix: make the database update conditional and atomic (UPDATE notification SET is_read = 2 WHERE id = ? AND user_id = ? AND is_read = 1) and decrease the cache counter only when the update reports one affected row. xorm's Update already returns the affected row count, it is currently discarded in the repository. This also covers concurrent duplicate requests, which the current read-then-update sequence cannot.
Related observations, possibly out of scope for this issue: the counter is also not adjusted when all notifications are marked as read (ClearUnRead only updates the database), and opening the notifications page deletes the counter without marking anything as read in the database. Together they make the counter a rough "notifications received since the last visit" value rather than an exact unread count.
Hi, @LinkinStars could you please take a look at this issue when you have time? I'd also be interested in working on a fix if the issue is confirmed. Thanks!
Thanks for the detailed report. I confirmed that the core issue is valid.
ClearIDUnRead reads the notification and conditionally updates the database only when it is unread, but it calls DecreaseRedDot unconditionally afterwards. A duplicate sequential request therefore decrements the cached red dot twice: the first request changes the notification from unread to read, while the second skips the meaningful database transition but still decrements the cache.
There is also a concurrency race. Two requests can both read the notification as unread before either update completes. The repository update does not include an is_read = unread predicate and discards the affected-row count, so both requests may decrement the cache.
The recommended fix is to make the update atomic, for example:
UPDATE notification
SET is_read = 2
WHERE id = ? AND user_id = ? AND is_read = 1
Then call DecreaseRedDot only when the update affects exactly one row. The badge-alert cache cleanup can remain independent, since it is already idempotent. Please add coverage for sequential duplicate requests and, where practical, concurrent duplicate requests.
One scope note: the red-dot cache is not currently designed as an exact mirror of database unread rows. Opening the notifications page deletes the red-dot key without marking records as read, while “mark all read” updates the database without changing the key. This fix should therefore focus on making the single-notification read endpoint idempotent and preventing duplicate requests from clearing the indicator too early, rather than redefining the overall red-dot semantics.
Thanks again for the investigation — assigning this to you to work on the fix.