Skip to content

Close the agentic round's loop and recorder server; node lease tests see no host processes - #926

Open
michaellchung wants to merge 1 commit into
mainfrom
oc-z/deterministic-resend-and-fabric
Open

michaellchung wants to merge 1 commit into
mainfrom
oc-z/deterministic-resend-and-fabric

Conversation

@michaellchung

Copy link
Copy Markdown
Contributor

Summary

Two tests failed depending on what else ran on the host or in the same shard. One root
cause is in product code (AgenticRound.close); the other is in a test helper.

test_fabric_groups: asyncio log records from an earlier test

test_fabric_groups asserts on its log capture (caplog.records == [fg.HINT]). In the
shard it shares with test_agentic_compaction_switch, a garbage collection inside a
fabric test destroyed tasks that the agentic tests had left pending, and asyncio logged
Task was destroyed but it is pending! (aiohttp RequestHandler) into that capture. A
leak detector (a gc.collect() after every test, with asyncio log records, unraisable
exceptions and ResourceWarnings recorded) found the source: every agentic round left an
unclosed event loop and the recorder server's listening socket, and a round closed during
a summary turn also left that pending handler task. No other test in the shard leaked
asyncio objects. The warning is fixed where it starts, not filtered in test_fabric_groups.

test_node_lease: the probe saw other tests' processes

still_runs_script scans ps -eo pid=,args= for venv sampler markers. The tests ran it
against the host's real process table, so a yeto-ssh-sampler:run-a:... process left by
another test process on the same host showed up as sampler:<pid> in five tests
(test_the_probe_finds_trainer_process_groups_and_venv_samplers, the docker and podman
view tests). The fake node PATH (_node_bin) now also has a fake ps. It lists the rows
a test writes to ps.txt, plus the probe's own shell (so the "the probe never finds
itself" check still holds). The venv-sampler test writes its samplers there instead of
starting a real process.

Product change: AgenticRound.close (engine sampler, agentic rounds)

Scope: yeto/rl/engine/agentic_round.py only. LoopThread.close() and
AgenticRound.close() changed; a helper LoopThread.cancel_pending() was added.

Before:

  • LoopThread.close() stopped the loop and joined the thread. It never closed the loop.
    Pending tasks and the selector stayed alive until the garbage collector found them.
  • AgenticRound.close() never stopped the recorder's aiohttp server, which runs on that
    loop. Its listening socket stayed open, and so did a request still in the engine
    (aiohttp does not cancel a handler whose client left).
  • A second AgenticRound.close() blocked forever, because run_coroutine_threadsafe on
    the stopped loop never returns.

After, in this order:

  1. Unchanged: cancel the trials and give them up to CLOSE_WAIT_S (2 s) to end. A
    cancelled trial's own cleanup on the loop still runs to its end before anything below.
  2. New: when the recorder has aclose (ProxyTokenRecorder), cancel what is still
    pending on the loop (the leftover recorder requests), then stop the recorder's server,
    bounded by CLOSE_WAIT_S. A failure here is swallowed, and the loop still closes.
  3. Unchanged: delete the pooled boat sandboxes (close_pools_now). Its error still
    propagates to the caller, as before.
  4. Changed: LoopThread.close() cancels the remaining tasks (bounded, 2 s), shuts down
    async generators, stops the loop, joins the thread and closes the loop. It does
    nothing if the loop is already closed.
  5. New: a second AgenticRound.close() returns 0 at once.

Impact:

  • The customer K3.1 agentic sampler (round_factory) calls close() once, at the end
    of the sampler run (engine_sampler). What happens before it is unchanged: rounds,
    drains, the live and drain weight switches, the infra retries. At the end, the
    recorder's port is released and the loop is closed, where before they lingered until
    the process exited.
  • A run that closes the round and keeps the process alive no longer leaks a selector,
    a socket and the default executor's threads.
  • Nothing closes the loop earlier than before: before, close() already stopped the
    loop at the same point. The new steps run only inside close(), after the trials were
    cancelled and waited for.
  • No CLI, config, plan or launch argv change. The customer gate below is identical to
    main.

