Skip to content

[Bug]: One busy thread's lock stalls every thread's run-end reaction, so queued runs on idle threads do not start #37412

Closed

Description

@navidnadali

Note

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

Area

apps/server

Steps to reproduce

On main at 69f8f59b51, Orchestrator.ts handles run ends in handleRunUpdate, which runs as a single Stream.runForEach over
every thread's run.updated events. Each reaction:

  • takes the thread's dispatch lock for finalizeDelegatedCompletionDelivery, then again for releaseUnattendedHolds and
    startNextQueuedRun;
  • for a delegated child, also takes the parent's lock for finalizeAppOwnedSubagent.

The deferred-steer reactor and the child-settle reactor have the same single-consumer shape.

  1. Make thread A's lock busy for a while. A coordinator with many delegated children does this naturally: each child's
    bookkeeping runs under it (see [Bug]: A delegated child's checkpoint capture reads every open delivery of its parent while holding the parent's lock #37393), and a long send or queued start (A send or queued start to a long thread still reads 6–15 s of its history on main (follow-up to #29468) #36127) holds it for seconds.
  2. A run ends on thread A, or on one of A's children, so its reaction waits for A's lock.
  3. A run ends on an unrelated idle thread B that has a queued message.

B's queued run does not start until every reaction ahead of it is done, including the ones waiting for A's lock. If a send starts
a run on B in the meantime, the late reaction finds an active run and does nothing. The queue then waits for the next run end.
After #33857 a send waits behind the queue, so the queue depends on that reaction alone.

Expected behavior

A run end on one thread is handled without waiting for unrelated threads. Each thread's reactions stay in order.

Actual behavior

Measured in production, read-only from the trace and the state database, over a 36-minute window:

  • Reactions. The run-end reactor finished 26 of them, taking p50 27 s, p90 290 s and max 437 s. Most of that time was a wait
    for a lock before the reaction's first step. Back to back they added up to 2,167 s, so the consumer never idled.
  • Arrivals. Events arrived at 31–46 per 10 minutes and were finished at about 7. Most were delegated-completion bookkeeping
    rewrites of a parent's already-ended run, one per finished child.
  • An idle thread whose queue never started. Its run ended at 06:24 and its two unheld queued runs were still queued 15.5 h
    later. Its later runs were all started by direct sends. When one of them ended, the reaction for that thread had still not run
    31 minutes later. The rows at rest pass every check in startNextQueuedRun (canStartQueuedRun returns true), and nothing was
    logged.
  • Three more idle threads each had an unheld queued run and no active run for up to 17 h.
  • Two busy coordinators sat idle for 5–6 minutes after a run ended, with 199 and 1,184 unheld queued runs.

Impact

Under load, queued messages, automations and delegated results wait for hours, or until something else happens on the thread.
A restart drops the backlog: the streams start at latestSequence. Startup recovery then holds the queues, so the restart does
not start them either.

Version or commit

The code path is unchanged on main at 69f8f59b51. Measured on a build of main from 11-14 (before #33857) with local performance
patches that do not touch these reactors; the reactor code there is the same as on main.

Environment

Linux, Node 26, about 175 concurrent Claude and Codex sessions, coordinator threads with hundreds of delegated tasks.

Logs or stack traces

Spans: Orchestrator.handleRunUpdate, whose first child (finalizeDelegatedCompletionDelivery) starts up to 274 s after the span
does.

Workaround

Send a message to the stuck thread: a person's send releases or starts the queue. Or use queue.resume.

Related

A fix is ready: each reactor keeps one thread's reactions in order and handles different threads concurrently on a bounded pool
(a delegated child shares its parent's key), in about 170 lines of server code. Its regression tests fail on main by
assertion. 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