Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Issue #23141 Fix ManagedHandler cancellation Tests #25511

Closed
wants to merge 1 commit into from

Conversation

WinCPP
Copy link

@WinCPP WinCPP commented Nov 26, 2017

An attempt to solve #23141. There are two parts to the PR.

  1. Improving the cancellation support in ManagedHandler: this employs handling the cancellation via the CancellationToken while allowing the operation to still run in background (as discussed in this comment). The idea is based on the blog article here :-)

  2. Even with the above 'by-pass' fix, the test 'ReadAsStreamAsync_ReadAsync_Cancel_TaskCanceledQuickly' still hanged while doing ReadAsync from the stream. In case of ManagedHandler the response stream is of type ContentLengthReadStream which needed fix on the lines of cancelable functionality in WinHttpResponseStream (here)

I am still trying to find out how to close / dispose the HttpConnection or the stream instead of allowing the operations to run in the background, consequences of which I am not very sure of.

@stephentoub, I have created this PR as we discussed here. Appreciate your inputs.

Thanks,
Mandar

@WinCPP WinCPP changed the title Issue #23141 Fix ManagedHandler cancellation Tests WIP Issue #23141 Fix ManagedHandler cancellation Tests Nov 26, 2017
@davidsh davidsh requested a review from stephentoub November 26, 2017 22:18
@stephentoub
Copy link
Member

stephentoub commented Nov 27, 2017

Improving the cancellation support in ManagedHandler: this employs handling the cancellation via the CancellationToken while allowing the operation to still run in background (as discussed in this comment). The idea is based on the blog article here :-)

I know I wrote about that as being an approach to addressing this kind of issue, but I don't believe it's an appropriate approach here; as core library, we should not be leaving async operations still in flight once the Task that's been returned to represent it is marked as canceled. As I'd mentioned previously, the right approach to addressing cancellation of reads/writes in the managed handler is to support cancellation of sends/receives on the underlying socket operations being used, and simply passing the token to those operations, as is already being done.

@WinCPP
Copy link
Author

WinCPP commented Nov 27, 2017

Yup. I agree. I'm working on finding solution to cancelling read / writes... I hope I have time...

@WinCPP WinCPP force-pushed the managed_handler_23141 branch from a39b11b to 8ff76c2 Compare November 28, 2017 17:47
@WinCPP WinCPP changed the title WIP Issue #23141 Fix ManagedHandler cancellation Tests Issue #23141 Fix ManagedHandler cancellation Tests Nov 28, 2017
@WinCPP
Copy link
Author

WinCPP commented Nov 28, 2017

@stephentoub I have re-implemented the code. Appreciate your review comments. Thanks.

@@ -308,7 +308,7 @@ private Task WriteFormattedInt32Async(int value, CancellationToken cancellationT
}

// Parse the response status line.
var response = new HttpResponseMessage() { RequestMessage = request, Content = new HttpConnectionContent(CancellationToken.None) };
var response = new HttpResponseMessage() { RequestMessage = request, Content = new HttpConnectionContent(cancellationToken) };
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to cancellationToken in place of CancellationToken.None because the token passed to ReadNextLineAsync is not further passed down into its call chain and hence there is no way to dispose the underlying streams to trip the operations via an exception.

@WinCPP
Copy link
Author

WinCPP commented Nov 29, 2017

@dotnet-bot retest Linux x64 Release Build please...

@stephentoub
Copy link
Member

@WinCPP, thank you, but again, I don't think this is the right approach. To be very clear, I believe the right approach is primarily to implement https://github.com/dotnet/corefx/issues/24430, and then https://github.com/dotnet/corefx/issues/23141 will effectively come for free.

@WinCPP
Copy link
Author

WinCPP commented Nov 30, 2017

@stephentoub thank you for your feedback. I understand. I thought the idea was to have it just like WinHTTP* wrappers. hmmm #24430 is not up-for-grabs, unfortunately. Can I try working on it and then on this one?

@stephentoub
Copy link
Member

@WinCPP, thanks, you're welcome to try. It's not an easy issue, though. Windows might not be too hard; it'll likely involve P/Invoking to CancelIoEx. On Unix, it'll likely involve changes in the bowels of SocketAsyncContext. For both cases, we need to be sure we're not adding significant cost, in particular when a non-cancelable token is passed in, only incurring additional cost (and as little as possible) when the provided token can be canceled.

@stephentoub
Copy link
Member

In the meantime, I'm going to close this PR. Thank you for your efforts.

@karelz karelz added this to the 2.1.0 milestone Dec 4, 2017
@WinCPP WinCPP deleted the managed_handler_23141 branch March 8, 2018 17:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants