{"record":{"id":"8538a8b9afd027fe","repo":"thedotmack/claude-mem","slug":"service-initializing-8538a8","errorCode":null,"errorMessage":"Service initializing","messagePattern":"Service initializing","errorType":"http","errorClass":null,"httpStatus":503,"severity":"info","filePath":"src/services/worker/http/routes/ViewerRoutes.ts","lineNumber":232,"sourceCode":"   * also refuses to be framed at all — frame-ancestors 'none' for modern\n   * browsers, X-Frame-Options for the rest.\n   */\n  private handleRestartPage = this.wrapHandler((req: Request, res: Response): void => {\n    res.setHeader('Content-Type', 'text/html; charset=utf-8');\n    res.setHeader('Cache-Control', 'no-store');\n    res.setHeader('Content-Security-Policy', \"frame-ancestors 'none'\");\n    res.setHeader('X-Frame-Options', 'DENY');\n    res.send(RESTART_PAGE_HTML);\n  });\n\n  private handleSSEStream = this.wrapHandler((req: Request, res: Response): void => {\n    try {\n      this.dbManager.getSessionStore();\n    } catch (initError: unknown) {\n      if (initError instanceof Error) {\n        logger.warn('HTTP', 'SSE stream requested before DB initialization', {}, initError);\n      }\n      res.status(503).json({ error: 'Service initializing' });\n      return;\n    }\n\n    res.setHeader('Content-Type', 'text/event-stream');\n    res.setHeader('Cache-Control', 'no-cache');\n    res.setHeader('Connection', 'keep-alive');\n\n    this.sseBroadcaster.addClient(res);\n\n    const projectCatalog = this.dbManager.getSessionStore().getProjectCatalog();\n    this.sseBroadcaster.broadcast({\n      type: 'initial_load',\n      projects: projectCatalog.projects,\n      sources: projectCatalog.sources,\n      projectsBySource: projectCatalog.projectsBySource,\n      timestamp: Date.now()\n    });\n","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/services/worker/http/routes/ViewerRoutes.ts#L214-L250","documentation":"The SSE viewer stream endpoint touches this.dbManager.getSessionStore() inside a try block. If the database is not yet initialized, getSessionStore throws, the route logs a warning and returns 503 'Service initializing' instead of opening the event stream.","triggerScenarios":"Opening the SSE stream (EventSource/fetch) immediately after worker start, before DB initialization completes; or when DB initialization fails, making getSessionStore throw on every attempt.","commonSituations":"Browser auto-reconnects to the EventSource during a worker restart; dashboard loaded before the worker finished booting; persistent DB initialization failure (corrupt DB, locked file, bad path) that makes 'initializing' permanent.","solutions":["Retry with backoff: poll a health endpoint until the worker is ready, then open the SSE stream","Rely on EventSource built-in reconnection — the 503 will resolve once init finishes","Check worker logs for the logged 'SSE stream requested before DB initialization' warning and any underlying init error","If it persists, fix the DB initialization failure (DB path, permissions, corruption) that keeps the service uninitialized"],"exampleFix":"// before\nconst es = new EventSource('http://localhost:37777/api/viewer/stream');\n// after\nasync function connectWhenReady() {\n  while (!(await fetch('http://localhost:37777/api/health')).ok) {\n    await new Promise(r => setTimeout(r, 500));\n  }\n  return new EventSource('http://localhost:37777/api/viewer/stream');\n}","handlingStrategy":"retry","validationCode":"const health = await fetch('http://localhost:37777/api/health');\nif (!health.ok) throw new Error('Worker not ready; defer opening the SSE stream');","typeGuard":null,"tryCatchPattern":"const es = new EventSource('/api/viewer/stream');\nes.onerror = () => {\n  // 503 while initializing: EventSource auto-reconnects; add backoff if needed\n  setTimeout(() => es.reconnect && es.close() || openStream(), backoffMs);\n};","preventionTips":["Open SSE connections only after the worker reports ready","Rely on EventSource's automatic reconnection for startup races","Use exponential backoff for manual reconnect loops","If 503 persists, investigate the underlying DB initialization failure in worker logs"],"tags":["http","sse","initialization","database"],"backgroundTag":"invalid-state-transition","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}