fix: add retry logic to daemon machine registration for transient connection errors

Add exponential backoff retry mechanism to handle ECONNREFUSED errors when the
server isn't ready yet during daemon startup. This includes:

- New errorUtils module with error classification helpers
- withRetry function in time.ts supporting configurable exponential backoff
- Machine registration retry with sensible defaults (60 attempts, 1-30s delays)
- Error refactoring to consolidate extractErrorInfo utility

close #35
This commit is contained in:
weishu
2026-01-08 11:37:29 +08:00
parent c0446766a5
commit 1665c63d95
4 changed files with 170 additions and 42 deletions
+20 -6
View File
@@ -12,6 +12,8 @@ import { getEnvironmentInfo } from '@/ui/doctor';
import { spawnHappyCLI } from '@/utils/spawnHappyCLI';
import { writeDaemonState, DaemonLocallyPersistedState, readDaemonState, acquireDaemonLock, releaseDaemonLock } from '@/persistence';
import { isProcessAlive, isWindows, killProcess, killProcessByChildProcess } from '@/utils/process';
import { withRetry } from '@/utils/time';
import { isRetryableConnectionError } from '@/utils/errorUtils';
import { cleanupDaemonState, getInstalledCliMtimeMs, isDaemonRunningCurrentlyInstalledHappyVersion, stopDaemon } from './controlClient';
import { startDaemonControlServer } from './controlServer';
@@ -522,12 +524,24 @@ export async function startDaemon(): Promise<void> {
// Create API client
const api = await ApiClient.create();
// Get or create machine
const machine = await api.getOrCreateMachine({
machineId,
metadata: buildMachineMetadata(),
daemonState: initialDaemonState
});
// Get or create machine (with retry for transient connection errors)
const machine = await withRetry(
() => api.getOrCreateMachine({
machineId,
metadata: buildMachineMetadata(),
daemonState: initialDaemonState
}),
{
maxAttempts: 60,
minDelay: 1000,
maxDelay: 30000,
shouldRetry: isRetryableConnectionError,
onRetry: (error, attempt, nextDelayMs) => {
const errorMsg = error instanceof Error ? error.message : String(error)
logger.debug(`[DAEMON RUN] Failed to register machine (attempt ${attempt}), retrying in ${nextDelayMs}ms: ${errorMsg}`)
}
}
);
logger.debug(`[DAEMON RUN] Machine registered: ${machine.id}`);
// Create realtime machine session