dgraph-io/badger · error

ErrBlockedWrites

ErrBlockedWrites

Error message

Writes are blocked, possibly due to DropAll or Close

What it means

ErrBlockedWrites signals that the DB has stopped accepting new writes. Badger sets this state during DropAll() and during Close(), so any write attempted in that window is rejected with this error. It is intentional flow control: dropping all data must happen without concurrent writes corrupting the DB.

Source

Thrown at errors.go:94

	ErrInvalidDump = stderrors.New("Data dump cannot be read")

	// ErrZeroBandwidth is returned if the user passes in zero bandwidth for sequence.
	ErrZeroBandwidth = stderrors.New("Bandwidth must be greater than zero")

	// ErrWindowsNotSupported is returned when opt.ReadOnly is used on Windows
	ErrWindowsNotSupported = stderrors.New("Read-only mode is not supported on Windows")

	// ErrPlan9NotSupported is returned when opt.ReadOnly is used on Plan 9
	ErrPlan9NotSupported = stderrors.New("Read-only mode is not supported on Plan 9")

	// ErrTruncateNeeded is returned when the value log gets corrupt, and requires truncation of
	// corrupt data to allow Badger to run properly.
	ErrTruncateNeeded = stderrors.New(
		"Log truncate required to run DB. This might result in data loss")

	// ErrBlockedWrites is returned if the user called DropAll. During the process of dropping all
	// data from Badger, we stop accepting new writes, by returning this error.
	ErrBlockedWrites = stderrors.New("Writes are blocked, possibly due to DropAll or Close")

	// ErrNilCallback is returned when subscriber's callback is nil.
	ErrNilCallback = stderrors.New("Callback cannot be nil")

	// ErrEncryptionKeyMismatch is returned when the storage key is not
	// matched with the key previously given.
	ErrEncryptionKeyMismatch = stderrors.New("Encryption key mismatch")

	// ErrInvalidDataKeyID is returned if the datakey id is invalid.
	ErrInvalidDataKeyID = stderrors.New("Invalid datakey id")

	// ErrInvalidEncryptionKey is returned if length of encryption keys is invalid.
	ErrInvalidEncryptionKey = stderrors.New("Encryption key's length should be" +
		"either 16, 24, or 32 bytes")
	// ErrGCInMemoryMode is returned when db.RunValueLogGC is called in in-memory mode.
	ErrGCInMemoryMode = stderrors.New("Cannot run value log GC when DB is opened in InMemory mode")

	// ErrGCInReadOnlyMode is returned when db.RunValueLogGC is called in read-only mode.

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Wrap writes in a check/retry that waits until the block is lifted, e.g. loop while err == badger.ErrBlockedWrites with a sleep, as write_bench.go does
  2. Ensure all writers are stopped (or coordinated via a mutex/channel) before calling DropAll or Close
  3. Call DropAll before starting writers, or use db.BlockWrites/UnblockWrites deliberately around maintenance windows
  4. Use db.NewManagedWriteBatch() afresh after the block clears, since batches can carry stale state

Example fix

// before
err := db.DropAll()
if err != nil {
    return err
}
// after
for err == badger.ErrBlockedWrites {
    time.Sleep(300 * time.Millisecond)
    err = db.DropAll()
}
Defensive patterns

Strategy: retry

Validate before calling

if db.IsClosed() { return errors.New("db closed, cannot write") }

Try / catch

for {
    err := batch.SetEntryAt(e, ts)
    if err == nil { break }
    if errors.Is(err, badger.ErrBlockedWrites) {
        time.Sleep(time.Second)
        batch = db.NewManagedWriteBatch()
        continue
    }
    return err
}

Prevention

When it happens

Trigger: Calling db.DropAll() (or DropPrefix) and writing concurrently; calling write APIs (batch.SetEntryAt, sendToWriteCh, WriteBatch) while the DB is closing; retry loops around DropAll that keep colliding with the write-block window.

Common situations: Benchmarks/tools that drop and rewrite data in loops (e.g. write_bench retried DropAll in a tight loop); goroutines still writing while another goroutine shuts the DB down; LongGoNCallback-free writes racing with db.Close().

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/4c5f146f12fcaf90. Report an issue: GitHub.