Repository navigation
Engine & TUI fixes: clientdb writes, dagql fan-out, high-verbosity traces - #13632
Conversation
a5c391f to
320d61a
Compare
320d61a to
a0981f4
Compare
database/sql had no MaxOpenConns limit, so each concurrent query got its own SQLite connection. SQLite only allows one writer at a time; under write contention every extra connection spins in the busy handler, and in modernc.org/sqlite that wait is a raw nanosleep syscall pinning an OS thread per connection. During a burst of concurrent telemetry inserts this exhausted the Go runtime's 10000-thread limit and crashed the engine. With a single connection, concurrent queries queue in database/sql's pool (plain goroutine parking) rather than busy-waiting inside SQLite. This trades away WAL-mode read/write concurrency, which is acceptable now that log writes are batched and reads are periodic bounded drains. Signed-off-by: Alex Suraci <[email protected]>
clientLogs was registered directly as a log Processor, so every log record emitted anywhere in the engine did a synchronous InsertLog into the per-client SQLite DB on the emitting goroutine (plus one more per parent client). A single query fanning out into thousands of parallel resolutions (e.g. a large Workspace.search result with all fields selected) piled thousands of concurrent writers onto SQLite's write lock, each busy-waiting in a raw nanosleep syscall that pins an OS thread in modernc.org/sqlite, crashing the engine with "fatal error: thread exhaustion" once the runtime hit its 10000-thread limit. Wrap the exporter in a BatchProcessor with the same NearlyImmediate interval already used for live span batching, so records are queued and inserted by one background goroutine per client instead. The synchronous OnEmit path is removed so it can't be reintroduced by accident. Signed-off-by: Alex Suraci <[email protected]>
Resolve and the list-element path in resolvePath used unbounded conc pools: one goroutine per sibling selection and one per list element, recursively. A single query selecting several fields on a large list (e.g. thousands of Workspace.search results) exploded into tens of thousands of concurrent resolutions, each emitting telemetry, which contributed to a thread-exhaustion engine crash. Cap each pool at 100 goroutines. Pools are per-node, so this bounds fan-out width rather than total request concurrency, and each pool's limiter is independent, so a blocked parent pool cannot deadlock a child pool. Legitimate parallel workloads still run 100 wide per level; pathological fan-outs are throttled instead of unbounded. Signed-off-by: Alex Suraci <[email protected]>
IsExpanded gated tool-call and rolled-up spans behind an unconditional neverExpand check, so no verbosity level could ever open them. Zooming a slow LLM tool call with 'dagger trace --span <id> -vvvvv' showed the same collapsed rollup as with no flag, hiding the call tree beneath it. Let a verbosity at or above the expand-completed threshold punch through the rollup boundary, matching the existing alwaysExpand gate, and honor per-span verbosity like the sibling ShouldShow checks. Below that threshold the guts stay hidden as before. Signed-off-by: Alex Suraci <[email protected]>
The trace loader always streamed priority spans and backfilled subtrees lazily on expand. But at a verbosity that expands every completed span (or --debug/--expand), the whole tree renders open, so lazy loading just leaves never-expanded spans unfetched in report mode's single render and costs a round-trip per expand interactively. Fetch the entire trace up front (Incremental: false) when the tree is going to be fully expanded anyway, keyed on the same threshold IsExpanded uses. Lower verbosities keep the incremental behavior. Signed-off-by: Alex Suraci <[email protected]>
a0981f4 to
e38e693
Compare
sipsma
left a comment
There was a problem hiding this comment.
Seems like there may be a meaningful regression in execution time for test-module? Saw a few runs and they were all way longer than it used to be. Not entirely sure but the concurrent resolvers could become a bottleneck for the type introspection queries that module loading especially does.
Could try just bumping the max concurrent resolvers. But I am also wondering if having a cap is really necessary if the fix is around thread-exhaustion. Goroutines don't force thread creation unless runtime.LockOSThread() is used and you are supposed to be able to run millions of goroutines on a single thread.
|
@sipsma Had an agent chew through different permutations with I agree though that fe763ea is probably redundant with e0e911e whichs lock the SQLite connection pool to 1, which should fix that source of OS thread locking (which is the real root cause of the crash). I opted to just include it anyway because it seemed like capping it at 100 per node (not the entire request) would be unlikely to affect normal paths and serve as a last line of defense for weird cases like this, but I have really low conviction there so I'll just drop it until we're sure it's needed; if we had it prior to this PR it may have just obfuscated the root cause. |
@vito Okay yeah I just saw a run last night that was slow and when I checked again this morning a new one was slow, so I assumed n=2 was statistically significant 🙂 Thanks for the extra checking, if it falls within noise range then no worries, we can iterate on it if anything concrete comes up |
|
Cool np - the agent is still working away and trying to make sense of things, letting it ride for now. None of my PRs are stacked on this and I only hit it from an LLM being an agent of chaos, so might as well let it cook. edit: Here's the report:Investigation:
|
| run | knobs (cumulative) | go-test |
|---|---|---|
| 1 | main | 420s |
| 2 | +SQLite pool cap=1 | 451s |
| 3 | +batch log writes | 484s |
| 4 | +dagql fan-out cap=100 | 577s |
This looked like a clean per-knob creep with the fan-out cap as the culprit (+93s) — exactly sipsma's hypothesis. But it's an artifact.
The control that broke it
A 5th run of plain main came back at 522s, not ~420s. Identical code, +102s. The shared cloud engine drifts — each successive run gets slower regardless of code.
Drift-controlled bracket (cap vs no-cap in adjacent runs)
| run | state | cap? | go-test |
|---|---|---|---|
| 6 | state2 | no cap | 577.6s |
| 7 | state3 | cap | 554.9s |
| 8 | state2 | no cap | 519.3s |
Fan-out cap effect = 554.9 − (577.6+519.3)/2 = +6.4s → noise. The adjacent deltas even straddle zero (cap −22s vs run6, +36s vs run8).
All 8 no-cap measurements: 420, 451, 484, 522, 577, 519 — a 158s spread on identical code. That ±90s variance swamps any per-knob effect. Per-operation type definitions (introspection) span durations tell the same story: cap runs sit ~2-3s higher only at p90, medians identical, all within the run-order drift.
Verdict
None of the three knobs produces a slowdown in test-modules that's separable from cloud run-to-run drift. The naive bisect was fooled by the engine getting ~25s/run slower on its own (main alone drifted 420→522; no-cap state2 went 484→577→519). The fan-out cap — the prime suspect — adds ~+6s in a drift-controlled test.
For sipsma specifically:
- His mechanistic worry (cap bottlenecks introspection) doesn't materialize: the cap only engages when one resolution level exceeds 100 parallel elements. Normal module introspection stays well under that, so it rarely bites — the >100 fan-out it targets is the pathological
Workspace.search-over-thousands case (PR Workspace file APIs: glob and search #13633), which isn't intest-modules. (Resolveeven has a no-pool fast path for single selections.) - His "is the cap even necessary?" point stands as a design question — the thread-exhaustion root cause was SQLite's busy-handler
nanosleeppinning OS threads, fixed by the SQLite-pool + batched-log knobs — but relaxing the cap wouldn't recover atest-modulesregression, because there isn't one attributable to it.
Bottom line: the "way longer" runs are most consistent with CI/cloud variance, not these knobs. I'd caution against tuning the cap to chase this specific regression.
Caveat: I couldn't get a stable baseline (main = 420–522s), so this cloud setup is too noisy to confirm or deny a small (<~90s) real regression. If you want certainty, the clean way is interleaved cap/no-cap runs on a fresh engine each time (or I can write a pure-resolution introspection micro-benchmark to bound the cap's cost directly, ~2 runs). Want me to do either?
(Bisect states preserved as tags bisect-state{0,1,2,3}-*; full log at /tmp/bisect-results.md.)
This reverts commit fe763ea. Should be redundant with the SQL conn pool cap. Signed-off-by: Alex Suraci <[email protected]>
Note
The catalyst for these fixes was something like
Workspace.search(".").{{ filePath, lineNumber, matchedLines }}with #13633 - i.e. a nested multi-field selection across a TON of search results, which made the engine crash via thread exhaustion. Alternative fixes could be applied elsewhere but these seem like reasonable boundaries to set to avoid a crash.Note
Stack 1/5, stacked on #13600 — each PR is based on the one below it, so the diff shows only this PR's own changes. Merge bottom-up; when a PR below merges and its branch is deleted, GitHub retargets this one automatically and the rest of the stack gets rebased.
Independent engine and TUI fixes, stacked at the bottom of the llm-workspace series to keep it linear (they don't depend on #13600's changes and could also target main):
🤖 Generated with Claude Code