{"record":{"id":"efb6cb17f29dc0db","repo":"litedb-org/LiteDB","slug":"current-stream-are-readonly","errorCode":null,"errorMessage":"Current stream are readonly","messagePattern":"Current stream are readonly","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/Disk/Streams/ConcurrentStream.cs","lineNumber":69,"sourceCode":"                return _position;\n            }\n        }\n\n        public override int Read(byte[] buffer, int offset, int count)\n        {\n            // lock internal stream and set position before read\n            lock (_stream)\n            {\n                _stream.Position = _position;\n                var read = _stream.Read(buffer, offset, count);\n                _position = _stream.Position;\n                return read;\n            }\n        }\n\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n            if (_canWrite == false) throw new NotSupportedException(\"Current stream are readonly\");\n\n            // lock internal stream and set position before write\n            lock (_stream)\n            {\n                _stream.Position = _position;\n                _stream.Write(buffer, offset, count);\n                _position = _stream.Position;\n            }\n        }\n    }\n}","sourceCodeStart":51,"sourceCodeEnd":80,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/Disk/Streams/ConcurrentStream.cs#L51-L80","documentation":"Thrown by ConcurrentStream.Write when the stream was created in read-only mode (_canWrite == false). ConcurrentStream wraps a base Stream with a canWrite flag; if the wrapper was constructed with canWrite=false, any Write call throws NotSupportedException. This is an internal engine type used for disk/stream management.","triggerScenarios":"A ConcurrentStream instantiated with canWrite=false (e.g., for a read-only data file or log stream) receives a Write call. This typically originates from engine internals attempting to write to a stream opened for reading only.","commonSituations":"Opening a database file in read-only mode (connection string with ReadOnly=true) and then performing an insert/update/delete. Engine misconfiguration where a read snapshot stream is used for a write operation. A bug in stream routing inside the engine.","solutions":["If you opened the database read-only, remove `ReadOnly=true` from the connection string to allow writes.","Ensure write operations target a read-write connection.","If this fires from engine internals without a read-only connection, report it as a bug with the operation that triggered it."],"exampleFix":"// before\nusing var db = new LiteDatabase(\"filename=data.db;readonly=true\");\ndb.GetCollection(\"items\").Insert(new BsonDocument());\n// after\nusing var db = new LiteDatabase(\"filename=data.db\");\ndb.GetCollection(\"items\").Insert(new BsonDocument());","handlingStrategy":"validation","validationCode":"// Ensure the connection string does not set ReadOnly=true when writes are needed\nif (connectionString.Contains(\"readonly=true\", StringComparison.OrdinalIgnoreCase))\n    throw new InvalidOperationException(\"Cannot write on a read-only database.\");","typeGuard":null,"tryCatchPattern":"try { db.GetCollection(\"items\").Insert(doc); }\ncatch (NotSupportedException ex) when (ex.Message == \"Current stream are readonly\")\n{ /* reopen database without ReadOnly=true */ }","preventionTips":["Do not set ReadOnly=true in the connection string for databases that need writes.","Separate read-only and read-write connections clearly in configuration.","Test write operations after changing connection-string parameters."],"tags":["engine","stream","readonly","not-supported","io"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}