You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The line var responseMessage = httpClient.SendAsync(requestMessage, CancellationToken.None).WaitForTask();
in the Upload method of the StandardFileUploader has the potential to cause a deadlock.
In particular, the WaitForTask method blocks on Task objects.
Blocking on Task objects (Via Task.Wait() or Task.Result or Task.GetAwaiter().GetResult())
is dangerous and could result in deadlocks.
(See, for example http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html)
Here is some client code to trigger this deadlock if invoked from a GUI context (such as a button-click handler) or from an MVC context.
var sfClient = new ShareFileClient("https://secure.sf-api.com/sf/v3/");
var parentFolder = (ShareFile.Api.Models.Folder) sfClient.Items.Get().Execute();
var file = File.Open(@"C:\test\image.png", FileMode.OpenOrCreate);
var uploadRequest = new UploadSpecificationRequest
{
FileName = file.Name,
FileSize = file.Length,
Details = "Sample details",
Parent = parentFolder.url,
Method = ShareFile.Api.Models.UploadMethod.Standard
};
var uploader = sfClient.GetFileUploader(uploadRequest, new PlatformFileStream(file, file.Length, file.Name));
var uploadResponse = uploader.Upload();
Other call sites that use WaitForTask are likely to be susceptible too.
Note that the code above won't deadlock a console app, or if called from a unit test. You need to invoke it from the main thread of a GUI app/MVC app.
I shall be happy to supply further details in the case that this report is not clear.
The text was updated successfully, but these errors were encountered:
Hi @anirudhsanthiar thanks for the report. Our synchronous APIs are currently only consumed from console based apps, all other GUI apps end up using our Async APIs, i.e. sfClient.GetAsyncFileUploader.
Are you using this in a .net 4.0 context where you're limited to sync only APIs?
Hi @rgmills...thanks for the acknowledgment. It would be great if you could add a note to the documentation clarifying that GUI apps are only meant to consume the async APIs. Another possible fix is to configure the await statements in ShareFile to not capture context via ConfigureAwait(false) [see, for example, the section under "avoiding context" in 1]
The line
var responseMessage = httpClient.SendAsync(requestMessage, CancellationToken.None).WaitForTask();
in the
Upload
method of theStandardFileUploader
has the potential to cause a deadlock.In particular, the
WaitForTask
method blocks on Task objects.Blocking on Task objects (Via Task.Wait() or Task.Result or Task.GetAwaiter().GetResult())
is dangerous and could result in deadlocks.
(See, for example http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html)
Here is some client code to trigger this deadlock if invoked from a GUI context (such as a button-click handler) or from an MVC context.
Other call sites that use
WaitForTask
are likely to be susceptible too.Note that the code above won't deadlock a console app, or if called from a unit test. You need to invoke it from the main thread of a GUI app/MVC app.
I shall be happy to supply further details in the case that this report is not clear.
The text was updated successfully, but these errors were encountered: