Skip to content

fix(core): stranded sessions hold the agent lock, deadlocking 4 app-core tests #126

Closed

Description

@KooshaPari

Two resume/attach paths strand a session: after the local client detaches (or a peer takes over), the server-side AppCore session is never torn down, so its agent task, pending turn, and idle-prewarm work keep running with no live client attached. The stranded session holds the agent lock, which blocks subsequent resume/attach attempts on the same session and leaves idle_prewarm waiting forever on pending_preparation.

What I observed

Full suite (workspace, run with export TMPDIR=/tmp/kcode-neutral-tmp) completed 298 passed, 0 failed, 4 deadlocked. The suite-level set is:

  • concurrency::viewer_attach_reuses_live_owner_without_tracking_placeholder
  • idle_prewarm_never_holds_agent_lock_across_pending_preparation
  • idle_prewarm_starts_before_user_input_and_skips_busy_sessions
  • reload_tests::handle_reload_does_not_wait_for_busy_agent_lock

Narrowed (2026-10-18T19:59Z): the reproducible core is a 2-test pair — viewer_attach_reuses_live_owner_without_tracking_placeholder + idle_prewarm_never_holds_agent_lock_across_pending_preparation. Run alone each passes in 0.32s / 0.65s; run together they deadlock each other (exit 124 at 150s). The other two pass in every pairing tried. The pair hangs identically at --test-threads=4, 16, so this is lock-order starvation on the global guard, not thread-count pressure.

Root cause (un-fixed, needs a decision)

crates/jcode-base/src/storage.rs:34 lock_test_env() returns a std::sync::MutexGuard (a non-async mutex). Measured on the resume branch at 008894ab2: 781 bare call sites across 194 files, of which 209 #[tokio::test] functions across 64 files await while holding the guard (a second scope-aware parse gives 227 acquisitions with .await in scope, across 71 files), i.e. they hold a blocking guard across an await on the async test runtime.

Correction chain (two wrong figures, both retracted): an earlier version of this issue said "63 call sites across 23 files, 16 awaiting". That under-counted by ~12x. The figure before that ("~284 sites across 74 files") was a loose grep that also matched three unrelated same-named helpers. The current figures come from a scope-aware parse cross-checked with a reverse pass over #[tokio::test] bodies. 19 files (macro/closure bodies) still need manual triage.

Superseded corrections, kept for the audit trail. (2026-10-18T19:28Z) I first replaced "~284 call sites across 74 files" — a loose grep -rn "lock_test_env()" that over-counted by matching three unrelated same-named helpers (jcode-telemetry-core/src/tests.rs, jcode-provider-bedrock/src/lib.rs, crates/jcode-herdr/src/env.rs) — with "63 call sites across 23 files, 16 awaiting". That second number was also wrong, in the opposite direction: it under-counted by ~12x. The figures above are the current ones. Do not cite either of the two older numbers.

Correction

An earlier revision of this fix PR reported the 7 hanging resume tests in crates/jcode-app-core/src/server/client_session_tests/resume* as fixed, using a wrong test path. The earlier isolation runs used client_session_tests::, which matched 0 tests, so they silently proved nothing — the tests never ran. With the correct path all 7 now genuinely pass (24 passed, 0 failed, 1 ignored browser test).

Out of scope here

  • Fixing lock_test_env (production change to a shared crate used by 284 call sites)
  • The 8 pre-existing cargo check warnings in crates/jcode-setup/bin/update_product.rs (still claims 1jehuang/jcode pre-rebrand branding)
  • Deleting the dead startup-spawn notice (Task D)

Suggested direction

Have the server treat "no live client + no pending peer takeover" as a settlement signal that drains the agent task and releases the agent lock, rather than leaving the session parked indefinitely. A narrower fix is to make lock_test_env() async-aware (or scope the guard so it is never held across .await) so a single blocked test cannot starve the whole runtime.

I filed this issue so the resume-fix PR can link a real issue; I am not proposing a specific patch yet.

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