coryrwest / B2.NET

.NET library for Backblaze's B2 Cloud Storage
MIT License
62 stars 33 forks source link

Made HttpClient initialization more thread-safeish #38

Closed swijnands closed 4 years ago

swijnands commented 4 years ago

While doing some multi-threaded batch processing using B2.NET I would sporadically get an Duplicate Key exception from the underlying System.Net.HttpClient during startup.

I traced it back to how the singleton HttpClient is being constructed. If the stairs align just right it is currently possible that two threads try to add the system Accept request header at the same time (resulting in the previously mentioned exception).

I have added a simple fix in this pull request by privately creating and configuring the HttpClient before sharing it with the rest of the AppDomain.