From 862b40c49e3024ec0f70cc4dda15828afaf701d1 Mon Sep 17 00:00:00 2001 From: Steven Liss Date: Fri, 2 Jan 2026 15:38:32 -0500 Subject: [PATCH] fix: remove _id when creating timeout retry task When a task times out and has retryOnTimeoutCount > 0, the code was spreading task.toObject() which includes the _id field. This caused E11000 duplicate key errors when trying to create the retry task. Also: - Decrement retryOnTimeoutCount so retries are properly limited - Reset execution state fields (startedRunningAt, error, result, etc.) --- src/taskSchema.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/taskSchema.js b/src/taskSchema.js index 8588c25..26beb89 100644 --- a/src/taskSchema.js +++ b/src/taskSchema.js @@ -175,9 +175,19 @@ taskSchema.statics.expireTimedOutTasks = async function expireTimedOutTasks() { } if (task.retryOnTimeoutCount > 0) { + // Copy task data but remove _id so MongoDB generates a new one + const taskData = task.toObject({ virtuals: false }); + delete taskData._id; await Task.create({ - ...task.toObject({ virtuals: false }), + ...taskData, status: 'pending', + retryOnTimeoutCount: task.retryOnTimeoutCount - 1, + startedRunningAt: null, + finishedRunningAt: null, + workerName: null, + error: null, + result: null, + timeoutAt: null, schedulingTimeoutAt: now.valueOf() + 10 * 60 * 1000 }); } else {