Missing syncDir on value log rotation and L0 table flush breaks crash consistency

Open
#2,364 0 comments 0 reactions 0 assignees View on GitHub

@hcrosse is already working on this.

Since Nov 21, 2026.

  • #2365 by @hcrosse — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
database

Research direction

Start with createVlogFile in value.go (~L526-553) and the comment above valueLog.sync (~L749-751), then compare addLevel0Table in levels.go (~L1656-1670) with the directory sync in compactBuildTables (~L1009-1014). Read handleMemTableFlush in db.go (~L1138-1146) to see the MANIFEST write path. Done means the new .vlog and .sst directory entries are durable before publication, with cleanup on sync failure, plus a crash-simulation test that drops the directory entry and reopens.

Written by the indexing model from the issue text.

Description

Describe the bug

Two file creation paths in Badger omit syncDir before referencing or publishing the new file, breaking crash consistency even with WithSyncWrites(true):

  1. Missing syncDir on value log rotation (silent corruption and cross key value substitution): createVlogFile calls lf.open(...) and publishes the new .vlog file without syncing the file or calling syncDir(vlog.dirPath). The directory sync added in #1434 was dropped during #1555, even though the comment above valueLog.sync still assumes createVlogFile syncs the value log directory on every rotation. If power is lost after a rotated .vlog (such as 000002.vlog) receives acknowledged writes and its LSM value pointer is synced in the memtable WAL, while the unsynced 000002.vlog directory entry is lost, badger.Open succeeds and recreates an empty 000002.vlog. Subsequent reads of previously acknowledged keys return empty byte slices with err == nil (even with WithVerifyValueChecksum(true)), and once new large values are written to the recreated 000002.vlog, reads of the old keys silently return the new keys' values with err == nil and a valid checksum.

  2. Missing syncDir on L0 table flush (permanent badger.Open failure): handleMemTableFlush calls table.CreateTable and addLevel0Table, which appends the new L0 .sst file to MANIFEST and fsyncs MANIFEST without first calling syncDir(db.opt.Dir) (unlike compaction in compactBuildTables). If power is lost after MANIFEST is durable while the unsynced .sst directory entry is lost, reopening fails with file does not exist for table 1.

To Reproduce

  1. Value log rotation: Open a DB with WithSyncWrites(true), WithVerifyValueChecksum(true), WithValueThreshold(256), and WithValueLogFileSize(1 << 20). Write large values until 000002.vlog is created and receives a committed key k_old. Simulate losing the unsynced 000002.vlog directory entry while retaining the synced memtable WAL and earlier files. Reopen the DB: badger.Open succeeds, reading k_old returns an empty value with err == nil, and writing a new large value k_new causes a subsequent read of k_old to return k_new's value with err == nil.
  2. L0 table flush: Open a DB with WithSyncWrites(true) and flush a memtable into 000001.sst. Observe that MANIFEST is synced without any intervening syncDir(opt.Dir). Simulate losing the unsynced 000001.sst directory entry while retaining the synced MANIFEST. Reopening with badger.Open fails with file does not exist for table 1.

Expected behavior

  1. createVlogFile should sync the newly created .vlog file and call vlog.db.syncDir(vlog.dirPath) before publishing the file in filesMap, cleaning up the file if syncing fails.
  2. addLevel0Table should call s.kv.syncDir(s.kv.opt.Dir) before recording the new .sst table in MANIFEST (when !t.IsInmemory).

Environment

  • OS: Linux / macOS
  • Language: Go
  • Version: v4 (main)
Dominant language
Go
Stars
15.8k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dgraph-io/badger

All issues in dgraph-io/badger

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.