openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI/AbsoluteLatest
MIT License
722 stars 62 forks source link

Fix non-thread-safe use of Random in MultipartFormDataBinaryContent #36

Closed stephentoub closed 2 weeks ago

stephentoub commented 3 weeks ago

MultipartFormDataBinaryContent has a static Random instance that's being used without a lock. This locks around each access (an alternative would be to use and lazily-initialize on each access a [ThreadStatic] Random).

While fixing that, also improved a few things:

KrzysztofCwalina commented 3 weeks ago

cc: @annelo-msft

annelo-msft commented 3 weeks ago

Adding some context regarding why Random needs to be used in a thread-safe manner: https://devblogs.microsoft.com/pfxteam/getting-random-numbers-in-a-thread-safe-way/