Skip to content

[Bug]: A delegated child's checkpoint capture reads every open delivery of its parent while holding the parent's lock #37393

Closed

Description

@navidnadali

Note

Written by Claude Opus 5.5 (Claude Code) on behalf of Navid

Area

apps/server

Steps to reproduce

Code path on main at 295a0a733:

  1. EffectWorker.ts runs checkpoint.capture. After runFinalization.finalize, it calls
    recoverDelegatedTask(threadId, { completedRunId }). fix(server): preserve delegated results across wake turns #34568 added this so completion bookkeeping survives a missed run update.
  2. For an app-owned child where hasPendingDelegatedCompletion(child) is true, recoverDelegatedTask runs
    offerDelegatedCompletionDeliveries(parentThreadId) under threadDispatch.withLock(parentThreadId). That predicate is also
    true while the child's task has no result yet, so it holds on every turn of a multi-turn child.
  3. offerDelegatedCompletionDeliveries reads all of the parent's runs with no filter. Then, for every run with a recorded
    delegatedCompletion.delivery, it calls offerDelegatedCompletionDelivery, which makes up to five reads: runs, messages,
    runs, provider turns by attempt, provider threads.

To reproduce:

  • A coordinator thread delegates many tasks over time.
  • Its completion wakes queue behind its own work, one per parent run, so the parent collects many runs whose delivery is still
    open.
  • Any child of that parent then finishes a turn.

Each child capture now makes about 4 reads per open delivery on the parent, plus a read of all of the parent's runs. It does
this inside an effect worker and while holding the parent's dispatch lock.

Expected behavior

A child's capture offers only the delivery of the parent run its own task belongs to. Its cost does not depend on how many
other open deliveries the parent has. Startup recovery (recoverDelegatedTasks) can keep the full sweep.

Actual behavior

Measured in production, read-only from the server's trace file, in two windows of 215 s and 307 s:

Open deliveries on the largest parent 1,187 (about 13.5k runs). A second parent had 217.
Reads per child capture 4,733–4,758 on the large parent, about 870 on the second. They fit 4 × open deliveries + 10.
Capture time in its worker 22–334 s across 17 captures. 98–99 % of a long one is recoverDelegatedTask: for example 334.3 s = 1.5 s finalize + 332.8 s recovery.
Workers At one point all four effect workers held captures of children of the same parent: one reading, three waiting for the parent's lock.
Unrelated threads A turn start waited 384–1,099 s to be claimed, then ran in 1.0–2.3 s.
Parent lock Held for the whole sweep, so the parent's own sends, Stop and run-end reactions waited minutes too.
SQLite The provider-turn read by attempt was 41–52 % of all SQLite statement time. One reader worker ran at 55–62 % CPU while sweeps ran.

Those times come from a build before 093 (the run_attempt_id index) and #34551. With both, each read is a point lookup, so the
per-read time is lower on main. The number of reads per capture is the same, and so are the parent's lock and the worker held
across the lock wait.

Impact

On a busy coordinator, every child turn end costs thousands of reads under the parent's lock, so the cost grows with
delegation volume squared. In production:

  • turn starts on unrelated threads waited 6–18 minutes to be claimed;
  • the parent's own messages and run-end reactions waited behind the sweep.

Version or commit

The code path is unchanged on main at 295a0a733. Measured on a build of main from 11-14 (it includes #34568, not 093 or
#34551), with local performance patches that do not touch this path.

Environment

Linux, Node 26, about 175 concurrent Claude sessions, a 153 GiB state database. Coordinator threads delegate a few hundred tasks
an hour.

Logs or stack traces

Aggregates only (above). The trace spans are EffectWorker.runEffect > Orchestrator.recoverDelegatedTask with
effect.type=checkpoint.capture.

Workaround

None in the app. Cancelling the parent's queued completion wakes shrinks the sweep, but it drops the results they carry.

Related

A narrow fix with a regression test is ready: 26 lines; the test fails on main by assertion; a capture with 1,200 open deliveries drops from 9,644 reads to 46. I can open it when I have a free PR slot, or a maintainer can take it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions