Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions apps/desktop/src/lib/trpc/workers/WorkerTaskRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,34 @@ describe("WorkerTaskRunner failure handling", () => {
expect(workers[0]?.terminated).toBe(true);
await runner.dispose();
});

test("a pre-aborted queued task does not strand the task behind it", async () => {
const runner = new WorkerTaskRunner({
workerScriptPath: "/mock/worker.js",
concurrency: 1,
createWorker,
});

const first = runner.runTask("status", { id: "first" });
const aborted = runner.runTask(
"status",
{ id: "aborted" },
{ signal: AbortSignal.abort() },
);
let third: unknown = "pending";
void runner.runTask("status", { id: "third" }).then(
(result) => {
third = result;
},
(error) => {
third = error;
},
);

await first;
await expect(aborted).rejects.toThrow();
await flushMicrotasks(10);
expect(third).toEqual({ workerId: 1 });
await runner.dispose();
});
});
46 changes: 24 additions & 22 deletions apps/desktop/src/lib/trpc/workers/WorkerTaskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,31 @@ export class WorkerTaskRunner {
}
if (this.queue.length === 0) break;

const nextTaskId = this.queue.shift();
if (!nextTaskId) continue;
const task = this.tasks.get(nextTaskId);
if (!task) continue;

if (task.abortSignal?.aborted) {
this.rejectTask(task.taskId, new WorkerTaskAbortedError("cancelled"));
continue;
while (this.queue.length > 0 && !slot.activeTaskId) {
const nextTaskId = this.queue.shift();
if (!nextTaskId) continue;
const task = this.tasks.get(nextTaskId);
if (!task) continue;

if (task.abortSignal?.aborted) {
this.rejectTask(task.taskId, new WorkerTaskAbortedError("cancelled"));
continue;
}

slot.activeTaskId = task.taskId;
task.slotId = slot.id;
task.timeoutHandle = setTimeout(() => {
this.handleTaskTimeout(task.taskId);
}, task.timeoutMs);

const request: WorkerTaskRequestMessage = {
kind: "task",
taskId: task.taskId,
taskType: task.taskType,
payload: task.payload,
};
slot.worker.postMessage(request);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: This new loop can reach an uncloneable payload after skipping a pre-aborted task, but postMessage throws synchronously and can crash the desktop process. Catch the dispatch error, clear activeTaskId, and reject the task so draining can continue.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At apps/desktop/src/lib/trpc/workers/WorkerTaskRunner.ts, line 282:

<comment>This new loop can reach an uncloneable payload after skipping a pre-aborted task, but `postMessage` throws synchronously and can crash the desktop process. Catch the dispatch error, clear `activeTaskId`, and reject the task so draining can continue.</comment>

<file context>
@@ -256,29 +256,31 @@ export class WorkerTaskRunner {
+					taskType: task.taskType,
+					payload: task.payload,
+				};
+				slot.worker.postMessage(request);
 			}
-
</file context>
Suggested change
slot.worker.postMessage(request);
try {
slot.worker.postMessage(request);
} catch (error) {
slot.activeTaskId = null;
this.rejectTask(
task.taskId,
new WorkerTaskError(
`postMessage failed for task "${task.taskType}": ${(error as Error).message}`,
),
);
}

}

slot.activeTaskId = task.taskId;
task.slotId = slot.id;
task.timeoutHandle = setTimeout(() => {
this.handleTaskTimeout(task.taskId);
}, task.timeoutMs);

const request: WorkerTaskRequestMessage = {
kind: "task",
taskId: task.taskId,
taskType: task.taskType,
payload: task.payload,
};
slot.worker.postMessage(request);
}
}

Expand Down
Loading