clockworklabs/SpacetimeDB · error · InvalidOperationException
uuid counter must be non-negative
Error message
uuid counter must be non-negative
What it means
FromCounterV7 takes a caller-owned ref counter that it masks into an unsigned 31-bit field each call, incrementing it as (counter + 1) & 0x7FFF_FFFF. The counter must therefore be non-negative on entry; a negative value throws InvalidOperationException before any UUID is produced.
Solutions
- Initialize the counter to 0 and let FromCounterV7 be the only code that advances it
- Remove any logic that assigns or decrements the counter below zero
- Track 'not yet used' state with a separate bool flag instead of -1
Example fix
// before int counter = -1; // 'uninitialized' sentinel var id = Uuid.FromCounterV7(ref counter, now, rand); // InvalidOperationException: uuid counter must be non-negative // after int counter = 0; bool used = false; // separate flag for 'not started' var id = Uuid.FromCounterV7(ref counter, now, rand);
Defensive patterns
Strategy: validation
Validate before calling
// Reset or reject before generating if (counter < 0) counter = 0; // or throw your own domain error
Prevention
- Initialize the counter to 0 and never write to it outside FromCounterV7
- Use a separate bool flag for 'not started' instead of -1
- Assert counter >= 0 in debug builds
When it happens
Trigger: Calling FromCounterV7 with a counter initialized to -1 (a common 'not started' sentinel) or one that application logic decremented below zero.
Common situations: Using -1 as an uninitialized marker; sharing the counter variable with code that decrements it; porting sequence generators that start at -1.
Related errors
- randomBytes must be exactly 4 bytes
- timestamp before unix epoch
- Argument must be a Uuid
- Must be 16 bytes
- Unknown UUID version
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/b333235366684efa.
Report an issue: GitHub.
Appendix: source
Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs:132
/// 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];
// unix_ts_ms (48 bits, big-endian)
var ts = unixTsMs & 0x0000_FFFF_FFFF_FFFFL;
bytes[0] = (byte)(ts >> 40);
bytes[1] = (byte)(ts >> 32);View on GitHub (pinned to 6dee26c6ef)