fix(worker): serialize box failure and idle reaping

This commit is contained in:
Gabriel Brown
2026-07-10 17:00:22 -04:00
parent 490c05329b
commit 284f7ea52b
2 changed files with 86 additions and 7 deletions
+12 -2
View File
@@ -38,8 +38,14 @@ const scheduleReapIfIdle = (username: string) => {
if (!box || box.refs.size > 0) return;
if (box.idleTimer) clearTimeout(box.idleTimer);
box.idleTimer = setTimeout(() => {
void stopWorkspaceContainer(userContainerName(username));
boxes.delete(username);
void withLock(username, async () => {
const current = boxes.get(username);
if (current !== box || current.refs.size > 0) return;
await stopWorkspaceContainer(current.name);
if (boxes.get(username) === current && current.refs.size === 0) {
boxes.delete(username);
}
});
}, env.boxIdleMs);
};
@@ -83,6 +89,10 @@ export const acquireUserBox = (args: {
return handle;
} catch (error) {
handle.release();
if (boxes.get(args.username) === box && box.refs.size === 0) {
if (box.idleTimer) clearTimeout(box.idleTimer);
boxes.delete(args.username);
}
throw error;
}
});