Repository navigation
Description
Expected behavior
Until and including v1.12.0 I was able to download many files from OneDrive concurrently (see code snippet below) without any issues (and fast!).
Actual behavior
Since v1.13.0-beta (and incl. the now available v1.14.0) the download of multiple OneDrive files is slow from the beginning and is getting even slower over time. The app can only download up to ~15 files before the process eventually stops completely and the following exception is raised (no custom timeout set):
Code: generalExceptionMessage: An error occurred sending the request. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Exception: The text associated with this error code could not be found.The operation timed out at System.Net.Http.HttpHandlerToFilter.SendAsync(HttpRequestMessage request, CancellationToken cancel) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken) at Microsoft.Graph.RetryHandler.SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken) at Microsoft.Graph.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) --- End of inner exception stack trace --- at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption) at Microsoft.Graph.BaseRequest.SendStreamRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption) at Diarium.OneDriveHelper.DownloadFile(GraphServiceClient graphClient, String fileName)
Steps to reproduce the behavior
Microsoft.Graph v1.14.0
UWP app (but also happens in Xamarin)
var client = new GraphServiceClient("https://graph.microsoft.com/beta", new DelegateAuthenticationProvider(reqMsg =>
{
reqMsg.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
return Task.CompletedTask;
}));
SemaphoreSlim semaphore = new SemaphoreSlim(10);
if (filesToDownload.Count > 0)
{
var downloadTasks = new List<Task>();
var downloaded = 0; var error = false;
foreach (var file in filesToDownload)
{
await semaphore.WaitAsync();
if (error) { semaphore.Release(); break; }
downloadTasks.Add(Task.Run(async () =>
{
try { file.Data = await DownloadFile(graphClient, file.Name); }
catch { error = true; throw; }
finally { semaphore.Release(); }
}));
}
await Task.WhenAll(downloadTasks);
}
static async Task<byte[]> DownloadFile(GraphServiceClient graphClient, string fileName)
{
using (var response = await graphClient.Drive.Special.AppRoot.ItemWithPath(fileName).Content.Request().GetAsync())
{
return Decrypt(response);
}
}
Activity
MIchaelMainer commentedon Mar 26, 2019
Thank you for reporting this issue and providing repro steps.. We will investigate this.
Fixes issues #417 and #413
peombwa commentedon Mar 29, 2019
Timo Partl (@tipa), We have released a fix for this issue as part of release 1.14.1. Give it a try and let us know if it addresses your scenario.
tipa commentedon Mar 29, 2019
I am struggling including the project into my solution. Would it be possible to release it on Nuget.org as a preview package or make the NuGet file available to me as a direct download?
peombwa commentedon Mar 29, 2019
Timo Partl (@tipa)
Microsoft.Graph.Core1.14.1 package is available on nuget.org. Just update the core package to 1.14.1.tipa commentedon Mar 29, 2019
I installed both packages via Nuget.org now:

Unfortunately, the issue was not resolved and the same error occurs. It looks like the library is receiving the download location (public.by.files.1drv.com....), but then doesnt send the GET request to that URL:

darrelmiller commentedon Mar 29, 2019
My reading of that trace is that request 53-59 + 62-63 are the initial requests that redirect with a 302 and then 66,67,70,71,etc are the corresponding GETs to the redirect location with response payload and .200.
Is there a reason you believe this is not the case?
tipa commentedon Mar 30, 2019
No, this is absolutely correct. As I wrote in the first post, the first ~10-15 files are downloaded correctly. But you then see the requests in line 86 ff: They redirect to a different URL, but the library never sends a GET requests to them
darrelmiller commentedon Mar 30, 2019
Timo Partl (@tipa) My apologies, I missed that detail in your original post. I will continue to try to repro your issue. Could you try performing the multiple downloads by simply accumulating the tasks into an array and doing WaitAll and see if you still experience the problem. I'm assuming the semaphone/Task.Run/try/catch is a way of aborting all the tasks if one fails. If so you might be interested in this approach https://gist.github.com/svick/9992598 which uses a cancellation token to cancel all other tasks if one fails.
darrelmiller commentedon Mar 30, 2019
Timo Partl (@tipa) When just loading tasks into an array and doing WaitAll I am able to download 25 files without any issue or slowdown.

