Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Async/Await issue #54

Closed
agnerbo opened this issue Jul 2, 2019 · 5 comments
Closed

Async/Await issue #54

agnerbo opened this issue Jul 2, 2019 · 5 comments

Comments

@agnerbo
Copy link

agnerbo commented Jul 2, 2019

Describe the bug
When using GetOrder the task never retruns. It can be fixed by adding .ConfigureAwait(false) when using async.

Async/Await - Best Practices in Asynchronous Programming
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

Which service do you use
Every call to klarna

To Reproduce
var klarnaEnv = isTest ? KlarnaEnvironment.TestingEurope : KlarnaEnvironment.LiveEurope;
var klarnaApi = new Klarna.Rest.Core.Klarna(username, password, klarnaEnv);
var task = klarnaApi.OrderManagement.GetOrder(klarnaOrderId);
task.Wait(TimeSpan.FromMinutes(1));

Expected behavior
Wait to terminate when order has been recieved

Additional context

@igeligel
Copy link

igeligel commented Jul 2, 2019

I think you did run into a deadlock situation. You can read more about it here: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Since you are working currently with this version of the Klarna SDK you should pass down the async context of your application the whole way down as mentioned in the article:

Don’t block on Tasks; use async all the way down.

The other option is:

In your “library” async methods, use ConfigureAwait(false) wherever possible.

which is on us. So we would need to create a new release, but we are happy to receive a PR including the changes. There is also another blog article explaining the ConfigureAwait in detail: https://medium.com/bynder-tech/c-why-you-should-use-configureawait-false-in-your-library-code-d7837dce3d7f


This is the code as it is right now in Klarna.Rest.Core/Store/OrderManagementStore.cs

public async Task<OrderManagementOrder> GetOrder(string orderId)
{
    var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}");
    var response = await Get<OrderManagementOrder>(url);
    return response;
}

But it should look like this:

public async Task<OrderManagementOrder> GetOrder(string orderId)
{
    var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}");
    return await Get<OrderManagementOrder>(url).ConfigureAwait(false);
}

@alexions
Copy link
Contributor

alexions commented Aug 5, 2019

The fix is coming: #55

@alexions
Copy link
Contributor

alexions commented Aug 6, 2019

@igeligel added as a reviewer

@alexions
Copy link
Contributor

alexions commented Aug 8, 2019

Merged. The changes will appear in the next release.

@alexions
Copy link
Contributor

alexions commented Aug 9, 2019

@alexions alexions closed this as completed Aug 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants