Skip to content
Merged
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
24 changes: 23 additions & 1 deletion src/agent/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HOST_WORKSPACE_NAME } from '../shared/client-types';
import { DEFAULT_AGENT_PORT } from '../shared/constants';
import { WorkspaceManager } from '../workspace/manager';
import { containerRunning, getContainerName } from '../docker';
import { startEagerImagePull } from '../docker/eager-pull';
import { startEagerImagePull, stopEagerImagePull } from '../docker/eager-pull';
import { TerminalWebSocketServer } from '../terminal/websocket';
import { ChatWebSocketServer } from '../chat/websocket';
import { OpencodeWebSocketServer } from '../chat/opencode-websocket';
Expand Down Expand Up @@ -297,17 +297,39 @@ export async function startAgent(options: StartAgentOptions = {}): Promise<void>
startEagerImagePull();
});

let isShuttingDown = false;

const shutdown = async () => {
if (isShuttingDown) {
console.log('[agent] Force exit');
process.exit(0);
}
isShuttingDown = true;

console.log('[agent] Shutting down...');

const forceExitTimeout = setTimeout(() => {
console.log('[agent] Force exit after timeout');
process.exit(0);
}, 3000);
forceExitTimeout.unref();

stopEagerImagePull();
fileWatcher.stop();

if (tailscaleServeActive) {
console.log('[agent] Stopping Tailscale Serve...');
await stopTailscaleServe();
Comment on lines 320 to 322

This comment was marked as outdated.

}

chatServer.close();
opencodeServer.close();
terminalServer.close();

server.closeAllConnections();

server.close(() => {
clearTimeout(forceExitTimeout);
console.log('[agent] Server closed');
process.exit(0);
});
Expand Down
24 changes: 21 additions & 3 deletions src/docker/eager-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MAX_RETRIES = 10;

let pullInProgress = false;
let pullComplete = false;
let abortController: AbortController | null = null;

async function isDockerAvailable(): Promise<boolean> {
try {
Expand Down Expand Up @@ -44,8 +45,15 @@ export async function startEagerImagePull(): Promise<void> {
}

pullInProgress = true;
abortController = new AbortController();
const signal = abortController.signal;

const attemptPull = async (attempt: number): Promise<void> => {
if (signal.aborted) {
pullInProgress = false;
return;
}

if (attempt > MAX_RETRIES) {
console.log('[agent] Max retries reached for image pull - giving up background pull');
pullInProgress = false;
Expand All @@ -58,7 +66,8 @@ export async function startEagerImagePull(): Promise<void> {
if (attempt === 1) {
console.log('[agent] Docker not available - will retry in background');
}
setTimeout(() => attemptPull(attempt + 1), RETRY_INTERVAL_MS);
const timer = setTimeout(() => attemptPull(attempt + 1), RETRY_INTERVAL_MS);
timer.unref();
return;
}

Expand All @@ -67,14 +76,23 @@ export async function startEagerImagePull(): Promise<void> {
if (success) {
pullComplete = true;
pullInProgress = false;
} else {
setTimeout(() => attemptPull(attempt + 1), RETRY_INTERVAL_MS);
} else if (!signal.aborted) {
const timer = setTimeout(() => attemptPull(attempt + 1), RETRY_INTERVAL_MS);
timer.unref();
}
};

attemptPull(1);
}

export function stopEagerImagePull(): void {
if (abortController) {
abortController.abort();
abortController = null;
}
pullInProgress = false;
}

export function isImagePullComplete(): boolean {
return pullComplete;
}