Skip to content

Make PseudoRandomProvider thread-safe on targets older than net6.0 - #128

Merged
Seramis merged 1 commit into
ByteAether:mainfrom
Seramis:fix/pseudo-random-provider-thread-safety
Nov 3, 2026
Merged

Seramis merged 1 commit into
ByteAether:mainfrom
Seramis:fix/pseudo-random-provider-thread-safety

Conversation

@Seramis

@Seramis Seramis commented Nov 3, 2026

Copy link
Copy Markdown
Member

Description

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.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Other (please specify):

Checklist

  • The PR is submitted to the correct branch (main).
  • My code follows the project's coding style. (.editorconfig)
  • I have commented my code, particularly in hard-to-understand areas and public interfaces.
  • I have added or updated tests for the changes I made.
  • All new and existing tests passed.
  • I have updated the documentation where applicable.

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.
@Seramis Seramis self-assigned this Nov 3, 2026
@Seramis Seramis added bug Something isn't working pkg:Ulid ByteAether.Ulid .NET Pull requests that update .NET code labels Nov 3, 2026
@Seramis
Seramis merged commit 90d0857 into ByteAether:main Nov 3, 2026
16 checks passed
@Seramis
Seramis deleted the fix/pseudo-random-provider-thread-safety branch November 3, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working .NET Pull requests that update .NET code pkg:Ulid ByteAether.Ulid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant