Skip to content

Commit 1f706e6

Browse files
authored
fix(orchestrator): race condition in dequeue (NangoHQ#2261)
another race condition in orchestrator dequeue where the new task event can be received at the same time of the timeout triggering. The dequeued tasks will then not be sent back to the processor. Here again the fix is simple: only try to dequeue after having cleanup the timeout and check that no response was already sent ## Checklist before requesting a review (skip if just adding/editing APIs & templates) - [ ] I added tests, otherwise the reason is: I managed to write a test to trigger the race condition but it was only occuring 50% of the time :( - [ ] I added observability, otherwise the reason is: - [ ] I added analytics, otherwise the reason is:
1 parent 85d03e1 commit 1f706e6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/orchestrator/lib/routes/v1/postDequeue.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ const handler = (scheduler: Scheduler, eventEmitter: EventEmitter) => {
5858
}
5959
};
6060
const onTaskStarted = async (_t: Task) => {
61-
const getTasks = await scheduler.dequeue({ groupKey, limit });
62-
if (getTasks.isErr()) {
63-
cleanupAndRespond((res) => res.status(500).json({ error: { code: 'dequeue_failed', message: getTasks.error.message } }));
64-
} else {
65-
cleanupAndRespond((res) => res.status(200).json(getTasks.value));
66-
}
61+
cleanupAndRespond(async (res) => {
62+
const getTasks = await scheduler.dequeue({ groupKey, limit });
63+
if (getTasks.isErr()) {
64+
res.status(500).json({ error: { code: 'dequeue_failed', message: getTasks.error.message } });
65+
} else {
66+
res.status(200).json(getTasks.value);
67+
}
68+
});
6769
};
6870
const timeout = setTimeout(() => {
6971
cleanupAndRespond((res) => res.status(200).send([]));

0 commit comments

Comments
 (0)