thedotmack/claude-mem · info

Service initializing

Error message

Service initializing

What it means

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.

Solutions

  1. Retry with backoff: poll a health endpoint until the worker is ready, then open the SSE stream
  2. Rely on EventSource built-in reconnection — the 503 will resolve once init finishes
  3. Check worker logs for the logged 'SSE stream requested before DB initialization' warning and any underlying init error
  4. If it persists, fix the DB initialization failure (DB path, permissions, corruption) that keeps the service uninitialized

Example fix

// before
const es = new EventSource('http://localhost:37777/api/viewer/stream');
// after
async function connectWhenReady() {
  while (!(await fetch('http://localhost:37777/api/health')).ok) {
    await new Promise(r => setTimeout(r, 500));
  }
  return new EventSource('http://localhost:37777/api/viewer/stream');
}
Defensive patterns

Strategy: retry

Validate before calling

const health = await fetch('http://localhost:37777/api/health');
if (!health.ok) throw new Error('Worker not ready; defer opening the SSE stream');

Try / catch

const es = new EventSource('/api/viewer/stream');
es.onerror = () => {
  // 503 while initializing: EventSource auto-reconnects; add backoff if needed
  setTimeout(() => es.reconnect && es.close() || openStream(), backoffMs);
};

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/8538a8b9afd027fe. Report an issue: GitHub.

Appendix: source

Thrown at src/services/worker/http/routes/ViewerRoutes.ts:232

   * also refuses to be framed at all — frame-ancestors 'none' for modern
   * browsers, X-Frame-Options for the rest.
   */
  private handleRestartPage = this.wrapHandler((req: Request, res: Response): void => {
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    res.setHeader('Cache-Control', 'no-store');
    res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
    res.setHeader('X-Frame-Options', 'DENY');
    res.send(RESTART_PAGE_HTML);
  });

  private handleSSEStream = this.wrapHandler((req: Request, res: Response): void => {
    try {
      this.dbManager.getSessionStore();
    } catch (initError: unknown) {
      if (initError instanceof Error) {
        logger.warn('HTTP', 'SSE stream requested before DB initialization', {}, initError);
      }
      res.status(503).json({ error: 'Service initializing' });
      return;
    }

    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');

    this.sseBroadcaster.addClient(res);

    const projectCatalog = this.dbManager.getSessionStore().getProjectCatalog();
    this.sseBroadcaster.broadcast({
      type: 'initial_load',
      projects: projectCatalog.projects,
      sources: projectCatalog.sources,
      projectsBySource: projectCatalog.projectsBySource,
      timestamp: Date.now()
    });

View on GitHub (pinned to d8bc9755e7)