Cost & failure log

"Database Is Locked": The Night Our Support Bot Gave Up 7 Times

One uncommitted SQLite write and a Friday deploy: 304 "database is locked" errors, 7 chats abandoned. The git log, a reproduction, and the fix.

· by OI

Most “AI bot” outages are not AI outages. This one was a SQLite file and a thread pool having a disagreement, and I’m writing it up because every tutorial on “running concurrent bots” skips straight past it, whistling.

Database is locked

Three bots honking at a SQLite padlock

What the bot does

Every closed support chat gets summarised by Claude and written as a row to our claims sheet. To make that fast, three summaries run in parallel. The bot keeps its local state in a SQLite file: which chats are done, which photos it has already shown, how many times it has retried something.

What happened, from the git log

The log from that night has since rotated away, but the commits haven’t, so here is the evening by commit time:

Time (KST) What
18:53 I ship “summarise up to three closed chats at once”. Sheet writes stay one at a time. It’s a Friday.
evening The log fills with database is locked. 304 lines before the night is over.
22:51 Fix: two commit() calls (below). The errors stop.
22:59 Give-up alerts move from the team channel to my DM.
23:15 Abandoned chats are removed from the failure list, which ends the alert storm.

The log looked like this, like a toddler who’s learned one word:

The log that evening

Seven chats were abandoned. Each was retried before the bot gave up on it, and the retry counts are the shape of the evening: 63, 62, 61, 31, 25, 20 and 10. Nothing was lost in the end: most of those chats turned out not to be claims, and the real ones had already been written into the sheet by the support team, who don’t wait for a bot.

That evening, in numbers

Why

SQLite allows one writer at a time, and a connection that has written something but not committed is still holding the write lock.

The main loop saved customer profiles (a write) on its connection and didn’t commit. Then it handed three chats to the thread pool. Each worker opened its own connection and tried to write its result. All three waited for a lock the main connection was sitting on, hit the 120-second busy timeout, and failed. The retry loop then tried again a minute later, into the same open transaction.

Before that evening there was no pool, so the main connection’s next commit always came before the next write and nobody noticed the habit. Parallelism didn’t cause the bug. It made a sloppy habit fatal.

You can watch it happen in about four seconds. This is a 40-line reproduction (scripts/repro/locked_repro.py) run on my MacBook, with the busy timeout shortened to two seconds:

Real run: three workers fail with database is locked; with one commit first, all three save instantly

The fix

The real diff, with the Korean comments translated:

git show of the two fix commits from 2 October

That’s it: commit on the main connection before anything else wants to write, and keep the model calls parallel and the writes on one connection, one at a time. The summaries never touched the file, so they stayed parallel. It hasn’t come back: grep -c "database is locked" over the four log files I have from this week returns zero for each.

The second-order fix

The alert storm was a separate bug with the same night. A chat that hit its retry limit was abandoned and announced in the team channel, but it stayed in the failure list. On the next round, a minute later, the bot found it there, gave up on it again and announced it again. Seven abandoned chats became 265 messages in the channel before 23:15. The fix was the third line in the diff: drop the chat from the list when you give up on it. Give-ups now go to one DM, once each.

What I’d check first next time

  1. Is there an uncommitted write on the main connection when work is handed off?
  2. Did a change in the last few hours add threads or processes that open their own connections?
  3. Is anything announcing the same failure more than once?

And one rule that isn’t about SQLite: don’t ship concurrency on a Friday evening. Nobody is watching the bots on the weekend except the bots.

One line from me

The model was fine all night. It was one uncommitted write and a list that forgot to forget. Check the boring layer first.

The rules for which alerts survive a night like that are in why every bot alert goes to one DM.