Skip to content

Abort reading from stream when request has been aborted #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
retroplasma opened this issue Jul 22, 2019 · 7 comments
Closed

Abort reading from stream when request has been aborted #47

retroplasma opened this issue Jul 22, 2019 · 7 comments
Assignees
Labels

Comments

@retroplasma
Copy link

retroplasma commented Jul 22, 2019

Hi,

First of all, thanks for the wonderful project! There is a thing that I couldn't figure out yet:

I implemented an IFileSystem, IDocument and so forth. When GETting large files I noticed that if the request is aborted by the client, the server keeps reading data using OpenReadAsync from my subclass of Stream (the read method keeps being called). The cancellationToken doesn't seem to be triggered. Can we access HttpContext.RequestAborted somehow? Or is there another preferred way to react to client-side cancellation of a request?

I tested it by cURLing the server (curl http://localhost:5809/_dav/file.dat) and aborting using CTRL+C.

Thanks!

@fubar-coder
Copy link
Collaborator

That's a tough question. I'd check if this is a problem with the stream or if the cancellation token isn't triggered

@retroplasma
Copy link
Author

retroplasma commented Jul 22, 2019

@fubar-coder I put logs in the Stream's ReadAsync and Read function and checked the cancellation token from ReadAsync as well as the one from OpenReadAsync. The tokens are not triggered when the request is cancelled by the client.

Maybe this line is the cause?https://github.com/FubarDevelopment/WebDavServer/blob/master/src/FubarDev.WebDavServer/Handlers/Impl/GetResults/WebDavFullDocumentResult.cs#L73
(await content.CopyToAsync(response.Body).ConfigureAwait(false);)

Here's how I tested it:

Code

CustomFile.cs
public Task<Stream> OpenReadAsync(CancellationToken cancellationToken)
{
	Console.WriteLine(cancellationToken.IsCancellationRequested ? "wants to be cancelled" : "-");
	return Task.FromResult<Stream>(new CustomStream(/* ... */, cancellationToken));
}
CustomStream.cs
public override async Task<int> ReadAsync(byte[] buffer, int _offset, int count, CancellationToken cancellationToken)
{
	Console.WriteLine(cancellationToken.IsCancellationRequested ? "wants to be cancelled" : "-");
	Console.WriteLine(this.cancellationToken.IsCancellationRequested ? "wants to be cancelled" : "-");
	// ...
}

All three logs keep printing "-" after request cancellation.

Edit: Related CoreFX PR?

@fubar-coder
Copy link
Collaborator

Good catch! I will fix this.

@fubar-coder fubar-coder self-assigned this Jul 22, 2019
fubar-coder added a commit that referenced this issue Jul 22, 2019
Workaround for dotnet/corefx#9071
@retroplasma
Copy link
Author

@fubar-coder i ran the new code; unfortunately cancellation is still not triggered for me if the client cancels the request.

@fubar-coder
Copy link
Collaborator

Hmm, ok, will try again. I'm currently fighting the build server 😢

@retroplasma
Copy link
Author

Thanks for doing this! I'm looking around as well and just found this IIS issue, but I'm only using Kestrel and they fixed something like this here. I'm new to ASP though and not 100% sure if that's related.

@retroplasma
Copy link
Author

retroplasma commented Jul 22, 2019

Nice! I'll try it
edit: Awesome! Thanks a lot!

fubar-coder added a commit that referenced this issue Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants