clockworklabs/SpacetimeDB · error · ArgumentException
randomBytes must be exactly 4 bytes
Error message
randomBytes must be exactly 4 bytes
What it means
Uuid.FromCounterV7 assembles a time-ordered v7 UUID from a 48-bit millisecond timestamp, a 31-bit monotonic counter, and exactly 4 caller-supplied random bytes that fill the remaining random bits. A randomness span of any other length is rejected with ArgumentException before anything is generated.
Solutions
- Provide exactly 4 random bytes: RandomNumberGenerator.GetBytes(4)
- If your helper returns a fixed buffer, slice it: rng[..4]
- Keep separate creation helpers per UUID version so each size expectation is explicit
Example fix
// before var rand = RandomNumberGenerator.GetBytes(16); var id = Uuid.FromCounterV7(ref counter, now, rand); // ArgumentException: randomBytes must be exactly 4 bytes // after var rand = RandomNumberGenerator.GetBytes(4); var id = Uuid.FromCounterV7(ref counter, now, rand);
Defensive patterns
Strategy: validation
Validate before calling
static bool IsUuidV7RandomBytes(ReadOnlySpan<byte> b) => b.Length == 4;
Prevention
- Allocate exactly 4 bytes for v7 randomness (v4 needs 16 — do not share buffers)
- Wrap FromCounterV7 in a single factory method that owns RNG sizing
- Add unit tests asserting the helper's buffer width
When it happens
Trigger: Passing a random span with length != 4 — commonly a 16-byte buffer reused from v4 code paths, or an empty span when randomness was not plumbed in.
Common situations: Refactoring shared RNG buffers across UUID versions (v4 wants 16 bytes, v7 wants 4); copying sample code without adjusting the size; test stubs sized for a different version.
Related errors
- Must be 16 bytes
- timestamp before unix epoch
- uuid counter must be non-negative
- Argument must be a Uuid
- Byte array must be exactly
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/a0f357c2f3f9756f.
Report an issue: GitHub.
Appendix: source
Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs:127
/// </param>
/// <exception cref="InvalidOperationException">
/// Thrown if the counter value is negative.
/// </exception>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="randomBytes"/> is not exactly 4 bytes long, or <paramref name="now"/> is before unix epoch.
/// </exception>
/// <returns>
/// A <see cref="Uuid"/> `v7`.
/// </returns>
public static Uuid FromCounterV7(
ref int counter,
Timestamp now,
ReadOnlySpan<byte> randomBytes // must be length 4
)
{
if (randomBytes.Length != 4)
{
throw new ArgumentException("randomBytes must be exactly 4 bytes", nameof(randomBytes));
}
if (counter < 0)
{
throw new InvalidOperationException("uuid counter must be non-negative");
}
if (now.MicrosecondsSinceUnixEpoch < 0)
{
throw new ArgumentException("timestamp before unix epoch", nameof(now));
}
var unixTsMs = now.MicrosecondsSinceUnixEpoch / 1_000;
// monotonic 31-bit
var counterVal = counter;
counter = (counter + 1) & 0x7FFF_FFFF;
Span<byte> bytes = stackalloc byte[16];View on GitHub (pinned to 6dee26c6ef)