Skip to content

avoid scheduling duplicate repeating tasks when task times out#10

Merged
vkarpov15 merged 1 commit intomainfrom
vkarpov15/repeating-task-timeout-fix
May 7, 2026
Merged

avoid scheduling duplicate repeating tasks when task times out#10
vkarpov15 merged 1 commit intomainfrom
vkarpov15/repeating-task-timeout-fix

Conversation

@vkarpov15
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings May 7, 2026 14:27
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96b20f5c29

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/taskSchema.js
Comment on lines +234 to +238
{
name: task.name,
scheduledAt,
repeatAfterMS: task.repeatAfterMS,
params: task.params,
previousTaskId: task._id,
originalTaskId: task.originalTaskId || task._id,
timeoutMS: task.timeoutMS,
schedulingTimeoutAt: scheduledAt.valueOf() + 10 * 60 * 1000
});
}
previousTaskId: task._id
},
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add a unique key for repeat-task upserts

When two workers enter _handleRepeatingTask() concurrently for the same timed-out or scheduling-timed-out task, both updateOne(..., { upsert: true }) calls can evaluate this filter before either insert is visible and both insert a repeat task; the schema only declares a non-unique { status, scheduledAt } index, so MongoDB has no unique constraint to collapse that race. The new sequential tests pass, but the production race this change targets still remains unless these fields are backed by a unique index or another atomic guard.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent duplicate “next run” documents for repeating tasks when a task times out, particularly under race conditions between task polling/execution and the timeout-expiry logic.

Changes:

  • Treat handler-level execution timeouts as a distinct timed_out status (via a TimeoutError) instead of marking them as failed.
  • Add a 10-minute buffer to expireTimedOutTasks() so it only expires tasks that are still in_progress well after their timeoutAt.
  • Change repeating-task scheduling to use an upsert-based write and add regression tests for duplicate scheduling scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/task.test.js Updates timeout expectations and adds tests to ensure repeats aren’t duplicated under race/double-handle scenarios.
src/taskSchema.js Adds buffered timeout expiry, introduces TimeoutError for execution timeouts, and uses upsert logic when scheduling repeating tasks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/taskSchema.js
Comment thread src/taskSchema.js
Comment on lines +233 to +252
return Task.updateOne(
{
name: task.name,
scheduledAt,
repeatAfterMS: task.repeatAfterMS,
params: task.params,
previousTaskId: task._id,
originalTaskId: task.originalTaskId || task._id,
timeoutMS: task.timeoutMS,
schedulingTimeoutAt: scheduledAt.valueOf() + 10 * 60 * 1000
});
}
previousTaskId: task._id
},
{
$setOnInsert: {
name: task.name,
scheduledAt,
repeatAfterMS: task.repeatAfterMS,
params: task.params,
previousTaskId: task._id,
originalTaskId: task.originalTaskId || task._id,
timeoutMS: task.timeoutMS,
schedulingTimeoutAt: scheduledAt.valueOf() + 10 * 60 * 1000
}
},
{ upsert: true }
);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reasonable concern, but highly unlikely given the buffer we give for hanging tasks. Will consider improving this for the future.

@vkarpov15 vkarpov15 merged commit f6fddf2 into main May 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants