{"record":{"id":"018e00b0d07b70bf","repo":"garrytan/gstack","slug":"server-failed-to-start-errorlog","errorCode":null,"errorMessage":"Server failed to start:\n${errorLog}","messagePattern":"Server failed to start:\n(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"browse/src/cli.ts","lineNumber":367,"sourceCode":"  // Use HTTP health check (not isProcessAlive) — it's fast (~instant ECONNREFUSED)\n  // and works reliably on all platforms including Windows.\n  const start = Date.now();\n  while (Date.now() - start < MAX_START_WAIT) {\n    const state = readState();\n    if (state && await isServerHealthy(state.port)) {\n      return state;\n    }\n    await Bun.sleep(100);\n  }\n\n  // Server didn't start in time — check the on-disk startup error log.\n  // Both platforms now spawn with stdio: 'ignore', so the server writes\n  // errors to disk for the CLI to read (see server.ts start().catch).\n  const errorLogPath = path.join(config.stateDir, 'browse-startup-error.log');\n  try {\n    const errorLog = fs.readFileSync(errorLogPath, 'utf-8').trim();\n    if (errorLog) {\n      throw new Error(`Server failed to start:\\n${errorLog}`);\n    }\n  } catch (e: any) {\n    if (e.code !== 'ENOENT') throw e;\n  }\n  throw new Error(`Server failed to start within ${MAX_START_WAIT / 1000}s`);\n}\n\n/**\n * Acquire an exclusive lockfile to prevent concurrent ensureServer() races (TOCTOU).\n * Returns a cleanup function that releases the lock.\n */\nfunction acquireServerLock(): (() => void) | null {\n  const lockPath = `${config.stateFile}.lock`;\n  try {\n    // 'wx' — create exclusively, fails if file already exists (atomic check-and-create)\n    // Using string flag instead of numeric constants for Bun Windows compatibility\n    const fd = fs.openSync(lockPath, 'wx');\n    fs.writeSync(fd, `${process.pid}\\n`);","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/garrytan/gstack/blob/94993f74012782fd94416dd44b8314f6363a13a4/browse/src/cli.ts#L349-L385","documentation":"Error \"Server failed to start:\n${errorLog}\" thrown in garrytan/gstack.","triggerScenarios":"Thrown at browse/src/cli.ts:367 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"94993f74012782fd94416dd44b8314f6363a13a4","analyzedAt":"2026-08-12T04:06:23.140Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}