{"record":{"id":"4d3d9a481aee24de","repo":"clockworklabs/SpacetimeDB","slug":"operation-is-not-valid-due-to-the-current-state-of","errorCode":null,"errorMessage":"Operation is not valid due to the current state of the object.","messagePattern":"Operation is not valid due to the current state of the object\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/Builtins.cs","lineNumber":714,"sourceCode":"    {\n        Ok = 0,\n        Err = 1,\n    }\n\n    public readonly struct BSATN<OkRW, ErrRW> : IReadWrite<Result<T, E>>\n        where OkRW : struct, IReadWrite<T>\n        where ErrRW : struct, IReadWrite<E>\n    {\n        private static readonly SpacetimeDB.BSATN.Enum<ResultVariant> __enumTag = new();\n        private static readonly OkRW okRW = new();\n        private static readonly ErrRW errRW = new();\n\n        public Result<T, E> Read(BinaryReader reader) =>\n            __enumTag.Read(reader) switch\n            {\n                ResultVariant.Ok => new OkR(okRW.Read(reader)),\n                ResultVariant.Err => new ErrR(errRW.Read(reader)),\n                _ => throw new InvalidOperationException(),\n            };\n\n        public void Write(BinaryWriter writer, Result<T, E> value)\n        {\n            switch (value)\n            {\n                case OkR(var v):\n                    __enumTag.Write(writer, ResultVariant.Ok);\n                    okRW.Write(writer, v);\n                    break;\n\n                case ErrR(var e):\n                    __enumTag.Write(writer, ResultVariant.Err);\n                    errRW.Write(writer, e);\n                    break;\n            }\n        }\n","sourceCodeStart":696,"sourceCodeEnd":732,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/Builtins.cs#L696-L732","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before\nusing var reader = new BinaryReader(ms);\nvar result = resultRW.Read(reader); // InvalidOperationException when tag byte >= 2\n\n// after - validate the tag byte (Ok=0, Err=1) before decoding\nms.Position = 0;\nvar tag = ms.ReadByte();\nif (tag > 1) throw new InvalidDataException($\"Corrupt Result payload, tag={tag}\");\nms.Position = 0;\nvar result = resultRW.Read(reader);","handlingStrategy":"try-catch","validationCode":"// ResultVariant tags serialize as one byte: Ok=0, Err=1.\n// If you own the buffer, cheap sanity check before decoding:\nif (payload.Length == 0 || payload[0] > 1)\n    throw new InvalidDataException($\"Not a Result payload (tag={payload.FirstOrDefault()})\");","typeGuard":null,"tryCatchPattern":"try { var result = resultRW.Read(reader); }\ncatch (InvalidOperationException)\n{\n    // tag byte >= 2: corrupt/mismatched BSATN. Drop the buffer, do not retry the same bytes;\n    // re-establish the stream (reconnect / re-request) or fix the version mismatch.\n}","preventionTips":["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"],"tags":["csharp","bsatn","deserialization","result","corrupt-data"],"backgroundTag":"invalid-enum-discriminant","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}