Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: use conditional cancel and fail-fast on interrupt errors

Arvin Xu345f144b1e77
diff --git a/packages/database/src/models/taskTopic.ts b/packages/database/src/models/taskTopic.ts
index 5a2e92f0815..64b1b600780 100644
--- a/packages/database/src/models/taskTopic.ts
+++ b/packages/database/src/models/taskTopic.ts
@@ -44,6 +44,26 @@ export class TaskTopicModel {
);
}
+ /**
+ * Atomically cancel a topic only if it is still in `running` status.
+ * Returns true if a row was actually updated.
+ */
+ async cancelIfRunning(taskId: string, topicId: string): Promise<boolean> {
+ const result = await this.db
+ .update(taskTopics)
+ .set({ status: 'canceled' })
+ .where(
+ and(
+ eq(taskTopics.taskId, taskId),
+ eq(taskTopics.topicId, topicId),
+ eq(taskTopics.status, 'running'),
+ eq(taskTopics.userId, this.userId),
+ ),
+ )
+ .returning();
+ return result.length > 0;
+ }
+
async updateOperationId(taskId: string, topicId: string, operationId?: string): Promise<void> {
await this.db
.update(taskTopics)
diff --git a/src/server/routers/lambda/__tests__/integration/task.integration.test.ts b/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
index c8d1cae7b5a..b14ff267c07 100644
--- a/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
+++ b/src/server/routers/lambda/__tests__/integration/task.integration.test.ts
@@ -397,6 +397,26 @@ describe('Task Router Integration', () => {
await caller.updateStatus({ id: task.data.id, status: 'paused' });
expect(mockInterruptTask).not.toHaveBeenCalled();
});
+
+ it('should skip cancellation when interrupt fails', async () => {
+ const task = await caller.create({
+ assigneeAgentId: testAgentId,
+ instruction: 'Test interrupt failure',
+ });
+
+ await caller.run({ id: task.data.id });
+
+ // Make interruptTask fail
+ mockInterruptTask.mockRejectedValueOnce(new Error('network error'));
+
+ // Transition task from running → paused
+ await caller.updateStatus({ id: task.data.id, status: 'paused' });
+
+ // The topic should still be running because interrupt failed
+ // so re-running should hit CONFLICT
+ await caller.updateStatus({ id: task.data.id, status: 'backlog' });
+ await expect(caller.run({ id: task.data.id })).rejects.toThrow(/already has a running topic/);
+ });
});
describe('heartbeat timeout detection', () => {
diff --git a/src/server/routers/lambda/task.ts b/src/server/routers/lambda/task.ts
index fcbcf63ad82..bd521bf36ca 100644
--- a/src/server/routers/lambda/task.ts
+++ b/src/server/routers/lambda/task.ts
@@ -1240,12 +1240,21 @@ export const taskRouter = router({
for (const t of topics) {
if (t.status !== 'running') continue;
+
+ // Interrupt the remote operation first; if it fails, skip cancellation
+ // to avoid desynchronizing DB state from a still-running operation.
if (t.operationId) {
- await aiAgentService.interruptTask({ operationId: t.operationId }).catch((err) => {
+ try {
+ await aiAgentService.interruptTask({ operationId: t.operationId });
+ } catch (err) {
console.error('[task:updateStatus] failed to interrupt topic %s:', t.topicId, err);
- });
+ continue;
+ }
}
- await ctx.taskTopicModel.updateStatus(resolved.id, t.topicId, 'canceled');
+
+ // Conditionally cancel only if the topic is still running,
+ // avoiding overwrite of a concurrent completed/timeout transition.
+ await ctx.taskTopicModel.cancelIfRunning(resolved.id, t.topicId);
}
}