Repository navigation
fix: deadlock in stack_email_notification - #9941
Conversation
- Each per-issue email previously received all of the receiver's log ids, so concurrent send tasks updated the same email_notification_logs rows and deadlocked. The first email also marked every issue's logs as sent. - Track log ids per issue so each email only touches its own rows. - Set processed_at before dispatching send tasks so that update can't race their sent_at updates on the same rows. - Add a unit test checking each email gets only its issue's log ids.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review. 📝 WalkthroughWalkthroughThe notification task now associates each queued issue email with only that issue’s notification-log IDs. It marks the collected logs processed before dispatching email tasks. A unit test verifies both behaviors across two issues. ChangesIssue email notifications
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The change is mergeable after normal checks. Email publication failures can still suppress notifications, so the owner should be aware of that limitation. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to Issue-scoped log IDs improve isolation between emails. However, marking the entire pending batch processed before queueing creates a recovery gap: an interruption can prevent email delivery across multiple receivers. Separately stored notifications remain intact, and no expanded external access was identified. Retained concerns
Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Failed or interrupted dispatch can leave unsent notifications permanently excluded from retries.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Reduces contention between email notification tasks by isolating each issue’s logs and updating processing state before dispatch.
Changes:
- Pass only issue-specific log IDs to each send task.
- Mark logs processed before enqueueing emails.
- Add a regression test for log grouping and processing.
| File | Description |
|---|---|
| apps/api/plane/tests/unit/bg_tasks/test_stack_email_notification.py | Tests per-issue log IDs and processed state. |
| apps/api/plane/bgtasks/email_notification_task.py | Scopes log IDs and reorders processing before dispatch. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| # Update the email notification log | ||
| # Mark the logs processed before dispatching, so this update never races the | ||
| # send tasks' `sent_at` updates on the same rows | ||
| EmailNotificationLog.objects.filter(pk__in=processed_notifications).update(processed_at=timezone.now()) |
| with mock.patch("plane.bgtasks.email_notification_task.send_email_notification.delay") as delay: | ||
| stack_email_notification() | ||
|
|
||
| sent = {call.kwargs["issue_id"]: sorted(call.kwargs["email_notification_ids"]) for call in delay.call_args_list} | ||
| assert sent == {issue: sorted(ids) for issue, ids in logs.items()} | ||
| assert not EmailNotificationLog.objects.filter(processed_at__isnull=True).exists() |
|
Confirming this on a self-hosted instance running current |
sriramveeraghanta
left a comment
There was a problem hiding this comment.
LGTM. Each send task now touches only its own issue's log rows, and processed_at commits before dispatch. Trade-off worth knowing: if the broker fails partway through dispatch, the remaining logs stay marked processed and are never sent.
- Each per-issue email previously received all of the receiver's log ids, so concurrent send tasks updated the same email_notification_logs rows and deadlocked. The first email also marked every issue's logs as sent. - Track log ids per issue so each email only touches its own rows. - Set processed_at before dispatching send tasks so that update can't race their sent_at updates on the same rows. - Add a unit test checking each email gets only its issue's log ids.
- Each per-issue email previously received all of the receiver's log ids, so concurrent send tasks updated the same email_notification_logs rows and deadlocked. The first email also marked every issue's logs as sent. - Track log ids per issue so each email only touches its own rows. - Set processed_at before dispatching send tasks so that update can't race their sent_at updates on the same rows. - Add a unit test checking each email gets only its issue's log ids. (cherry picked from commit 431475d)


Description
stack_email_notificationpassed every one of a receiver's log ids to each per-issuesend_email_notificationtask. When a receiver had notifications on multiple issues, the concurrent send tasks updated the sameemail_notification_logsrows (sent_at) and deadlocked. The first email to finish also marked every issue's logs as sent.processed_atbefore dispatching the send tasks, so that update can't race theirsent_atupdates on the same rows.Type of Change
Screenshots and Media (if applicable)
Test Scenarios
pytest apps/api/plane/tests/unit/bg_tasks/test_stack_email_notification.py.stack_email_notification, and verify two send tasks are dispatched, each with only its issue's log ids.processed_atset and nosent_atis set by the stacking task.References
No work item.
🤖 Generated with Claude Code
Summary by CodeRabbit