clockworklabs/SpacetimeDB · error · InvalidOperationException
Operation is not valid due to the current state of the objec
Error message
Operation is not valid due to the current state of the object.
What it means
Thrown while BSATN-deserializing a Result<T,E>: Result.BSATN.Read reads a one-byte variant tag (ResultVariant.Ok=0, Err=1) and anything else hits `_ => throw new InvalidOperationException()` — the parameterless ctor whose runtime-supplied default message is 'Operation is not valid due to the current state of the object.'. It means the byte stream being decoded is not a valid Result: the discriminant byte is >= 2, typically from truncated, corrupted, or version-mismatched data.
Source
Thrown at crates/bindings-csharp/BSATN.Runtime/Builtins.cs:714
{
Ok = 0,
Err = 1,
}
public readonly struct BSATN<OkRW, ErrRW> : IReadWrite<Result<T, E>>
where OkRW : struct, IReadWrite<T>
where ErrRW : struct, IReadWrite<E>
{
private static readonly SpacetimeDB.BSATN.Enum<ResultVariant> __enumTag = new();
private static readonly OkRW okRW = new();
private static readonly ErrRW errRW = new();
public Result<T, E> Read(BinaryReader reader) =>
__enumTag.Read(reader) switch
{
ResultVariant.Ok => new OkR(okRW.Read(reader)),
ResultVariant.Err => new ErrR(errRW.Read(reader)),
_ => throw new InvalidOperationException(),
};
public void Write(BinaryWriter writer, Result<T, E> value)
{
switch (value)
{
case OkR(var v):
__enumTag.Write(writer, ResultVariant.Ok);
okRW.Write(writer, v);
break;
case ErrR(var e):
__enumTag.Write(writer, ResultVariant.Err);
errRW.Write(writer, e);
break;
}
}
View on GitHub (pinned to 6dee26c6ef)
Solutions
- Confirm the bytes were produced by serializing a Result<T,E> with the same generic arguments and package version
- Match client SDK and server module versions (rebuild/republish the module, update the NuGet packages) so Result wire layout agrees
- Verify the BinaryReader position before Read: the tag byte must be the first byte of the value
- If you control the buffer, inspect the first byte: anything >1 proves corruption before deeper debugging
Example fix
// before
using var reader = new BinaryReader(ms);
var result = resultRW.Read(reader); // InvalidOperationException when tag byte >= 2
// after - validate the tag byte (Ok=0, Err=1) before decoding
ms.Position = 0;
var tag = ms.ReadByte();
if (tag > 1) throw new InvalidDataException($"Corrupt Result payload, tag={tag}");
ms.Position = 0;
var result = resultRW.Read(reader); Defensive patterns
Strategy: try-catch
Validate before calling
// ResultVariant tags serialize as one byte: Ok=0, Err=1.
// If you own the buffer, cheap sanity check before decoding:
if (payload.Length == 0 || payload[0] > 1)
throw new InvalidDataException($"Not a Result payload (tag={payload.FirstOrDefault()})"); Try / catch
try { var result = resultRW.Read(reader); }
catch (InvalidOperationException)
{
// tag byte >= 2: corrupt/mismatched BSATN. Drop the buffer, do not retry the same bytes;
// re-establish the stream (reconnect / re-request) or fix the version mismatch.
} Prevention
- Keep client SDK and server module on matching SpacetimeDB releases
- Always read a value from the exact position its writer started — don't share a BinaryReader across guessed offsets
- Validate message framing/lengths before handing payloads to BSATN readers
When it happens
Trigger: Decoding BSATN bytes into Result<T,E> when the buffer starts with a byte other than 0 or 1 — e.g. a client SDK message decoded with the wrong type/schema, a host and bindings disagreeing on the payload layout, or a truncated response body fed to FromBytes.
Common situations: Client SDK version newer/older than the spacetimedb server so wire schemas drift; reusing a BinaryReader positioned mid-buffer (reading a Result where a different value begins); hand-rolled BSATN buffers with a wrong tag byte; corrupted transport (partially written files/streams).
Related errors
- Invalid tag value, this state should be unreachable.
- Tag {tag} is out of range of enum {typeof(T).Name}
- Result failed without an error object.
- Unknown Result variant.
- Unrecognized extra bytes while decoding BSATN value
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/4d3d9a481aee24de.
Report an issue: GitHub.