{"record":{"id":"4c5f146f12fcaf90","repo":"dgraph-io/badger","slug":"errblockedwrites","errorCode":"ErrBlockedWrites","errorMessage":"Writes are blocked, possibly due to DropAll or Close","messagePattern":"Writes are blocked, possibly due to DropAll or Close","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":94,"sourceCode":"\tErrInvalidDump = stderrors.New(\"Data dump cannot be read\")\n\n\t// ErrZeroBandwidth is returned if the user passes in zero bandwidth for sequence.\n\tErrZeroBandwidth = stderrors.New(\"Bandwidth must be greater than zero\")\n\n\t// ErrWindowsNotSupported is returned when opt.ReadOnly is used on Windows\n\tErrWindowsNotSupported = stderrors.New(\"Read-only mode is not supported on Windows\")\n\n\t// ErrPlan9NotSupported is returned when opt.ReadOnly is used on Plan 9\n\tErrPlan9NotSupported = stderrors.New(\"Read-only mode is not supported on Plan 9\")\n\n\t// ErrTruncateNeeded is returned when the value log gets corrupt, and requires truncation of\n\t// corrupt data to allow Badger to run properly.\n\tErrTruncateNeeded = stderrors.New(\n\t\t\"Log truncate required to run DB. This might result in data loss\")\n\n\t// ErrBlockedWrites is returned if the user called DropAll. During the process of dropping all\n\t// data from Badger, we stop accepting new writes, by returning this error.\n\tErrBlockedWrites = stderrors.New(\"Writes are blocked, possibly due to DropAll or Close\")\n\n\t// ErrNilCallback is returned when subscriber's callback is nil.\n\tErrNilCallback = stderrors.New(\"Callback cannot be nil\")\n\n\t// ErrEncryptionKeyMismatch is returned when the storage key is not\n\t// matched with the key previously given.\n\tErrEncryptionKeyMismatch = stderrors.New(\"Encryption key mismatch\")\n\n\t// ErrInvalidDataKeyID is returned if the datakey id is invalid.\n\tErrInvalidDataKeyID = stderrors.New(\"Invalid datakey id\")\n\n\t// ErrInvalidEncryptionKey is returned if length of encryption keys is invalid.\n\tErrInvalidEncryptionKey = stderrors.New(\"Encryption key's length should be\" +\n\t\t\"either 16, 24, or 32 bytes\")\n\t// ErrGCInMemoryMode is returned when db.RunValueLogGC is called in in-memory mode.\n\tErrGCInMemoryMode = stderrors.New(\"Cannot run value log GC when DB is opened in InMemory mode\")\n\n\t// ErrGCInReadOnlyMode is returned when db.RunValueLogGC is called in read-only mode.","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L76-L112","documentation":"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.","triggerScenarios":"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.","commonSituations":"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().","solutions":["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","Ensure all writers are stopped (or coordinated via a mutex/channel) before calling DropAll or Close","Call DropAll before starting writers, or use db.BlockWrites/UnblockWrites deliberately around maintenance windows","Use db.NewManagedWriteBatch() afresh after the block clears, since batches can carry stale state"],"exampleFix":"// before\nerr := db.DropAll()\nif err != nil {\n    return err\n}\n// after\nfor err == badger.ErrBlockedWrites {\n    time.Sleep(300 * time.Millisecond)\n    err = db.DropAll()\n}","handlingStrategy":"retry","validationCode":"if db.IsClosed() { return errors.New(\"db closed, cannot write\") }","typeGuard":null,"tryCatchPattern":"for {\n    err := batch.SetEntryAt(e, ts)\n    if err == nil { break }\n    if errors.Is(err, badger.ErrBlockedWrites) {\n        time.Sleep(time.Second)\n        batch = db.NewManagedWriteBatch()\n        continue\n    }\n    return err\n}","preventionTips":["Coordinate DropAll/Close with all writer goroutines via a WaitGroup or mutex","Retry only on ErrBlockedWrites; fail fast on other errors","Recreate write batches after the block clears","Perform destructive maintenance (DropAll) in a dedicated window with no active writers"],"tags":["badger","concurrency","lifecycle","writes-blocked"],"backgroundTag":"writes-blocked-during-shutdown","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}