fix(elevenlabs): close TTS websocket on job shutdown
Summary
- register the ElevenLabs TTS
aclose()method as a job shutdown callback when its reusable multi-stream WebSocket is first acquired - register only once even when the WebSocket connection is reused
- close ElevenLabs-owned streams and the WebSocket without closing the job-owned
aiohttp.ClientSession
Problem
The ElevenLabs multi-stream WebSocket is intentionally reused across synthesis requests. AgentSession.aclose() stops active synthesis streams, but it does not close the configured TTS model, and FallbackAdapter.aclose() does not cascade cleanup to its child providers.
At job teardown, the shared HTTP context can therefore close while the ElevenLabs WebSocket is still open. The remote endpoint observes an abnormal 1006 closure because it never receives a WebSocket Close frame, even when synthesis completed successfully.
Why the job shutdown callback
The worker awaits AgentSession.aclose() before running job shutdown callbacks, then closes the shared HTTP context after those callbacks. Registering the concrete ElevenLabs model in that window:
- lets active synthesis streams stop first
- gracefully closes the reusable WebSocket
- preserves ownership of the shared HTTP session
- avoids closing provider connections during agent handoffs
This is deliberately narrower than closing TTS models from AgentActivity, as discussed in #3825, and avoids the shared-session ownership issue identified in #4745.
Tests
The regression test uses the real ElevenLabs plugin against a local aiohttp WebSocket server and verifies that:
- job shutdown produces close code
1000 - repeated connection acquisition registers only one callback
- the supplied/shared HTTP session remains open
Commands run:
uv run pytest --plugin elevenlabs tests/test_plugin_elevenlabs_tts.py -qmake check