litedb-org/LiteDB · error · NotSupportedException

Current stream are readonly

Error message

Current stream are readonly

What it means

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.

Source

Thrown at LiteDB/Engine/Disk/Streams/ConcurrentStream.cs:69

                return _position;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            // lock internal stream and set position before read
            lock (_stream)
            {
                _stream.Position = _position;
                var read = _stream.Read(buffer, offset, count);
                _position = _stream.Position;
                return read;
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_canWrite == false) throw new NotSupportedException("Current stream are readonly");

            // lock internal stream and set position before write
            lock (_stream)
            {
                _stream.Position = _position;
                _stream.Write(buffer, offset, count);
                _position = _stream.Position;
            }
        }
    }
}

View on GitHub (pinned to f906a5f850)

Solutions

  1. If you opened the database read-only, remove `ReadOnly=true` from the connection string to allow writes.
  2. Ensure write operations target a read-write connection.
  3. If this fires from engine internals without a read-only connection, report it as a bug with the operation that triggered it.

Example fix

// before
using var db = new LiteDatabase("filename=data.db;readonly=true");
db.GetCollection("items").Insert(new BsonDocument());
// after
using var db = new LiteDatabase("filename=data.db");
db.GetCollection("items").Insert(new BsonDocument());
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the connection string does not set ReadOnly=true when writes are needed
if (connectionString.Contains("readonly=true", StringComparison.OrdinalIgnoreCase))
    throw new InvalidOperationException("Cannot write on a read-only database.");

Try / catch

try { db.GetCollection("items").Insert(doc); }
catch (NotSupportedException ex) when (ex.Message == "Current stream are readonly")
{ /* reopen database without ReadOnly=true */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/efb6cb17f29dc0db. Report an issue: GitHub.