Everything you never wanted to know about file locking (2010)

File locks are a hot mess: Mac bug, database fear, and devs losing it

TLDR: A developer flagged a Mac OS X 10.6 bug that could corrupt SQLite databases via broken file locking. Commenters blasted Unix locking as “insane,” debated outdated advice, and swapped hacks like using a directory as a lock—because when safety tools are shaky, creativity becomes survival.

The internet is clutching pearls over file locks after a 2010 deep-dive revealed a Mac OS X 10.6 bug that could corrupt SQLite databases. Translation for non-nerds: the “traffic lights” that keep multiple apps from crashing into each other over the same file are broken in some places, and people are mad. The author says the three main ways to lock files across Unix systems are inconsistent, unpredictable, and honestly untrustworthy, with flock (simple whole-file locks), fcntl (fancier, per‑byte locks), and lockf (another flavor) all behaving differently.

Cue the comments. One camp is raging: “Unix just totally screwed it up for no reason,” snarled Bratmon, while jabl went full caps-lock energy calling it “insane” and nitpicking that the page is a bit outdated, linking the Linux flock manpage for receipts. The pragmatists showed up with DIY hacks—chasil flexed a cheeky trick: just make a directory and treat that as your lock. Meanwhile, pseudohadamard pointed everyone to the battle-hardened SQLite locking module, basically saying, “These folks survived the chaos, take notes.”

Between applause (“THANK YOU”) and eye-rolls, the vibe is frustrated realism: locking should be simple, but the real world is messy. The meme of the day? “mkdir as mutex.” The cliffhanger? Who do you trust when even standard tools don’t agree—and your database might get wrecked

Key Points

  • A bug in Mac OS X 10.6’s fcntl(F_SETLK) locking can corrupt SQLite databases, and a test program (locky.c) is provided.
  • Unix file locking is inconsistent and unreliable across systems and versions, even within Linux.
  • There are three Unix file locking APIs: flock(), fcntl(), and lockf(); simple lockfiles are an alternative not detailed here.
  • flock() locks entire files, is not POSIX-standard, may not work over NFS, and upgrading shared to exclusive is racily implemented.
  • fcntl(F_SETLK) is POSIX-standard and supports shared/exclusive byte-range locks that can extend beyond a file’s end.

Hottest takes

"Unix (and thus Linux and OSX) just totally screwed it up for no reason" — Bratmon
"One sure way to get a lock is to make a directory" — chasil
"Unix/POSIX locking is insane" — jabl
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.