Skip to content

Commit 63eab52

Browse files
authored
Prevent WAL writes when using EXCLUSIVE locking mode (#426)
1 parent 1dc7fcf commit 63eab52

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Diff for: cmd/litefs/mount_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,43 @@ func TestEventStream(t *testing.T) {
25832583
})
25842584
}
25852585

2586+
// See: https://github.com/superfly/litefs/issues/425
2587+
func TestPreventExclusiveLockingModeWithWAL(t *testing.T) {
2588+
if !testingutil.IsWALMode() {
2589+
t.Skip("test only applies to WAL mode, skipping")
2590+
}
2591+
2592+
// Ensures writes to a new WAL fail if using EXCLUSIVE locking mode.
2593+
t.Run("WALHeader", func(t *testing.T) {
2594+
cmd0 := runMountCommand(t, newMountCommand(t, t.TempDir(), nil))
2595+
db := testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))
2596+
2597+
if _, err := db.Exec("PRAGMA locking_mode = EXCLUSIVE"); err != nil {
2598+
t.Fatal(err)
2599+
}
2600+
if _, err := db.Exec(`CREATE TABLE t (x)`); err == nil || err.Error() != `disk I/O error` {
2601+
t.Fatalf("unexpected error: %v", err)
2602+
}
2603+
})
2604+
2605+
// Ensures writes to an existing WAL fail if using EXCLUSIVE locking mode.
2606+
t.Run("WALFrame", func(t *testing.T) {
2607+
cmd0 := runMountCommand(t, newMountCommand(t, t.TempDir(), nil))
2608+
db := testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))
2609+
2610+
if _, err := db.Exec(`CREATE TABLE t (x)`); err != nil {
2611+
t.Fatal(err)
2612+
}
2613+
2614+
if _, err := db.Exec("PRAGMA locking_mode = EXCLUSIVE"); err != nil {
2615+
t.Fatal(err)
2616+
}
2617+
if _, err := db.Exec(`INSERT INTO t VALUES ('foo')`); err == nil || err.Error() != `disk I/O error` {
2618+
t.Fatalf("unexpected error: %v", err)
2619+
}
2620+
})
2621+
}
2622+
25862623
// Ensure multiple nodes can run in a cluster for an extended period of time.
25872624
func TestFunctional_OK(t *testing.T) {
25882625
if *funTime <= 0 {

Diff for: db.go

+15
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,11 @@ func (db *DB) writeWALHeader(ctx context.Context, f *os.File, data []byte, offse
13791379
return fmt.Errorf("WAL header write must be 32 bytes in size, received %d", len(data))
13801380
}
13811381

1382+
// Prevent transactions when the write lock has not been acquired (e.g. EXCLUSIVE lock)
1383+
if db.writeLock.State() != RWMutexStateExclusive {
1384+
return fmt.Errorf("cannot write to WAL header without WRITE lock, exclusive locking not allowed")
1385+
}
1386+
13821387
// Determine byte order of checksums.
13831388
switch magic := binary.BigEndian.Uint32(data[0:]); magic {
13841389
case 0x377f0682:
@@ -1427,6 +1432,11 @@ func (db *DB) writeWALFrameHeader(ctx context.Context, f *os.File, data []byte,
14271432
}
14281433
}()
14291434

1435+
// Prevent transactions when the write lock has not been acquired (e.g. EXCLUSIVE lock)
1436+
if db.writeLock.State() != RWMutexStateExclusive {
1437+
return fmt.Errorf("cannot write to WAL frame header without WRITE lock, exclusive locking not allowed")
1438+
}
1439+
14301440
// Prevent SQLite from writing before the current WAL position.
14311441
if offset < db.wal.offset {
14321442
return fmt.Errorf("cannot write wal frame header @%d before current WAL position @%d", offset, db.wal.offset)
@@ -1442,6 +1452,11 @@ func (db *DB) writeWALFrameData(ctx context.Context, f *os.File, data []byte, of
14421452
TraceLog.Printf("[WriteWALFrameData(%s)]: offset=%d size=%d owner=%d %s", db.name, offset, len(data), owner, errorKeyValue(err))
14431453
}()
14441454

1455+
// Prevent transactions when the write lock has not been acquired (e.g. EXCLUSIVE lock)
1456+
if db.writeLock.State() != RWMutexStateExclusive {
1457+
return fmt.Errorf("cannot write to WAL frame data without WRITE lock, exclusive locking not allowed")
1458+
}
1459+
14451460
// Prevent SQLite from writing before the current WAL position.
14461461
if offset < db.wal.offset {
14471462
return fmt.Errorf("cannot write wal frame data @%d before current WAL position @%d", offset, db.wal.offset)

0 commit comments

Comments
 (0)