-
Notifications
You must be signed in to change notification settings - Fork 0
SMS workflow reminder retry count tracking #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShashankFC
wants to merge
1
commit into
sms-retry-base
Choose a base branch
from
sms-retry-enhanced
base: sms-retry-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,10 +28,19 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { | |||||||||
| //delete all scheduled sms reminders where scheduled date is past current date | ||||||||||
| await prisma.workflowReminder.deleteMany({ | ||||||||||
| where: { | ||||||||||
| method: WorkflowMethods.SMS, | ||||||||||
| scheduledDate: { | ||||||||||
| lte: dayjs().toISOString(), | ||||||||||
| }, | ||||||||||
| OR: [ | ||||||||||
| { | ||||||||||
| method: WorkflowMethods.SMS, | ||||||||||
| scheduledDate: { | ||||||||||
| lte: dayjs().toISOString(), | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| retryCount: { | ||||||||||
| gt: 1, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| ], | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
@@ -44,8 +53,11 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { | |||||||||
| lte: dayjs().add(7, "day").toISOString(), | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| select, | ||||||||||
| })) as PartialWorkflowReminder[]; | ||||||||||
| select: { | ||||||||||
| ...select, | ||||||||||
| retryCount: true, | ||||||||||
| }, | ||||||||||
| })) as (PartialWorkflowReminder & { retryCount: number })[]; | ||||||||||
|
|
||||||||||
| if (!unscheduledReminders.length) { | ||||||||||
| res.json({ ok: true }); | ||||||||||
|
|
@@ -163,9 +175,26 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { | |||||||||
| referenceId: scheduledSMS.sid, | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
| } else { | ||||||||||
| await prisma.workflowReminder.update({ | ||||||||||
| where: { | ||||||||||
| id: reminder.id, | ||||||||||
| }, | ||||||||||
| data: { | ||||||||||
| retryCount: reminder.retryCount + 1, | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } catch (error) { | ||||||||||
| await prisma.workflowReminder.update({ | ||||||||||
| where: { | ||||||||||
| id: reminder.id, | ||||||||||
| }, | ||||||||||
| data: { | ||||||||||
| retryCount: reminder.retryCount + 1, | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The retry counter increment in the error handler also uses Severity Level: Major
|
||||||||||
| retryCount: reminder.retryCount + 1, | |
| retryCount: { | |
| increment: 1, | |
| }, |
Steps of Reproduction ✅
1. The same cron handler in
`packages/features/ee/workflows/api/scheduleSMSReminders.ts:21-202` iterates over
unscheduled SMS reminders and calls `twilio.scheduleSMS` (lines 158-166); if this call
throws (e.g. network error), execution jumps to the `catch` block at lines 189-198.
2. In the `catch` block, the reminder's `retryCount` is updated via
`prisma.workflowReminder.update` with `data: { retryCount: reminder.retryCount + 1 }` at
`scheduleSMSReminders.ts:190-197`, using the `retryCount` value that was originally read
in the `findMany` at lines 47-60.
3. If two instances of the cron endpoint `/api/cron/workflows/scheduleSMSReminders` run
concurrently and both load the same reminder (each seeing the same `retryCount` value from
`findMany`) and both encounter a thrown error from `twilio.scheduleSMS`, they will each
execute the `catch` block and issue concurrent `update` statements writing `retryCount` to
the same computed value (e.g. both write `1` instead of incrementing from `0` to `2`).
4. Because cleanup at `scheduleSMSReminders.ts:28-44` relies on `retryCount > 1` to stop
retrying and delete reminders, these lost increments from the non-atomic `retryCount:
reminder.retryCount + 1` in the `catch` block can allow additional unintended retry
attempts or leave reminders in the system longer than the intended maximum number of
failures.Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** packages/features/ee/workflows/api/scheduleSMSReminders.ts
**Line:** 195:195
**Comment:**
*Race Condition: The retry counter increment in the error handler also uses `retryCount: reminder.retryCount + 1`, which can drop increments under concurrent runs of the cron job, so it should likewise use an atomic `increment` to ensure retries are counted correctly.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
2 changes: 2 additions & 0 deletions
2
packages/prisma/migrations/20240508134359_add_retry_count_to_workflow_reminder/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "WorkflowReminder" ADD COLUMN "retryCount" INTEGER NOT NULL DEFAULT 0; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Incrementing the retry counter using
retryCount: reminder.retryCount + 1is a non-atomic read-modify-write that can lose increments when multiple cron executions overlap, so this should use Prisma's atomicincrementoperation to be concurrency-safe. [race condition]Severity Level: Major⚠️
Steps of Reproduction ✅
Prompt for AI Agent 🤖