{"record":{"id":"08fdd487c9843957","repo":"tursodatabase/turso","slug":"unable-to-read-native-value-bytes","errorCode":null,"errorMessage":"Unable to read native value bytes.","messagePattern":"Unable to read native value bytes\\.","errorType":"exception","errorClass":"TursoException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Raw/Public/TursoBindings.cs","lineNumber":406,"sourceCode":"        if (bytes.Length == 0)\n            return bind(IntPtr.Zero);\n\n        var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);\n        try\n        {\n            return bind(handle.AddrOfPinnedObject());\n        }\n        finally\n        {\n            handle.Free();\n        }\n    }\n\n    private static byte[] ReadBytes(TursoStatementHandle statement, UIntPtr index)\n    {\n        var length = TursoInterop.StatementRowValueBytesCount(statement, index);\n        if (length < 0)\n            throw new TursoException(\"Unable to read native value bytes.\");\n        if (length == 0)\n            return [];\n\n        var ptr = TursoInterop.StatementRowValueBytesPtr(statement, index);\n        if (ptr == IntPtr.Zero)\n            throw new TursoException(\"Unable to read native value bytes.\");\n\n        unsafe\n        {\n            var data = new ReadOnlySpan<byte>((void*)ptr, checked((int)length));\n            return data.ToArray();\n        }\n    }\n\n    private static UIntPtr ToNativeIndex(int index) => checked((UIntPtr)(ulong)index);\n\n    private static UIntPtr ToNativeLength(int length) => checked((UIntPtr)(ulong)length);\n","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Raw/Public/TursoBindings.cs#L388-L424","documentation":"ReadBytes backs text and blob reads in the raw bindings: it first asks native code for the current value's byte count, and a negative count throws TursoException with this message. The native layer reporting no readable length for a value the type kind said was Text/Blob indicates the row is not currently valid — e.g. the statement was stepped past its data or is being disturbed concurrently.","triggerScenarios":"Calling TursoBindings.GetValue(statement, i) (which routes Text/Blob kinds through ReadBytes) when the statement has no current row — before a successful Read(), after Read() returned false/Done, or after another thread re-stepped the statement; also possible with a native library that does not match the bindings.","commonSituations":"Missing `if (TursoBindings.Read(stmt))` guard before GetValue; sharing one statement across tasks; reading values after finalizing; version skew between the native turso library and the managed package.","solutions":["Only call GetValue after Read() returned true for that row, and read all columns before stepping again.","Keep one statement on one thread/task; serialize access.","Confirm the native library version matches the bindings package.","If it persists with correct sequencing, capture the SQL, column kinds, and library versions and report it upstream."],"exampleFix":"// before\nvar value = TursoBindings.GetValue(statement, 0); // no row stepped yet -> TursoException\n\n// after\nif (TursoBindings.Read(statement))\n{\n    var value = TursoBindings.GetValue(statement, 0);\n}","handlingStrategy":"validation","validationCode":"// step to a row before reading any value\nif (TursoBindings.Read(statement))\n{\n    var value = TursoBindings.GetValue(statement, columnIndex);\n}","typeGuard":null,"tryCatchPattern":"try { v = TursoBindings.GetValue(stmt, i); }\ncatch (TursoException ex) when (ex.Message == \"Unable to read native value bytes.\") { /* row not current or version skew */ }","preventionTips":["Read all columns immediately after the Read() that produced the row.","One statement per thread; never step and read concurrently.","Keep native and managed package versions aligned."],"tags":["dotnet","native-interop","blob-read","row-lifecycle"],"backgroundTag":"native-interop-read-failed","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}