aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
963 stars 332 forks source link

Do not call End/Abort on disconnect #432

Closed Tratcher closed 2 years ago

Tratcher commented 2 years ago

Fixes #430

Http.Sys always fires the connection disconnect callback even if requests complete successfully. There's a race where the disconnect can fire during the write call that finishes the response. For HTTP/2 this callback is per-request, not per connection, making the race more likely to be observable.

Katana's old logic would end up Aborting the response whenever the disconnect fired. This could cause the final WriteAsync call to throw even if it succeeded.

The new logic will cancel the request CTS without calling Abort. Abort is only called if exceptions are thrown from the application. This matches the behavior of HttpSysServer in ASP.NET Core. This does not get rid of the race condition for the disconnect callback, it only mitigates the write error. Passing the the token to the WriteAsync operation could still cause it to fail.

Testing: