Skip to content

db: getNonDestroyedHost, getThreadStatus, and listActiveHostThreads block the main event loop 7×–116× longer by recompiling SQL on every call #8365

Description

@Tiberriver256

Summary

getNonDestroyedHost (packages/db/src/data/hosts.ts), getThreadStatus, and listActiveHostThreads (packages/db/src/data/threads.ts) run synchronously on Node's main event loop during host/entity lookups (apps/server/src/routes/hosts.ts#L90, apps/server/src/services/lib/entity-lookup.ts#L172, apps/server/src/ws/host-shared-ports.ts#L238,L279), thread lifecycle checks (apps/server/src/services/threads/thread-startup-store.ts#L144), and daemon reconnects (apps/server/src/internal/session-owner-side-effects.ts#L352), yet rebuild their Drizzle SQL ASTs and call sqlite3_prepare_v2 (better-sqlite3 prepare()) on every single call.

On clean origin/main (b0ddaad731), 1,000 calls on an empty table show extreme synchronous main-thread overhead compared to their already-compiled siblings getHost and getThread in the exact same files:

  • getHost: 2.2 ms (1 prepare) vs getNonDestroyedHost: 256.6 ms (1,000 prepares — 116× slower)
  • getThread: 7.4 ms (1 prepare) vs listActiveHostThreads: 367.9 ms (50× slower) and getThreadStatus: 50.1 ms (7× slower)

Versions & Environment

  • Commit: b0ddaad731565d7e4de68d824120d802fc78f7d5 (origin/main)
  • Node: v26.13.1
  • OS: Linux x64

Root Cause

Reproduction

Run from repo root at b0ddaad731565d7e4de68d824120d802fc78f7d5:

node --conditions=source --import tsx -e '
import { performance } from "node:perf_hooks";
import { createMigratedConnection } from "./packages/db/src/testing.ts";
import { ensurePersonalProject } from "./packages/db/src/data/projects.ts";
import { createThread, getThread, getThreadStatus, listActiveHostThreads } from "./packages/db/src/data/threads.ts";
import { getHost, getNonDestroyedHost } from "./packages/db/src/data/hosts.ts";
import { noopNotifier } from "./packages/db/src/notifier.ts";

function measure(n: number, fn: (db: ReturnType<typeof createMigratedConnection>, threadId: string) => void) {
  const db = createMigratedConnection();
  const project = ensurePersonalProject(db);
  const thread = createThread(db, noopNotifier, { projectId: project.id, providerId: "codex", status: "idle" });
  let prepares = 0;
  const orig = db.$client.prepare.bind(db.$client);
  db.$client.prepare = ((sql: string) => { prepares++; return orig(sql); }) as typeof db.$client.prepare;
  const t0 = performance.now();
  for (let i = 0; i < n; i++) fn(db, thread.id);
  const ms = +(performance.now() - t0).toFixed(1);
  db.$client.close();
  return { prepares, ms };
}

console.log(JSON.stringify({
  getHost_cached: measure(1000, (db) => getHost(db, "h1")),
  getNonDestroyedHost: measure(1000, (db) => getNonDestroyedHost(db, "h1")),
  getThread_cached: measure(1000, (db, id) => getThread(db, id)),
  getThreadStatus: measure(1000, (db, id) => getThreadStatus(db, id)),
  listActiveHostThreads: measure(1000, (db) => listActiveHostThreads(db, { hostId: "h1" })),
}));
'

Actual output

{"getHost_cached":{"prepares":1,"ms":2.2},"getNonDestroyedHost":{"prepares":1000,"ms":256.6},"getThread_cached":{"prepares":1,"ms":7.4},"getThreadStatus":{"prepares":1000,"ms":50.1},"listActiveHostThreads":{"prepares":1000,"ms":367.9}}

Expected behavior

  • getNonDestroyedHost, getThreadStatus, and listActiveHostThreads should compile and prepare once per connection (prepares: 1 for 1,000 calls) via prepareCachedQuery + placeholder(...) (already imported in both hosts.ts and threads.ts), bringing latency in line with getHost and getThread.
  • Scope is limited to packages/db/src/data/hosts.ts and packages/db/src/data/threads.ts (pnpm exec turbo run test --filter=@bb/db).

What was ruled out

  • Confirmed all 3 queries have fixed SQL shapes with a single scalar parameter (id / hostId).

AGENT GENERATED

Activity

  1. added theissue type on Nov 29, 2026
  2. added
    threads
    host
    confirmed-repro
    on Nov 29, 2026
  3. bb-slop-cop commented on Nov 29, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority: Medium · Effort: Low · Areas: threads, host · perf.
    • Report: https://get-bb.github.io/reports/issues/8365.html — REPRODUCED, high root-cause confidence. Reproduction label: confirmed-repro.
    • Root cause: Three synchronous lifecycle reads rebuild Drizzle SQL and prepare SQLite statements on every call rather than reusing connection-scoped queries. Two clean trusted checkouts each show ten unnecessary compilations and preparations for ten warm calls; exact timing ratios were not asserted.
    • Fix: Ready PR Cache fixed-shape lifecycle database queries #8366 — 157 changed text lines across three database files. The focused regression fails before and passes after; all 766 database tests and the typecheck pass. Not merged.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reprohostperfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions