DEV Community

I killed a SQLite writer mid-commit to see what would survive

Alex Georgiev on November 28, 2026

I expected killing a SQLite process with kill -9 to either lose nothing, because WAL mode is supposed to be crash-safe, or lose everything, because...
Collapse
 
kreout profile image
Andrei Ovcharenko •

The copy-paste repro needs a pause after print("PID", os.getpid()): as written, the process exits immediately, so the last connection can checkpoint/remove the WAL before there's time to send kill -9. Adding input("Kill this process from another terminal now...") there keeps it alive at the intended point. That also lines up with the clean-shutdown gotcha you described.

Collapse
 
alexgeorgiev17 profile image
Alex Georgiev •

Good catch, you're right. As written the script just falls off the end, Python closes the connection on the way out, and as the last connection it checkpoints and deletes the WAL before you've even found the PID. Which is the exact same trap I fell into with con.close() in the "what I got wrong" section, I just rebuilt it in the repro. The input() pause is the right fix, it holds the process open with the WAL still on disk until you kill it.

Collapse
 
kreout profile image
Andrei Ovcharenko •

Thanks for checking it. That is a nicely sneaky version of the same bug: removing an explicit close still leaves the normal process exit doing the cleanup. The pause makes the intended kill point much clearer for anyone copying the example.

Thread Thread
 
alexgeorgiev17 profile image
Alex Georgiev •

Yeah, dropping the close() felt like enough at the time, and the interpreter just did it for me on the way out. Thanks again for spotting it.

Collapse
 
brookiecat profile image
Brookie •

200 bytes off a good WAL and sqlite threw away the whole 50-row commit, not just the damaged part. interrupt a cat mid-pounce and you do not get half a pounce. you get a cat sitting there pretending it was not going to jump. 🐾

Collapse
 
alexgeorgiev17 profile image
Alex Georgiev •

Ha, that's pretty much it. And just like the cat, integrity_check looks at you completely straight-faced and says everything's fine, no jump was ever planned.

Collapse
 
brookiecat profile image
Brookie •

exactly, and the straight face isn't lying. integrity_check is answering a smaller question: is this .db a valid database at the commit it has. so losing the -wal makes it more confident, not less. the more you lose, the cleaner it gets.

cheap tell if you want one: row count and last rowid, recorded somewhere outside the db before the -wal went missing. structure will never mention what isn't there.

a cat sitting calmly next to a fallen glass is telling the truth about the glass. it's just not the question you asked. 🐾

Thread Thread
 
alexgeorgiev17 profile image
Alex Georgiev •

That's a better way to say it than mine. It answers "is this a valid database", not "is this the database you had". And the outside record is really the only reason the post caught it at all: I knew to expect 500, so 450 stood out. Without that number written down somewhere else, a clean integrity_check and 450 rows would have looked like a perfectly normal day.

Collapse
 
kvmdrai profile image
KVMDR •

Nice experiment. One thing worth separating: kill -9 tests process crash, not power loss. After SIGKILL the kernel page cache still holds every write, so anything that reached write() survives even if it was never fsynced. That is why synchronous never came up.

Power loss is where synchronous matters. In WAL mode with synchronous=NORMAL, the last few commits can disappear after a power cut but the database stays consistent; with FULL they survive. To test that properly, run it in a VM and hard reset it, or use dm-log-writes / dm-flakey to replay the block layer at any point.

Same rule applies to block level snapshots and replicas: the .db and the -wal file must be captured at the same instant, otherwise you get exactly your 200 byte truncation case, or worse, a WAL from a different moment than the database.

Collapse
 
alexgeorgiev17 profile image
Alex Georgiev •

Yeah, that's a fair split and the post blurs it. kill -9 only takes out the process, so everything that already went through write() is still in the page cache and lands on disk eventually. That's why the post-commit kill kept all 5,000 rows, and why synchronous never mattered in any of these runs.

It also means the 200 byte case isn't something kill -9 can produce on its own. I truncated the WAL by hand to fake a write that never fully landed, and in real life that comes from what you're describing: a power cut before fsync, or a snapshot that grabbed the -wal at a different moment than the .db. The snapshot one is a good addition, because nothing errors out there either. The database just quietly comes back one commit short, or worse if the two files really don't belong together.

dm-log-writes is a nice idea for doing this properly, being able to replay up to any point beats hard resetting a VM and hoping you hit the right moment.

Collapse
 
kvmdrai profile image
KVMDR •

Exactly, the silent case is the dangerous one. Two ways to avoid it in practice: snapshot the whole volume at one instant so the .db and -wal always match (a single LVM/ZFS snapshot, or a consistency group if they sit on different disks), or skip file copies and use VACUUM INTO / the backup API for an application consistent copy.

Thread Thread
 
alexgeorgiev17 profile image
Alex Georgiev •

Both good. VACUUM INTO is the one I'd reach for on a small db, it runs inside one read transaction so you get a single consistent file with no -wal to keep in sync, and a busy writer doesn't block it. The one thing I'd watch with the backup API is copying in small steps while another process keeps writing, because a write from a different connection makes it start over. Doing it in one step, or just using VACUUM INTO, avoids that.