From b2a68a467c570a0424ac2b21b8a3ec63e4b35f16 Mon Sep 17 00:00:00 2001 From: cthomas Date: Tue, 19 Aug 2025 16:02:04 -0700 Subject: [PATCH] feat: add resource closed errors throughout stream (#4021) --- letta/server/rest_api/streaming_response.py | 60 +++++++++++++-------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/letta/server/rest_api/streaming_response.py b/letta/server/rest_api/streaming_response.py index 361facff..a8d0f245 100644 --- a/letta/server/rest_api/streaming_response.py +++ b/letta/server/rest_api/streaming_response.py @@ -273,13 +273,17 @@ class StreamingResponseWithStatusCode(StreamingResponse): } ) raise - await send( - { - "type": "http.response.body", - "body": cancellation_event, - "more_body": more_body, - } - ) + if self._client_connected: + try: + await send( + { + "type": "http.response.body", + "body": cancellation_event, + "more_body": more_body, + } + ) + except anyio.ClosedResourceError: + self._client_connected = False return # Handle client timeouts (should throw error to inform user) @@ -298,13 +302,17 @@ class StreamingResponseWithStatusCode(StreamingResponse): } ) raise - await send( - { - "type": "http.response.body", - "body": error_event, - "more_body": more_body, - } - ) + if self._client_connected: + try: + await send( + { + "type": "http.response.body", + "body": error_event, + "more_body": more_body, + } + ) + except anyio.ClosedResourceError: + self._client_connected = False capture_sentry_exception(exc) return @@ -323,14 +331,22 @@ class StreamingResponseWithStatusCode(StreamingResponse): } ) raise - await send( - { - "type": "http.response.body", - "body": error_event, - "more_body": more_body, - } - ) + if self._client_connected: + try: + await send( + { + "type": "http.response.body", + "body": error_event, + "more_body": more_body, + } + ) + except anyio.ClosedResourceError: + self._client_connected = False + capture_sentry_exception(exc) return if more_body and self._client_connected: - await send({"type": "http.response.body", "body": b"", "more_body": False}) + try: + await send({"type": "http.response.body", "body": b"", "more_body": False}) + except anyio.ClosedResourceError: + self._client_connected = False