darrelmiller commentedon Mar 30, 2019
Timo Partl (@tipa) Do you have AppInsights enabled on your app? In the stacktrace I see a reference to DiagnosticsHandler and I suspect that might be coming from AppInsights.
tipa commentedon Mar 31, 2019
Tried that (I simply commented out the
await semaphore.WaitAsync()) but then not even a single file (out of 748 in my test case) could be downloaded.It might be worth noting that:
ServicePointManager.DefaultConnectionLimit = 20;to increase the amount of max. concurrent HTTP calls. However I played around with multiple values and also commented out the line, but the problem persisted10, sometimes the app does download more than 25 files, up to maybe 40 - but then ultimately stops.5, I was able to download way more files, but at a very slow pace. I stopped after 6 minutes with 160 files downloaded1(so no parallel downloads) it works better and I can download more files. However, even then the downloads slow down over time. After after 50 files it takes around 1.5 seconds for each filepublic.by.files.1drv.comI see this header:WWW-Authenticate: Bearer realm="OneDriveAPI", error="invalid_token", error_description="Invalid auth token"
The main reason for using the sema is to only download 10 files at a time. Sometimes I need to download multiple hundreds or even thousands of files which would lead to timeout errors very quickly if I requested them all at once. The try-catch is for the case a task fails and to not make any additional requests. I don't think the semaphore is the cause of the issue here because it worked fine with v1.1.12 and with other cloud storage libraries I implemented (Google Drive, Dropbox, ...)
I am using AppCenter in my app
MIchaelMainer commentedon Apr 1, 2019
Can you share with us a capture of using 1.14.1 getting redirected but not sending the subsequent GET requests? Can you show us the same capture using 1.12 with the same settings?
24 remaining items
tipa commentedon Apr 30, 2019
It works on UWP, but not on Xamarin Android. Haven't tested it on Xamarin.iOS.

I am using these settings for HttpClient on Android:
tipa commentedon May 2, 2019
Bug still present with v1.15.0 on Xamarin.Android & Xamarin.iOS
MIchaelMainer commentedon May 2, 2019
We are investigating this today.
tipa commentedon May 23, 2019
Any updates?
peombwa commentedon May 24, 2019
Timo Partl (@tipa) We internally use
HttpClientHandlerand I believe it's not handling parallel requests as expected on Xamarin.Android and Xamarin.iOS. Try adding the following lines when initializingGraphServiceClientin your Xamarin.Android project. It simply swaps our internalHttpClientHandlerwithAndroidClientHandler.Alternatively, you can clone this sample that I've setup and run it against your test data and see if it works for your scenario.
tipa commentedon May 24, 2019
That works for Android, but I am still having problems with iOS. My code looks like this now:
I am getting this exception:
I am getting a similar exception on Android, when I initialize the AndroidClientHandler like this:
var innerHandler = new Xamarin.Android.Net.AndroidClientHandler { AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip };peombwa commentedon May 24, 2019
Timo Partl (@tipa) Thanks for the detailed log!
The exception is thrown because both
GraphServiceClientandNSUrlSessionHandlerhandle Decompression out of the box andNSUrlSessionHandlerdoesn't provide an option to turn it off.For now, in your iOS preprocessor directive, you can remove our default
CompressionHandlerwhen wiring up theHttpProvideras such until we provide a fix for this:With this change, i'm able to download all files in parallel without an exception. Let us if it works for you.
tipa commentedon May 24, 2019
Yaaay, that made it work! Thanks a lot - finally I can use v1.15.0 in all 3 projects.
Any chance to integrate those workarounds into the library (or at least mention them in the docs). I am sure some other Xamarin devleopers are using this lib as well.
peombwa commentedon May 24, 2019
Timo Partl (@tipa) Awesome, I'm glad it worked!
Yes, we'll make these changes to our library and I've added them to our backlog via issues #481 and #482 .
I'll go ahead and close this issue since we now have a workaround/fix for it. Thanks for the feedback in resolving this issue.
tipa commentedon Mar 23, 2020
Peter Ombwa (@peombwa) Darrel (@darrelmiller) I started buidling a Xamarin.macOS app recently and now experience this issue again. Would it be possible to also use NSUrlSession by default for macOS? Right only the native handlers are used for Android and iOS only: https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/blob/ada45005494a1e274a3a7db5a167ab3e0bc3940a/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs#L217
darrelmiller commentedon Mar 23, 2020
Timo Partl (@tipa) That seems like a reasonable request. I'm guessing it would require adding in an extra platform check. If you had the time to make a PR to add this, I can get it merged. If not, I'll try and get a dev to make the fix before our next release. Unfortunately, most of our devs are tied up with other priorities at the moment.
tipa commentedon Mar 23, 2020
I created this pull request here: microsoftgraph/msgraph-sdk-dotnet-core#99
Hope everything is correct, it's only my second ever PR - apparently some check failed, not sure if it's my fault...
darrelmiller commentedon Mar 24, 2020
Timo Partl (@tipa) Thank you. We will investigate the issue.