fix(cli): prevent 'Press ENTER to continue...' on exit#2555
Merged
Conversation
When AsyncOpenAI clients are garbage-collected after the event loop closes, their AsyncHttpxClientWrapper.__del__ tries to schedule aclose() on the dead loop, causing RuntimeError: Event loop is closed. prompt_toolkit catches this as an unhandled exception and shows 'Press ENTER to continue...' which blocks CLI exit. Fix: Add shutdown_cached_clients() to auxiliary_client.py that marks all cached async clients' underlying httpx transport as CLOSED before GC runs. This prevents __del__ from attempting the aclose() call. - _force_close_async_httpx(): sets httpx AsyncClient._state to CLOSED - shutdown_cached_clients(): iterates _client_cache, closes sync clients normally and marks async clients as closed - Also fix stale client eviction in _get_cached_client to mark evicted async clients as closed (was just del-ing them, triggering __del__) - Call shutdown_cached_clients() from _run_cleanup() in cli.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
During normal CLI usage (not on exit), users see this interrupting their session:
Root Cause
The OpenAI SDK wraps httpx's
AsyncClientinAsyncHttpxClientWrapperwhich has a__del__:The mid-session flow that triggers this:
AsyncOpenAIclient bound to thread's event loop → client is cached in_client_cache_get_cached_client()detects stale entry (loop is closed) → evicts it withdeldeltriggers__del__→asyncio.get_running_loop()finds prompt_toolkit's running loop → schedulesaclose()aclose()tries to close TCP connections on the dead worker loop →RuntimeError: Event loop is closedFix
_force_close_async_httpx()— sets httpxAsyncClient._state = ClientState.CLOSED, so__del__checksis_closedand returns immediately (no-op)_get_cached_client) — now calls_force_close_async_httpx()beforedel, preventing the mid-session errorshutdown_cached_clients()— called from_run_cleanup()on CLI exit to clean up any remaining cached clients_run_cleanup()in cli.py callsshutdown_cached_clients()Files Changed
agent/auxiliary_client.py— new shutdown functions + fix stale evictioncli.py— call shutdown in cleanup