SocketException: Reading from a closed socket crash#4526
SocketException: Reading from a closed socket crash#4526
Conversation
… crash Return a synthetic 503 StreamedResponse when a SocketException occurs in sendStreaming instead of letting the exception propagate as a fatal crash. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a crash caused by an unhandled SocketException in sendStreaming by catching the exception and returning a synthetic 503 response. This is a good approach to gracefully handle network interruptions during streaming requests. My review includes one suggestion to improve observability by logging the caught exception, which is important for monitoring since these errors will no longer appear as crashes.
| }) async { | ||
| try { | ||
| return await _client.send(request).timeout(timeout); | ||
| } on SocketException { |
There was a problem hiding this comment.
While this change correctly prevents the crash, it silently swallows the SocketException. This means you will lose visibility into these network errors, which were previously reported as crashes. It would be beneficial to log the exception to aid in future debugging and monitoring.
First, capture the exception object. Then, you can log it using a suitable logging utility from your project (e.g., DebugLogManager.logWarning).
on SocketException catch (e, stackTrace) {
// You may need to import your logging utility
DebugLogManager.logWarning('send_streaming_socket_error', {
'error': e.toString(),
'stackTrace': stackTrace.toString(),
'request_url': request.url.toString(),
});
// ... rest of the block
}| } on SocketException { | |
| } on SocketException catch (e, stackTrace) { |
Summary
sendStreamingwhen aSocketException("Reading from a closed socket") is thrownStreamedResponseinstead of letting the exception propagate as a fatal crashCrash Stats
Test plan
🤖 Generated with Claude Code