OpenAI realtime `RealtimeSession.aclose()` blocks ~20s on WebSocket close (OpenAI never sends a CLOSE frame)
Bug Description
For OpenAI realtime sessions, RealtimeSession.aclose() blocks for ~20s during session/agent shutdown. Because AgentSession.aclose() (and the job-shutdown path) awaits it, the whole teardown stalls ~20s on every realtime call.
Root cause is in livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/realtime_model.py:
RealtimeSession.aclose()closes the message channel andawaits_main_atask._main_ataskruns_run_ws, whose inner_send_taskcallsawait ws_conn.close()once the outbound channel drains (there is a secondawait ws_conn.close()in the_run_wsfinally).- aiohttp's
ClientWebSocketResponse.close()sends a CLOSE frame and then waits for the peer's CLOSE frame up to its timeout. OpenAI's realtime server does not send a CLOSE frame on session end (it drops the connection with EOF), so the handshake waits out the full timeout.
There is no timeout bound on that close inside the plugin, so aclose() stalls.
Expected Behavior
aclose() should return promptly. Because the server never sends a CLOSE frame, waiting for the handshake is pointless — a short bounded wait followed by force-detaching the transport is correct. Well-behaved servers (e.g. Azure OpenAI) that do complete the handshake should be unaffected.
Reproduction Steps
1. Run an agent with openai.realtime.RealtimeModel(model="gpt-realtime").
2. Connect, then have the remote participant disconnect (or otherwise end the session).
3. Measure the time from disconnect to aclose() / the on_session_end callback returning.
Observed: ~20s consistently (aiohttp close handshake waiting on a CLOSE frame OpenAI never sends).
Expected: sub-second / a small bounded wait.
Operating System
Linux (LiveKit Cloud) and macOS — OS-independent
Models Used
OpenAI Realtime API — gpt-realtime (RealtimeModel over WebSocket)
Package Versions
livekit==1.1.12
livekit-agents==1.6.4
livekit-plugins-openai==1.6.4
livekit-api==1.0.7
Session/Room/Call IDs
No response
Proposed Solution
# Bound the ws_conn.close() calls in _run_ws with a short timeout; force-detach on timeout.
_WS_CLOSE_TIMEOUT = 5.0 # real handshakes finish <1s; caps the dead wait
async def _close_ws(ws_conn):
try:
await asyncio.wait_for(ws_conn.close(), timeout=_WS_CLOSE_TIMEOUT)
except asyncio.TimeoutError:
# OpenAI realtime never sends a CLOSE frame; stop waiting so aclose() returns.
logger.debug("realtime ws close timed out; detaching")
Additional Context
- OpenAI Developer Community, "Lack of realtime websocket CLOSE frame": https://community.openai.com/t/lack-of-realtime-websocket-close-frame/1369160 (a participant said they'd add a CLOSE frame; still unimplemented as of ~May 2026).
- #5875 bounded AgentSession.aclose() at the supervisor level with a 60s ceiling — prevents an indefinite hang but sits far above this ~20s stall and doesn't address the WS close cause. Targeted fix is to bound ws_conn.close() inside the OpenAI plugin. PR to follow.