Repository navigation
Conversation
Stall records named "ws:daemon heartbeat" as the longest synchronous work in most windows because DB transactions, plugin handlers and plugin background work were not measured. Record every bb.db transaction (labelled by its first write), single statements of 1 ms or more, and the synchronous part of each request, plugin invocation, plugin load and plugin service before its first await. Name the dominant child chain and the calling work, and add main-thread CPU and context switches to the longest unit and to the slow timeline build log. Profile stall attribution now totals sampled time per plugin id. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
marked this pull request as ready for review
November 9, 2026 17:36
ymichael
deleted the
bb/server-o4-stall-attribution-names-the-wrong-work-thr_h65vwu3zj7
branch
November 9, 2026 17:41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Event loop stalledrecords could only name work that had been wrapped withrunEventLoopWorkSync: daemon WebSocket messages, timeline builds and a few sweep steps. SQLite transactions, plugin RPC/HTTP handlers, plugin event handlers, plugin load and plugin background services were async frames, and async frames are never candidates. So the longest measured synchronous work in most five-second windows was a 0.4 ms daemon heartbeat. In production logsserver.210–214.log(Nov 6 08:00 → Nov 7 10:46), 912 of 959 stalls (95%, 184.5 s of summed max delay) wereunattributed, and 614 of them hadlongestSynchronousWork = "ws:daemon heartbeat". The 29 heartbeat stalls that were candidates had a median of 114 ms wall but 16.7 ms CPU. The heartbeat itself was waiting inside itshost_daemon_sessionswrite transaction, and nothing recorded that. The CPU-profile records found the real work (61 s of the 97 s of core samples were in the transaction wrapper), but they named plugin code only by a build-directory UUID.What changed
apps/server/src/services/system/event-loop-work.tsAsyncLocalStorageas objects, so callbacks and timers a plugin scheduled while it loaded keep their attribution. A completed frame drops its parent pointer, so caller chains cannot grow.runEventLoopWorkcall now also measures its synchronous part before the firstawait, as<label> (until first await).ws:daemon heartbeat > db:transaction deferred: update "host_daemon_sessions" ….longestSynchronousWorkCaller(the enclosing request, plugin invocation, sweep, plugin load or service), pluslongestSynchronousWorkVoluntaryContextSwitchesandlongestSynchronousWorkInvoluntaryContextSwitches.packages/db/src/connection.ts: a newsynchronousWorkTrackerconnection option.db:transaction <mode>: <first write SQL>, or the first statement's SQL when the transaction only reads (redacted, 120 chars).db:<op> <sql>.initDb.plugin-runtime.ts: plugin load (plugin:<id> load) and background services (plugin:<id> service <name>) now run inside work frames. RPC, HTTP, event, schedule and hook invocations were already framed throughinvokeWrapped.packages/process-utilsstall attribution: the capture command now carries plugin code locations, taken from the runtime's existingpluginCodeLocationsmap.Event loop stall attributionrecords gainowners(sampled ms perplugin:<id>) and a per-stackowner. This work runs in the profile worker, so it costs the main thread nothing.Thread timeline build blocked the event loopnow logscpuDurationMs,voluntaryContextSwitchesandinvoluntaryContextSwitches.docs/debugging-and-qa.md(stall diagnosis) anddocs/configuration.md.How you verified
Regression tests, all written against this change:
apps/server/test/system/event-loop-work.test.tsuses a real migrated DB and blocks inside SQLite withAtomics.wait. It covers:awaitnames the plugin RPC and request as callers;plugin:<id> load;packages/db/test/connection.test.ts: the tracker receives transaction labels by first write, read-only labels, slow statement records, and no literal values.packages/process-utils/test/stall-attribution.test.tsandperformance-diagnostics.test.ts: native frames are charged to the calling plugin, and a real profile recordsowners.apps/server/test/services/threads/timeline-build-log.test.ts: the log carries CPU and context-switch fields.plugin-reload-collection.test.tscaught a retention bug in the first draft: a lazy label closure shared its V8 context with the plugin factory. It passes now.Commands:
pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/db --filter=@bb/process-utilspnpm exec turbo run test --filter=@bb/process-utils --filter=@bb/db: 120 and 663 tests pass.apps/servervitest overtest/services/plugins,test/system,test/services/threads,internal-event-append-ownershipandskeleton: 1,185 tests.builtin-pluginshot-reload timed out once at load ~40; it passed on rerun.plugin-authoring-docsneededbuild:typesfirst.turbo run linton all three packages andoxfmt --checkon the changed files.Live check: a source dev server (
pnpm start --data-dir <empty dir> --perf-diagnostics, not signed in), with the same scenario run onorigin/maincode and on this branch. The load was a local probe plugin: a route that spins 300 ms synchronously, a route that spins 300 ms after anawait, and a service that spins 250 ms on start. The forced slow transaction came from a second process holdingBEGIN IMMEDIATEon bb.db for 2.5 s.origin/mainunattributed,ws:daemon heartbeat(4.4 ms)candidate:plugin:o4-probe service spinner (until first await), callerPOST /api/v1/plugins/reload, 250 ms wall / 205.9 ms CPUunattributed,ws:daemon heartbeat(0.7 ms)candidate:plugin:o4-probe http POST /slow-sync (until first await), callerPOST /api/v1/plugins/o4-probe/http/slow-sync, 300.1 ms wall / 261 ms CPUawait(300 ms)unattributed,ws:daemon heartbeat. Profile record: no plugin namedunattributed(not measurable on the main thread). Profile record:owners: [plugin:o4-probe 257.4 ms]candidate:ws:daemon heartbeat, 893.8 ms wall / 4.4 ms CPUcandidate:ws:daemon heartbeat > db:transaction deferred: update "host_daemon_sessions" … > db:get update "host_daemon_sessions" …, 717.7 ms wall / 2.6 ms CPU, 0 voluntary / 205 involuntary switchesOverhead, measured in-process with 300k iterations, 5 rounds, at load average 20–40, so the numbers are noisy:
resourceUsagepair);A drizzle immediate transaction (select + update) costs about 100–120 µs on the same machine, so the ~2.5 µs per transaction is about 2%.
Not covered: code a plugin runs after its first
awaitis still not attributed in the immediateEvent loop stalledrecord. That would need promise hooks or a worker per plugin. With diagnostics on, the profile record now names that work by plugin id.🤖 Generated with Claude Code