fix: ChatController naming consistency and streaming cancellation/error handling#1089
Conversation
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/c4364690-d679-41d6-a271-69d6c45343af Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates ChatController to improve naming consistency and to better align streaming response writes and exception handling with request cancellation behavior for the chat streaming endpoint.
Changes:
- Renames the
AIChatServicefield from_AiChatServiceto_AIChatServiceand updates usages. - Updates
StreamMessageto use the requestcancellationToken(instead ofCancellationToken.None) when writing JSON/SSE error responses. - Simplifies cancellation and consolidates duplicated
ConversationContextLimitExceededExceptionhandling based on whether the response has started.
Comments suppressed due to low confidence (1)
EssentialCSharp.Web/Controllers/ChatController.cs:170
catch (Exception ex) when (!Response.HasStarted)writes a JSON 500 body usingcancellationTokenwithout any protection. If the exception was triggered by a client disconnect / abort (common with streaming endpoints), this write can throwOperationCanceledException(or other I/O exceptions) from inside the catch and bypass your error handling. Consider guarding withif (cancellationToken.IsCancellationRequested) return;or wrapping the write in try/catch for cancellation/I/O.
}
catch (Exception ex) when (!Response.HasStarted)
{
LogChatStreamErrorBeforeResponseStarted(_Logger, ex, User.Identity?.Name);
Response.StatusCode = 500;
Response.ContentType = "application/json";
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/ca7b0afb-1731-4a50-ac2e-b4d7152c972a Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/ca7b0afb-1731-4a50-ac2e-b4d7152c972a Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/c0627de3-aa52-4d77-a845-1420ecbe2416 Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot Sommeone else just commented this: EssentialCSharp.Web/Program.cs:115-133, 442-449 + EssentialCSharp.Web/appsettings.json:5-6: clearing KnownIPNetworks/KnownProxies while shipping TrustedProxyCidrs: [] means forwarded headers are accepted from any source. That re-opens X-Forwarded-For spoofing and can bypass IP-based rate limits unless every deployment overrides this. Do we have docs to prove our side? |
I couldn’t find Microsoft docs that support clearing |
Move forwarded headers proxy/CIDR parsing into service extensions and enforce fail-closed behavior outside Development when trusted proxies are not configured. Add ForwardedHeaders config section with TrustedProxyCidrs/TrustedProxies defaults.
Handle request-abort cancellations explicitly while rethrowing non-request OperationCanceledException cases. Also harden pre-start context-limit error writes to avoid inheriting an already-canceled token for best-effort JSON responses.
/home/runner/work/EssentialCSharp.Web/EssentialCSharp.Web/EssentialCSharp.Web/Controllers/ChatController.csStreamMessagecancellation/error handling to address the active PR comments and fix the compile errorOriginal prompt