Repository navigation
Make PseudoRandomProvider thread-safe on targets older than net6.0 - #128
Merged
Seramis merged 1 commit intoNov 3, 2026
Merged
Conversation
On `netstandard2.0`, `netstandard2.1` and `net5.0` every call shared one static `System.Random`, which is not thread-safe, so concurrent use corrupted its state and produced duplicate values. Each thread now has its own `System.Random` instance, seeded from `CryptographicallySecureRandomProvider` so that threads started within the same clock tick cannot share a seed. On `netstandard2.0`, where `Random.NextBytes` has no `Span<byte>` overload, bytes are taken from `Random.Next()` three at a time. Adds `PseudoRandomProviderTests`, including a regression test that generates ULIDs with a fixed timestamp from eight threads at once and asserts that they are all distinct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On
netstandard2.0,netstandard2.1andnet5.0every call shared one staticSystem.Random, which is not thread-safe, so concurrent use corrupted its state and produced duplicate values. Each thread now has its ownSystem.Randominstance, seeded fromCryptographicallySecureRandomProviderso that threads started within the same clock tick cannot share a seed. Onnetstandard2.0, whereRandom.NextByteshas noSpan<byte>overload, bytes are taken fromRandom.Next()three at a time.Adds
PseudoRandomProviderTests, including a regression test that generates ULIDs with a fixed timestamp from eight threads at once and asserts that they are all distinct.Type of Change
Checklist
main)..editorconfig)