fix startup session refresh stalls

This commit is contained in:
Benjamin Shafii
2026-04-24 11:46:35 -07:00
parent 20fcb84fd4
commit 403e7c3e76
2 changed files with 25 additions and 5 deletions

View File

@@ -86,8 +86,22 @@ export const desktopBridge: DesktopBridge = new Proxy({} as DesktopBridge, {
},
});
function isLoopbackUrl(input: RequestInfo | URL): boolean {
const raw = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
try {
const url = new URL(raw);
return url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname === "[::1]";
} catch {
return false;
}
}
export const desktopFetch: typeof globalThis.fetch = (input, init) => {
if (isElectronDesktopRuntime()) {
if (isLoopbackUrl(input)) {
return globalThis.fetch(input, init);
}
return invokeElectronHelper<{
status: number;
statusText: string;

View File

@@ -395,15 +395,17 @@ export function SessionRoute() {
[sessionsByWorkspaceId],
);
const backgroundSessionLoadInFlight = useRef<Set<string>>(new Set());
const backgroundSessionLoadInFlight = useRef<Map<string, number>>(new Map());
const loadWorkspaceSessionsInBackground = useCallback(
async (openworkClient: OpenworkServerClient, workspaces: RouteWorkspace[]) => {
const MAX_ATTEMPTS = 6;
const backoffMs = (attempt: number) => Math.min(500 * Math.pow(2, attempt), 4_000);
const fetchOnce = async (workspace: RouteWorkspace, attempt: number): Promise<void> => {
if (backgroundSessionLoadInFlight.current.has(workspace.id)) return;
backgroundSessionLoadInFlight.current.add(workspace.id);
const startedAt = backgroundSessionLoadInFlight.current.get(workspace.id) ?? 0;
if (startedAt && Date.now() - startedAt < 5_000) return;
const requestStartedAt = Date.now();
backgroundSessionLoadInFlight.current.set(workspace.id, requestStartedAt);
try {
const response = await openworkClient.listSessions(workspace.id, { limit: 200 });
const workspaceRoot = normalizeDirectoryPath(workspace.path ?? "");
@@ -426,7 +428,9 @@ export function SessionRoute() {
// in the meantime instead of flashing "error" next to the
// workspace name.
if (attempt + 1 < MAX_ATTEMPTS && isTransientStartupError(message)) {
backgroundSessionLoadInFlight.current.delete(workspace.id);
if (backgroundSessionLoadInFlight.current.get(workspace.id) === requestStartedAt) {
backgroundSessionLoadInFlight.current.delete(workspace.id);
}
await new Promise((r) => window.setTimeout(r, backoffMs(attempt)));
await fetchOnce(workspace, attempt + 1);
return;
@@ -439,7 +443,9 @@ export function SessionRoute() {
current.includes(workspace.id) ? current.filter((id) => id !== workspace.id) : current,
);
} finally {
backgroundSessionLoadInFlight.current.delete(workspace.id);
if (backgroundSessionLoadInFlight.current.get(workspace.id) === requestStartedAt) {
backgroundSessionLoadInFlight.current.delete(workspace.id);
}
}
};