Skip to content

Commit

Permalink
Fix threading issue in HttpClientManager
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Apr 1, 2019
1 parent f577fa0 commit 00c070a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
namespace Microsoft.Identity.Client
{
/// <summary>
/// Http Client Factory
/// Factory responsible for creating HttpClient
/// .Net recommends to use a single instance of HttpClient
/// </summary>
/// <remarks>
/// Implementations must be thread safe. Consider creating and configuring an HttpClient in the constructor
/// of the factory, and returning the same object in <see cref="GetHttpClient"/>
/// </remarks>
public interface IMsalHttpClientFactory
{
/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Microsoft.Identity.Client/Http/HttpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@

namespace Microsoft.Identity.Client.Http
{
/// <remarks>
/// We invoke this class from different threads and they all use the same HttpClient.
/// To prevent race conditions, make sure you do not get / set anything on HttpClient itself,
/// instead rely on HttpRequest objects which are thread specific.
///
/// In particular, do not change any properties on HttpClient such as BaseAddress, buffer sizes and Timeout. You should
/// also not access DefaultRequestHeaders because the getters are not thread safe (use HttpRequestMessage.Headers instead).
/// </remarks>
internal class HttpManager : IHttpManager
{
private readonly IMsalHttpClientFactory _httpClientFactory;
Expand Down Expand Up @@ -210,7 +218,7 @@ private async Task<HttpResponse> ExecuteAsync(
await client.SendAsync(requestMessage).ConfigureAwait(false))
{
HttpResponse returnValue = await CreateResponseAsync(responseMessage).ConfigureAwait(false);
returnValue.UserAgent = client.DefaultRequestHeaders.UserAgent.ToString();
returnValue.UserAgent = requestMessage.Headers.UserAgent.ToString();
return returnValue;
}
}
Expand Down

0 comments on commit 00c070a

Please sign in to comment.