New tests (tests/test_agentic_compaction_switch.py):

  • Normal end: after finished rounds, close() writes the samples, stops the recorder
    server (its port refuses connections), closes the loop, emits rl_agentic_close once,
    and a gc.collect() afterwards logs nothing from asyncio. Before close(), the loop is
    open and the server answers (nothing closed early).
  • Close during a summary turn: the pending recorder request is cancelled, nothing is left
    for the collector.
  • A cancelled trial whose cleanup awaits on the loop finishes that cleanup before the
    loop closes.
  • Errors: a recorder whose aclose raises does not fail close(), and the loop is
    closed. A close_pools_now error still raises, and the loop is closed anyway.
  • A second close() returns 0 and changes nothing: the pooled sandboxes are not deleted
    again, rl_agentic_close is not emitted again, and no coroutine is handed to the closed
    loop (no "never awaited" warning). Before this change it blocked.
  • LoopThread.close() cancels a pending task, closes the loop, can be called twice, and
    leaves nothing for the collector.

Verification

Fault injection was done with local-only pytest plugins; none of it is in this PR.

  • test_fabric_groups. Injection: the automatic garbage collector is off, and a full
    collection runs at the start of each output-sensitive fabric test (inside its log
    capture), after the agentic close tests. Old code: 20/20 iterations failed
    (['Task was destroyed but it is pending! ...'] == [HINT]). New code: 200/200
    iterations passed (the agentic close tests and the four fabric tests in each).
    CPU load: 200/200 passed.
  • test_node_lease. Injection: a ps wrapper first on the pytest process's own PATH
    adds another test's yeto-ssh-sampler:run-a: row to full listings (nothing runs on the
    host). Old code: the same five tests failed. New code: 200/200 iterations of those
    five passed. CPU load: 200/200 passed.
  • Every test file that uses agentic_round (23 files), plus test_fabric_groups and
    test_node_lease: 887 passed, 22 skipped, with -W error::RuntimeWarning.
  • Mutations of the close change (each applied alone, then the agentic close tests and
    test_fabric_groups run): no second-close guard; no recorder stop; no loop.close();
    no early return of LoopThread.close() on a closed loop; no task cancel in
    LoopThread.close(); no cancel before the recorder's aclose; an aclose error not
    swallowed; the recorder stopped before the trials were waited for; the loop closed
    without shutdown_asyncgens and without the join check. All 9 are caught.
  • Customer gate: the 52 goldens pass, and the examples/k31 dry-runs (run up --dry-run of both run configs with their preflight logs, and the pool --dry-run)
    are identical line by line to origin/main after normalization.

Not changed: tests/test_bulk_resend.py

A timing failure under load was reported for this file, without the test name or the
assertion. It did not reproduce: 3 runs of the file under CPU load, one with every push
delayed 25 ms and one with a niced syncer all passed. The margins measured: merges during
the gate were 239 to 352 against the 10 required; late merges 127 to 184 against 5; the
replay-tail backlog 151 to 197 against more than 0; merge rate 26 to 35/s against one
merge each second. The assertions that depend on wall time are: the inline cases
(during == 0 from 0.5 s into the gate, late == 0 in its second half), the background
lower bounds after a fixed GATE_S sleep, "a second without a merge", and the 90 s
replay window. They are left as they are: without a failure to aim at, a change could not
be checked with the failing-injection and 200-iteration protocol (one run of the file
takes 4 to 5 minutes).

🤖 Generated with Claude Code

… processes

AgenticRound.close() stopped its event loop but never closed it, and the
records proxy recorder's aiohttp server on that loop kept its listening
socket and any request still in the engine (aiohttp does not cancel a
handler whose client left). The garbage collector later destroyed the
pending handler tasks wherever it ran, and asyncio logged "Task was
destroyed but it is pending!" into that test's log capture:
test_fabric_groups' caplog assertions failed after
test_agentic_compaction_switch ran in the same shard.

close() now cancels what is still pending on the loop, stops the
recorder's server (when the recorder has aclose), shuts down async
generators and closes the loop. A second close returns at once (it used
to block on the stopped loop). The trials are still cancelled and given
CLOSE_WAIT_S first, so a cancelled trial's cleanup runs before the loop
closes, and a failure to stop the recorder does not fail the close.

test_node_lease's probe scanned the host's real process table, so a
venv sampler marker left by another test process on the same host
(yeto-ssh-sampler:run-a:...) showed up as sampler:<pid> in five tests.
The fake node PATH now has a ps that lists only the processes a test
writes to ps.txt, plus the probe itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant