Skip to content

fix: handle graceful cancellation in CloudEventsMiddleware to prevent 499 errors#1713

Merged
WhitWaldo merged 2 commits intodapr:masterfrom
ali-Hamza817:fix/pubsub-cancellation-1237
Feb 23, 2026
Merged

fix: handle graceful cancellation in CloudEventsMiddleware to prevent 499 errors#1713
WhitWaldo merged 2 commits intodapr:masterfrom
ali-Hamza817:fix/pubsub-cancellation-1237

Conversation

@ali-Hamza817
Copy link
Contributor

Description

This PR addresses the "499 Client Closed Connection" error in Pub/Sub consumers by properly propagating the CancellationToken from the HttpContext to the JsonSerializer. It also implements a graceful catch for OperationCanceledException that swallows the error ONLY if the response has already started, preventing erroneous telemetry failures during high-load race conditions.

Issue reference

Fixes #1237

Checklist

  • Code compiles correctly
  • Created/updated tests

@ali-Hamza817 ali-Hamza817 requested review from a team as code owners February 19, 2026 06:21
@ali-Hamza817 ali-Hamza817 force-pushed the fix/pubsub-cancellation-1237 branch from 951b0f3 to a70c99e Compare February 19, 2026 06:25
@WhitWaldo
Copy link
Contributor

Thank you for your contribution here! No notes - this looks great.

That said, I cannot merge this until you've signed your commits. Once you do that, I'd be more than happy to merge as-is. Again, thank you!

… 499 errors

Propagate CancellationToken to JsonSerializer and handle OperationCanceledException
gracefully when the response has already started. This prevents erroneous 499
Client Closed Connection errors in PubSub consumers under high load.

Fixes dapr#1237

Signed-off-by: ali-Hamza817 <alihamzaminhas.21@gmail.com>
- Fix test ordering: move app.Run() before app.Build() to match
  the pattern of all other tests in the file
- Use IHttpResponseFeature to properly simulate HasStarted=true
  since DefaultHttpContext doesn't set it from WriteAsync
- Add new test: PropagatesCancellation_WhenResponseHasNotStarted
  to verify exceptions are NOT swallowed when the response has
  not started (correct ASP.NET Core behavior)

Signed-off-by: ali-Hamza817 <alihamzaminhas.21@gmail.com>
@ali-Hamza817 ali-Hamza817 force-pushed the fix/pubsub-cancellation-1237 branch from 595c9b4 to 75cb668 Compare February 23, 2026 05:00
@WhitWaldo WhitWaldo merged commit 35e04a2 into dapr:master Feb 23, 2026
413 of 417 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"499 client closed connection" on PubSub consumer after upgrading to .NET 8

2 participants