hermes-agent/tests/gateway/test_api_server_turn_executor.py
Teknium 5f0182e611 fix(api_server): the API turn executor is retired at adapter disconnect
API turns used to run on the loop's default executor, which went away with its loop. The
module-global executor that replaced it outlived every adapter and loop: parked threads
lingered after disconnect, and in one test process a worker an earlier test left finishing
raised the process-global api_worker_live_count inside the shutdown close-gate tests
(test_shutdown_executor_quiesce: "SessionDB was never closed", "assert 2 == 1").

The executor is now created on first use and shut down without a join at adapter disconnect
(_close_run_state): parked workers exit, a worker inside a turn finishes it and keeps its
live-count share until then (the shutdown gate is unchanged), and a later submit gets a fresh
executor. The gateway tests' conftest joins what a test left running, as each test's loop
teardown did before. The run_shutdown comment no longer calls the executor bounded.
2026-10-27 12:35:50 -07:00

72 lines
2.5 KiB
Python

"""API-server agent turns must not occupy asyncio's default executor (#144362).
The session handlers do their SessionDB work with ``asyncio.to_thread`` (the default pool). If live
turns hold that pool, every /api/sessions read queues behind a whole turn.
"""
import asyncio
import os
import threading
import pytest
from gateway.platforms import api_server_runs as api_runs
def _default_pool_size() -> int:
return min(32, (os.cpu_count() or 1) + 4)
@pytest.mark.asyncio
async def test_live_api_turns_leave_the_default_pool_to_session_reads():
loop = asyncio.get_running_loop()
release = threading.Event()
started = threading.Semaphore(0)
n = _default_pool_size() + 2
def _turn():
started.release()
release.wait(10)
turns = [api_runs._submit_api_worker(loop, _turn) for _ in range(n)]
try:
session_read = asyncio.ensure_future(asyncio.to_thread(lambda: "row"))
assert await asyncio.wait_for(session_read, 3) == "row", "session read queued behind live turns"
await asyncio.wait_for(asyncio.to_thread(lambda: [started.acquire(timeout=5) for _ in range(n)]), 8)
assert api_runs.api_worker_live_count() >= n
finally:
release.set()
await asyncio.gather(*turns)
@pytest.mark.asyncio
async def test_cron_fire_body_keeps_contextvars_off_the_default_pool():
import contextvars
var = contextvars.ContextVar("probe_scope", default="unset")
var.set("profile-a")
seen = {}
def _fire(job, *, adapters, loop):
seen.update(job=job, scope=var.get(), adapters=adapters)
return "fired"
out = await api_runs.on_turn_executor(_fire, "job-1", adapters={"a": 1}, loop=asyncio.get_running_loop())
assert out == "fired"
assert seen["scope"] == "profile-a" and seen["job"] == "job-1" and seen["adapters"] == {"a": 1}
@pytest.mark.asyncio
async def test_adapter_disconnect_leaves_no_api_turn_thread_behind():
from gateway.config import PlatformConfig
from gateway.platforms.api_server import APIServerAdapter
loop = asyncio.get_running_loop()
ran_on = []
await api_runs._submit_api_worker(loop, lambda: ran_on.append(threading.current_thread()))
adapter = APIServerAdapter(PlatformConfig(enabled=True))
await adapter.disconnect()
ran_on[0].join(5)
assert not ran_on[0].is_alive(), "an idle API turn thread outlived the adapter's disconnect"
assert await api_runs._submit_api_worker(loop, lambda: "ran") == "ran", "no API turn ran after it"