SQLite for the pantry list, and the lock I made myself
I've mentioned the pantry database a couple of times. It's the smallest real program I run: one SQLite file that knows what's in the pantry, the chest freezer and the spice drawer, with best-before dates where they matter. It started in 2023 as a spreadsheet, and became a database the week I found three open jars of cumin.
Three things use it:
- a shell script reading a USB barcode scanner, plugged into the box on a ten-foot extension that runs under the closet door to a hook by the pantry (the scanner types like a keyboard, and the script reads a line and adds or removes one of whatever it was);
- a Python script I run at the box's own keyboard, for "used it up", "threw it out" and fixing my mistakes;
- a nightly job that writes a list of things to eat soon into my notes inbox.
For nearly three years this worked without my thinking about it. Then this summer the scanner script started failing now and then with Error: database is locked. Not often, maybe once a week. Just often enough that I was never sure whether the last can of tomatoes had been counted.
Where the lock came from
My first suspect was the nightly job, since it reads every row. It wasn't the main culprit, though it turned out to matter. The main culprit was me.
The interactive script uses Python's sqlite3 module, which by default quietly opens a transaction before the first INSERT or UPDATE and keeps it open until you call commit(). My script made its changes as I answered each prompt and committed once, at the very end, when I typed q. So if I marked the rice as used up and then went to look in the freezer, the database stayed locked for writing as long as that prompt sat there. Any scan in that window failed.
It failed at once, because the scanner script uses the sqlite3 command-line tool, which by default doesn't wait at all. The Python module waits up to five seconds before giving up, which is why the problem only ever showed up in one direction.
The fixes
Commit before asking anything. This was the actual bug. Each answer is now its own short transaction:
with conn:
conn.execute(
"UPDATE item SET qty = qty - 1 WHERE id = ?", (item_id,)
)
with conn: commits at the end of the block, or rolls back if something inside it raises. No transaction stays open across an input() call anymore.
A busy timeout on every connection. Short overlaps are normal, and the right response to one is to wait a moment. The timeout belongs to the connection, not the file, so the scanner script now sets it on every call. It also checks that the code is all digits before it goes anywhere near the SQL.
sqlite3 -cmd '.timeout 3000' /srv/pantry/pantry.db \
"UPDATE item SET qty = qty + 1 WHERE barcode = '$code';"
WAL mode. In the default rollback-journal mode, a reader holding the database stops a writer from committing. The nightly job reads every row before writing its list, so a scan during that read had to wait. With write-ahead logging, readers and the writer stop getting in each other's way. There's still only one writer at a time, which is why the first fix mattered more than this one. Unlike the timeout, WAL is a property of the database file, so it's a one-time change:
PRAGMA journal_mode = WAL;
WAL mode keeps two extra files next to the database, ending in -wal and -shm. Since the first restore drill my backups use SQLite's .backup command, which handles them properly. A plain copy of the main file on its own would not.
Vacuum
While I was in there I looked at the file size: 41 MB, for about 900 items. Most of it was a table where the nightly job had been saving every item's quantity, every night, since 2023, "for graphs later". I never drew a single graph. I dropped the table, and the file stayed exactly 41 MB, because SQLite keeps freed pages for reuse instead of handing them back to the filesystem.
VACUUM rebuilds the file from scratch, and took it down to 3.2 MB. To keep it from creeping back up I turned on incremental auto-vacuum, which only takes effect on an existing database after a full VACUUM, so the order matters:
PRAGMA auto_vacuum = INCREMENTAL;
VACUUM;
-- then, at the end of the nightly job:
PRAGMA incremental_vacuum;
Six weeks on
No locked errors since the first week of August. The file is 3.4 MB. And the cumin is down to one jar, which I'm told is the correct number.