Skip to content

engine: stop fresh remote-cache engines running out of memory on large module workloads - #14884

Merged
sipsma merged 8 commits into
mainfrom
sipsma/fresh-engine-memory
Oct 27, 2026
Merged

sipsma merged 8 commits into
mainfrom
sipsma/fresh-engine-memory

Conversation

@sipsma

@sipsma sipsma commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Symptom

A fresh engine using the remote cache, running a large module workload (our test-base check), grew to about 15.9 GB and was killed for lack of memory, on 16-core, 32 GB machines. Engines that weren't fresh, on the same workload, stayed around 3 GB.

Cause

Heap profiles of the live engine showed most of the growth in cause links: the client telemetry store's span index, and the span-ID strings in it.

When the engine runs deferred work for a lazy result, it starts a hidden evaluation span. That span cause-links the call that returned the result and every span that installed it, which often means hundreds of module calls. On the remote-cache path, each part of a result is evaluated as its own span, and every one repeats the same list. The span index kept each link twice, as strings, in a map from linked span to linking spans and back, and never shared anything between spans.

What this changes

  • dagql: a part cleanup's function is released once it has run, so a completed re-run no longer keeps its invocation, and everything it references, alive.
  • clientdb: the span index interns span IDs and stores each distinct set of link targets once, shared by every span carrying it, with an index from each target to the sets that contain it. When a span's links grow, it moves to the larger set, and a set no span carries any more is dropped. Every query returns what it did before: causal children and parents, log scope, and whether a span has descendants.
  • telemetry: each session logs, when its telemetry shuts down, how many spans its bounded live span lane dropped on overflow. If the shutdown was cut short, it logs the number still outstanding instead of calling them dropped.

Nothing users see changes. Traces, the TUI, Cloud and every store read are as before. The only addition is one log line per session.

Result

On a 16-core spot machine running the test-base check, seed then fresh with the remote cache, in two runs:

  • Fresh engine: peak RSS 6.1 and 6.0 GB, against about 15.9 GB before (killed in two runs). It plateaus at 4–6 GB instead of climbing.
  • Seed engine: peak 3.0 and 3.2 GB, unchanged.
  • Speed: no slowdown. Through 10 minutes, the fresh engine progressed at the same rate as one without the index. Then it kept going where that one nearly stalled under memory pressure, and finished the same work sooner. Its telemetry writes had a lower tail (p99 3.2 s against 8.5–10.4 s).
  • Dropped spans: none. The fresh test-base session queued 421,320 spans on the bounded lane and dropped 0. This now shows in the engine's log.

A follow-up change (separate PR) cuts the repeated links at their source, so that later evaluations of a result link only its first one.

Tests

All are package tests in the test-split/test-packages check:

  • TestPartLazyOperationCleanupReleasesItsInvocation (dagql): a completed cleanup no longer keeps its invocation.
  • TestSpanLookupSharedCauseLinksStayCompact, TestSpanLookupGrowingCauseLinksStayCompact and TestSpanLookupCauseLinkAnswers (engine/clientdb): memory proportional to distinct link sets, including a span whose links grow one at a time; answers identical to a straightforward index under randomized inputs.
  • TestSpanQueueCountsDrops and TestSpanQueueCountsShutdownCutShort (engine/telemetry): the drop count is exact against a stalled exporter, and a cut-short shutdown isn't reported as drops.

A test that fails today. When a lazy operation's output is installed, the
operation's cleanup runs and releases what it built: for a no-source
decision, the private object decoded from the row's record, its recipe
and the record. The row keeps the cleanup in its release chain and in its
installed outputs, and the cleanup keeps its function after running, so
everything the operation built stays in memory for as long as the row
does.

Signed-off-by: Erik Sipsma <[email protected]>
An installed row keeps its install's cleanup in its release chain and in
its installed outputs, both as long-lived as the row. The cleanup ran once
the part was installed, but kept its function, so everything that
function captured stayed in memory with the row. For a lazy operation
re-run because a row from the remote cache had no installable content,
that is the private object decoded from the row's record, its recipe and
the record, about a megabyte a re-run. A fresh engine re-running
thousands of such rows grew by gigabytes and was killed.

A cleanup that has run now drops its function. A failed one keeps it, so
a later release still runs it.

Signed-off-by: Erik Sipsma <[email protected]>
A lazy evaluation's span cause-links the call that returned its result
and every span that installed it, often hundreds of spans, and part
demand runs many evaluations of one result with the same list. Test that
the client telemetry store's span index keeps memory proportional to
the distinct link lists rather than to every link, including when one
span's links grow a row at a time, and that its answers (causal children
and parents, log scope) match a straightforward index's.

Signed-off-by: Erik Sipsma <[email protected]>
The span index kept every cause link twice, as span-ID strings in a map
from each linked span to its linking spans and back. On a fresh
remote-cache engine running a large module workload, these maps grew to
several gigabytes, and the engine was killed for lack of memory.

Intern span IDs and store each distinct set of link targets once,
shared by every span carrying it, with an index from each target to the
sets containing it. When a span's links grow, it moves to the larger set
and a set no span carries any more is dropped, so a span gaining links
one at a time keeps the index proportional to its links. Every query
returns what it did before.

Signed-off-by: Erik Sipsma <[email protected]>
The bounded live span queue drops spans on overflow rather than stall a
session, and nothing reports how many. Test that a counted lane reports
exactly the spans a stalled consumer never got, zero without overflow,
and that a shutdown cut short by its deadline is reported as incomplete
rather than as drops, since the queue still hands its spans on.

Signed-off-by: Erik Sipsma <[email protected]>
The bounded live span queue drops spans on overflow rather than stall
the session, and the SDK keeps its drop count to itself. Count the spans
the session's lane takes in and hands on to its exporter, and log the
result once per session when its telemetry shuts down: the number
dropped when the shutdown completed, a warning when any were, and the
number still outstanding, without calling them dropped, when the
shutdown was cut short and the queue is still draining.

Signed-off-by: Erik Sipsma <[email protected]>
@grouville
grouville self-requested a review October 27, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants