Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not recreate Random each time #945

Merged
merged 4 commits into from Apr 15, 2024

Conversation

watfordsuzy
Copy link
Contributor

On .NET Framework it is incorrect to recreate Random instances each time, these should instead be one per instance. On .NET Core and later this is not a limitation, but the libraries share a codebase.

Fixes #944

On .NET Framework it is incorrect to recreate Random instances each time, these should instead be one per instance. On .NET Core and later this is not a limitation, but the libraries share a codebase.

Fixes box#944
@mwwoda
Copy link
Contributor

mwwoda commented Apr 10, 2024

Hi @watfordsuzy
Thanks for the contribution! I think it's a good practice in general reuse the Random instance so we could do it for both of the SDKs (Framework and Core). I think thread safety could be a potential issue there (e.g. in chunked file uploads) https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-random#thread-safety. What do you think?

@watfordsuzy
Copy link
Contributor Author

For .NET Core and beyond you don't strictly need to re-use it (but should) as the underlying implementation already gives you a Thread Safe implementation:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Random.cs

On .NET Framework a thread local Random could be used instead, but there are some interesting caveats if you go and read the comments regarding ThreadSafeRandom used in .NET Core+.

@mwwoda
Copy link
Contributor

mwwoda commented Apr 11, 2024

There is also an explicit thread safe implementation for Random https://learn.microsoft.com/en-us/dotnet/api/system.random.shared?view=net-8.0, but only for .NET 6+ and we need to support older versions too.

If you want to work on this further, I think a thread-safe solution is the way to go. The overhead of potential locks shouldn't be a big deal, since this feature is already used to delay subsequent api calls, so that would just mean a slight bigger delay. While arguably making it not thread-safe isn't such a big deal either, it might result in slightly different behavior in multi-threaded environment compared to the single-threaded one. If necessary it's also possible to provide two implementation for both SDKs with pragmas. See example

@watfordsuzy
Copy link
Contributor Author

I've gone ahead and made the suggested change, adapting their internal ThreadSafeRandom to fit the existing code. I've also added a section for .NET 6+, delegating to the inboxed thread safe random implementation.

@watfordsuzy
Copy link
Contributor Author

I don't believe the Integration Test failures are related @mwwoda

@mwwoda
Copy link
Contributor

mwwoda commented Apr 11, 2024

Don't worry about the integration tests. Unfortunately, we cannot currently run them on forks.

@mwwoda
Copy link
Contributor

mwwoda commented Apr 15, 2024

The code looks pretty good. I see that you have introduced a modern implementation for .NET6+ too which is great. Unfortunately, I just checked that when you use a conditional compilation flag such as NET6_0_OR_GREATER when not explicitly targeting this version in the library, this code will be never used during compilation (so it is basically dead code at this point). We would have to explicitly target .NET 6 version, which I don't think we will tackle in the near future. Maybe you could remove implementation for .NET 6+, and then we can merge your change. Sorry for the confusion.

@watfordsuzy
Copy link
Contributor Author

@mwwoda no worries!

Copy link
Contributor

@mwwoda mwwoda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Will be included in the next release of the SDK.

@mwwoda mwwoda merged commit d03b1ce into box:main Apr 15, 2024
6 of 7 checks passed
@watfordsuzy watfordsuzy deleted the issue-944-recreating-random branch April 15, 2024 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ExponentialBackoff strategy recreates Random instance each time
2 participants