Skip to content
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

Issue: The DownloadFileAsync method will throw an OutOfMemoryException if the file is too large #342

Conversation

pvgritsenko-ansible
Copy link
Contributor

@pvgritsenko-ansible pvgritsenko-ansible commented Apr 19, 2024

I found an issue when we use the DownloadFileAsync method. Since it uses IRequest.AsStream(), this method invokes HttpClient.SendAsync(request, cancellationtoken) and than HttpResponseMessage.Content.ReadAsStreamAsync() for the response message.
The HttpResponseMessage.Content.ReadAsStreamAsync() read whole content into the memory buffer and makes the stream from result buffer.

In our project we encountered with following issue: if we receive a large enough file, buffer can overflow.

But I also found (here Streaming Large HTTP Responses) if we call HttpClient.SendAsync with HttpCompletionOption.ResponseHeadersRead flag it just return stream without buffering and we can handle received stream as we want.

So I suggest to use old implementation of DownloadFileAsync in new method, but with HttpCompletionOption.ResponseHeadersRead flag.

May we also need to create issue in Pathoschild repository.

@Jericho Jericho added the Bug This change resolves a defect label Apr 19, 2024
@Jericho Jericho added this to the 0.76.0 milestone Apr 19, 2024
Copy link
Owner

@Jericho Jericho left a comment

Choose a reason for hiding this comment

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

Looks good but do we really need DownloadFile and DownloadFileWithoutBuffering? Why not have a single method and download all files with streaming enabled.

@@ -372,6 +367,25 @@ public Task<Stream> DownloadFileAsync(string downloadUrl, CancellationToken canc
.AsStream();
}

/// <inheritdoc/>
public async Task<Stream> DownloadFileWithouBufferingAsync(string downloadUrl, CancellationToken cancellationToken)
Copy link
Owner

Choose a reason for hiding this comment

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

Can you think of a downside to downloading a file without buffering? I can't think of one. Which leads me to think that all files, large or small, should probably be downloaded without buffering. Which means there's no need for a separate DownloadFileWithouBufferingAsync method and you can move the logic to the DownloadFileAsync method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From this point of view, then you are right =)
I have removed the method separating.

Source/ZoomNet/Resources/CloudRecordings.cs Show resolved Hide resolved
Copy link
Owner

@Jericho Jericho left a comment

Choose a reason for hiding this comment

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

Looks good to me!
Thank you for your contribution.

@Jericho Jericho merged commit 9389bc5 into Jericho:develop Apr 22, 2024
1 of 2 checks passed
@Jericho
Copy link
Owner

Jericho commented Apr 27, 2024

🎉 This issue has been resolved in version 0.76.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This change resolves a defect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants