Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(restate): better error message on abort, preserve error stacktraces and fix stack undeterminsim

Mohamed Bassem1745f6f9b8cd
diff --git a/packages/plugins/queue-restate/src/dispatcher.ts b/packages/plugins/queue-restate/src/dispatcher.ts
index 56d6ebc9..ada99c41 100644
--- a/packages/plugins/queue-restate/src/dispatcher.ts
+++ b/packages/plugins/queue-restate/src/dispatcher.ts
@@ -156,7 +156,13 @@ export function buildDispatcherService<T, R>(
res.error instanceof Error ? res.error.name : "RPCError",
message: errorMessage,
stack:
- res.error instanceof Error ? res.error.stack : undefined,
+ res.error instanceof Error
+ ? // TerminalError stacks can be non determinstic
+ // https://github.com/restatedev/sdk-typescript/issues/656
+ res.error instanceof restate.TerminalError
+ ? undefined
+ : res.error.stack
+ : undefined,
},
}),
);
diff --git a/packages/plugins/queue-restate/src/runner.ts b/packages/plugins/queue-restate/src/runner.ts
index ae79ea8a..54a0509f 100644
--- a/packages/plugins/queue-restate/src/runner.ts
+++ b/packages/plugins/queue-restate/src/runner.ts
@@ -6,6 +6,18 @@ import { tryCatch } from "@karakeep/shared/tryCatch";
import type { RunnerJobData, RunnerResult, SerializedError } from "./types";
+function timeoutSignal(timeoutSecs: number): AbortSignal {
+ const controller = new AbortController();
+ const timer = setTimeout(() => {
+ controller.abort(new Error(`Job timed out after ${timeoutSecs} seconds`));
+ }, timeoutSecs * 1000);
+ // Don't keep the process alive just for this timer
+ // TODO: Fix the monorepo type mismatch - mobile sees setTimeout returning number
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (timer as any).unref?.();
+ return controller.signal;
+}
+
function serializeError(error: Error): SerializedError {
return {
name: error.name,
@@ -73,9 +85,7 @@ export function buildRunnerService<T, R>(
data: payload,
priority: jobData.priority,
runNumber: jobData.runNumber,
- abortSignal: AbortSignal.timeout(
- jobData.timeoutSecs * 1000,
- ),
+ abortSignal: timeoutSignal(jobData.timeoutSecs),
}),
);
if (result.error) {
@@ -85,7 +95,10 @@ export function buildRunnerService<T, R>(
delayMs: result.error.delayMs,
};
}
- throw result.error;
+ return {
+ type: "error" as const,
+ error: serializeError(result.error),
+ };
}
return { type: "success" as const, value: result.data };
},
@@ -120,7 +133,7 @@ export function buildRunnerService<T, R>(
data: data.job.data,
priority: data.job.priority,
runNumber: data.job.runNumber,
- abortSignal: AbortSignal.timeout(data.job.timeoutSecs * 1000),
+ abortSignal: timeoutSignal(data.job.timeoutSecs),
},
data.result,
);