kaxil opened a new pull request, #77384: URL: https://github.com/apache/airflow/pull/77384
A task that calls `run_durably_sync` twice fails on the second call with `RuntimeError: Event loop is closed`. That is any task that runs two agents, or one agent on two questions, with the SDK's default client or with one `AsyncOpenAI` passed to both runs. `run_durably_sync` ran each call through [`run_coroutine_sync`](https://github.com/apache/airflow/blob/42a6aa28ecd3c7036bc4bac2d57e215fa77a5bf7/providers/common/ai/src/airflow/providers/common/ai/utils/coroutines.py#L30-L47), which uses `asyncio.run`: a new event loop per call, closed when the call returns. The OpenAI Agents SDK sends every request on [one process-wide HTTP client](https://github.com/openai/openai-agents-python/blob/0a74177ed23ffb0c2dba24eda3fb12fb58e3e09d/src/agents/models/openai_provider.py#L36-L42), and an `AsyncOpenAI` given to several runs pools its connections the same way. Those connections belong to the loop of the first call, so the second call reuses connections of a closed loop. It has been there since `run_durably_sync` was added in #74770 (Common AI 1.0.0). Phani pointed at the same loop behaviour in review of #77139. `run_durably_sync` now does what the SDK's own `Runner.run_sync` [does on purpose](https://github.com/openai/openai-agents-python/blob/0a74177ed23ffb0c2dba24eda3fb12fb58e3e09d/src/agents/run.py#L2546-L2580): it runs on the thread's event loop and leaves it open, so both share one loop and the SDK's client works in either order. Two things `asyncio.run` did are kept: - Tasks a call started and left unfinished are cancelled and drained before it returns. Without that, a run interrupted by `execution_timeout` or a failed `gather()` would leave work that resumes inside the next, unrelated call. - An `asyncio.run()` between two calls unsets the thread's current loop. Building `AirflowTools(<toolset>)` does one, so `run_durably_sync(build_agent(), q)` in a loop would otherwise lose the kept loop. The helper remembers the loop per thread and puts it back; a forked child does not reuse its parent's. ## Design rationale **Why only `run_durably_sync`, not the shared `run_coroutine_sync`.** `run_coroutine_sync` also builds toolsets' tools at Dag parse time and backs the Anthropic, LangChain and Strands bridges. A loop left open across a `fork()` shares its epoll instance with the child, and a child that closes it stalls the parent's loop. Keeping that exposure to `run_durably_sync` makes it no wider than `Runner.run_sync`'s, which users of the SDK already have; the other callers keep `asyncio.run`. The new `run_coroutine_on_thread_loop` sits beside `run_coroutine_sync` in `utils/coroutines.py`, and only `run_durably_sync` uses it. ## Evidence On a live stack (Airflow main, LocalExecutor, Postgres), `openai-agents` 0.24.0, `gpt-5.4-mini` behind an OpenAI-compatible gateway, one task per row, each calling `run_durably_sync` twice: | Task | Before | After | |---|---|---| | No client passed (the SDK's shared HTTP client) | `RuntimeError: Event loop is closed` | `['first', 'second']` | | One `AsyncOpenAI` passed to both runs | `RuntimeError: Event loop is closed` | `['first', 'second']` | | `AirflowTools(SQLToolset(...))` built before each run | `RuntimeError: Event loop is closed` | `['2', 'orders']` | Outside Airflow, `Runner.run_sync` and `run_coroutine_on_thread_loop` share the SDK's client in both orders (`run_sync`, ours, `run_sync` and the reverse). `test_two_runs_in_a_task_can_share_one_pooled_client` fails with `Event loop is closed` on main and with `bound to a different event loop` without the loop being put back after `asyncio.run`. It runs in the agent-framework CI job, which installs `openai-agents`. ## Gotchas - From inside a running event loop, `run_durably_sync` still runs on a new loop per call, as before. In async code, `await run_durably(...)`. - On a thread that exits, the loop stays open until garbage collection, as with `Runner.run_sync`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
