What is this error can any one tell me? Is this Li...
# ask-ai
d
What is this error can any one tell me? Is this LiveKit end's or my code or sarvam? {"level": "ERROR", "name": "livekit.plugins.sarvam.log", "message": "Error in send task: Cannot write to closing transport\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 553, in send_task\n await ws.send_str(json.dumps(config_msg))\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 245, in send_str\n await self._writer.send_frame(\n data.encode(\"utf-8\"), WSMsgType.TEXT, compress=compress\n )\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 83, in send_frame\n self._write_websocket_frame(message, opcode, 0)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 151, in _write_websocket_frame\n raise ClientConnectionResetError(\"Cannot write to closing transport\")\naiohttp.client_exceptions.ClientConnectionResetError: Cannot write to closing transport", "session_id": 140708909754816, "connection_state": "connected", "model": "bulbul:v2", "speaker": "abhilash", "pid": 84, "job_id": "AJ_MoYmwCVPeWYj", "room_id": "RM_ogAu5EFYdvjN", "timestamp": "2025-11-19T074444.488063+00:00"} {"level": "ERROR", "name": "livekit.plugins.sarvam.log", "message": "Error in receive task: Cannot write to closing transport\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 576, in recv_task\n msg = await ws.receive(timeout=self._conn_options.timeout)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 378, in receive\n await self.pong(msg.data)\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 234, in pong\n await self._writer.send_frame(message, WSMsgType.PONG)\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 83, in send_frame\n self._write_websocket_frame(message, opcode, 0)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 151, in _write_websocket_frame\n raise ClientConnectionResetError(\"Cannot write to closing transport\")\naiohttp.client_exceptions.ClientConnectionResetError: Cannot write to closing transport", "session_id": 140708909754816, "connection_state": "connected", "model": "bulbul:v2", "speaker": "abhilash", "pid": 84, "job_id": "AJ_MoYmwCVPeWYj", "room_id": "RM_ogAu5EFYdvjN", "timestamp": "2025-11-19T074444.488895+00:00"} {"level": "ERROR", "name": "livekit.plugins.sarvam.log", "message": "WebSocket session failed: Send task failed: Cannot write to closing transport (body=None, retryable=True)\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 553, in send_task\n await ws.send_str(json.dumps(config_msg))\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 245, in send_str\n await self._writer.send_frame(\n data.encode(\"utf-8\"), WSMsgType.TEXT, compress=compress\n )\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 83, in send_frame\n self._write_websocket_frame(message, opcode, 0)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 151, in _write_websocket_frame\n raise ClientConnectionResetError(\"Cannot write to closing transport\")\naiohttp.client_exceptions.ClientConnectionResetError: Cannot write to closing transport\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 621, in _run_ws\n await asyncio.gather(*tasks)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 571, in send_task\n raise APIConnectionError(f\"Send task failed: {e}\") from e\nlivekit.agents._exceptions.APIConnectionError: Send task failed: Cannot write to closing transport (body=None, retryable=True)", "session_id": 140708909754816, "connection_state": "connected", "model": "bulbul:v2", "speaker": "abhilash", "pid": 84, "job_id": "AJ_MoYmwCVPeWYj", "room_id": "RM_ogAu5EFYdvjN", "timestamp": "2025-11-19T074444.489503+00:00"} {"level": "ERROR", "name": "livekit.plugins.sarvam.log", "message": "Unexpected error in WebSocket session: Send task failed: Cannot write to closing transport (body=None, retryable=True)\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 553, in send_task\n await ws.send_str(json.dumps(config_msg))\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 245, in send_str\n await self._writer.send_frame(\n data.encode(\"utf-8\"), WSMsgType.TEXT, compress=compress\n )\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 83, in send_frame\n self._write_websocket_frame(message, opcode, 0)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 151, in _write_websocket_frame\n raise ClientConnectionResetError(\"Cannot write to closing transport\")\naiohttp.client_exceptions.ClientConnectionResetError: Cannot write to closing transport\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 621, in _run_ws\n await asyncio.gather(*tasks)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 571, in send_task\n raise APIConnectionError(f\"Send task failed: {e}\") from e\nlivekit.agents._exceptions.APIConnectionError: Send task failed: Cannot write to closing transport (body=None, retryable=True)", "session_id": 140708909754816, "connection_state": "failed", "model": "bulbul:v2", "speaker": "abhilash", "pid": 84, "job_id": "AJ_MoYmwCVPeWYj", "room_id": "RM_ogAu5EFYdvjN", "timestamp": "2025-11-19T074444.490452+00:00"} {"level": "WARNING", "name": "livekit.agents", "message": "failed to synthesize speech, retrying in 0.1s\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 553, in send_task\n await ws.send_str(json.dumps(config_msg))\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/client_ws.py\", line 245, in send_str\n await self._writer.send_frame(\n data.encode(\"utf-8\"), WSMsgType.TEXT, compress=compress\n )\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 83, in send_frame\n self._write_websocket_frame(message, opcode, 0)\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"/app/.venv/lib/python3.13/site-packages/aiohttp/_websocket/writer.py\", line 151, in _write_websocket_frame\n raise ClientConnectionResetError(\"Cannot write to closing transport\")\naiohttp.client_exceptions.ClientConnectionResetError: Cannot write to closing transport\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 621, in _run_ws\n await asyncio.gather(*tasks)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 571, in send_task\n raise APIConnectionError(f\"Send task failed: {e}\") from e\nlivekit.agents._exceptions.APIConnectionError: Send task failed: Cannot write to closing transport (body=None, retryable=True)\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 514, in _run\n await asyncio.gather(*tasks)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 507, in _process_segments\n await self._run_ws(word_stream, output_emitter)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 649, in _run_ws\n raise APIStatusError(f\"TTS WebSocket session failed: {e}\") from e\nlivekit.agents._exceptions.APIStatusError: TTS WebSocket session failed: Send task failed: Cannot write to closing transport (body=None, retryable=True) (status_code=-1, request_id=None, body=None, retryable=True)\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/app/.venv/lib/python3.13/site-packages/livekit/agents/tts/tts.py\", line 383, in _main_task\n await self._run(output_emitter)\n File \"/app/.venv/lib/python3.13/site-packages/livekit/plugins/sarvam/tts.py\", line 522, in _run\n raise APIConnectionError() from e\nlivekit.agents._exceptions.APIConnectionError: Connection error. (body=None, retryable=True)", "tts": "livekit.plugins.sarvam.tts.TTS", "attempt": 1, "streamed": true, "pid": 84, "job_id": "AJ_MoYmwCVPeWYj", "room_id": "RM_ogAu5EFYdvjN", "timestamp": "2025-11-19T074444.492712+00:00"}
t
The error "Cannot write to closing transport" usually happens when there’s an attempt to send data over a WebSocket or transport that’s already in the process of closing. I understand how frustrating this can be! This often occurs because of race conditions or how the lifecycle of your connection is managed in your code. Here are some helpful steps to guide you through resolving this: • Make sure all data transmissions are fully completed before you close the transport or WebSocket connection. • Keep an eye on the connection state and try to avoid sending data when it’s transitioning to closed or failed. • Use proper synchronization techniques to prevent race conditions when closing the transport. • Handle errors gently by retrying or stopping operations when the transport isn’t available. If you continue to see this error, it might be a good idea to dig a bit deeper into your application’s lifecycle management or how it integrates with LiveKit’s WebSocket handling. I’m here to help if you need more guidance! Sources: pkg/rtc/transport.go