Skip to content

BuildNot yet confirmed elsewhere1 publisher3 min readPublished

A mutex made of mkdir: the cheap fix for agent fleets fighting over one resource

A dev.to post turns one atomic mkdir into a logged, self-healing lock for parallel agents. The mechanism holds up. The shipped defaults are the part to read closely.

The Engineer · Build desk

How we use AISend a correction

What happened

  • A dev.to post ships lock_folder_util.py, which builds a mutual exclusion lock for agent fleets out of a single atomic mkdir.
  • Its run subcommand wraps a command so the lock is taken, the work executed and the lock released whatever the outcome.
  • Every acquire, release, stale break and timeout appends one UTC-timestamped line naming the agent to .tmp/lock.log.
  • Shipped defaults are a 15 second poll while waiting and an 1800 second overall timeout.

Why it matters

  • constraint At the default 15 second poll, a queue's wall clock is set by the polling interval rather than by the resource, so the knob matters more than the lock: twelve one-second jobs cost about 155 seconds.
  • exposure The stale break decides on age, not liveness, so anyone whose commands can exceed ten minutes is exposed to two simultaneous holders and finds out from the log afterwards.
  • decision The defaults bound fleet size multiplied by hold time, which forces a choice for anyone queueing a dozen agents on multi-minute work: raise --timeout or cut the critical section.
  • capability Corruption postmortems become attributable to a named agent and a second, which prompt-level etiquette between subagents cannot deliver.

The sample log is the strongest evidence in the post, and also the most revealing. Eight agents, first acquire at 23:05:52Z, last release at 23:06:35Z, every hold either zero or one second [1]. That is a 43 second window in which the lock was actually held for seven seconds, about 16 per cent of the wall clock [14]. The gaps between one release and the next acquire run five to six seconds [1], which is shorter than the 15 second default poll interval [9], so whatever produced that log was not running the shipped defaults [13].

Run the defaults and the shape gets worse. A waiter that misses sleeps 15 seconds [9], so each handoff costs roughly seven and a half seconds on average, and the post's own guidance adds a one-off 60 second sleep for every agent that is not first in the batch [12]. For the twelve subagents the piece opens with [6], each holding the resource for a second, that works out at about 155 seconds of wall clock to buy 12 seconds of work at the resource [15]. This is not an argument against the lock. It is an argument for setting `--poll` to the length of the critical section, and for the instruction to release immediately after the last resource command rather than after the thinking and the file writes [12].

The 1800 second default timeout draws a harder line [9]. Queue twelve agents on one resource and the last one in line gives up unless every holder stays under about 164 seconds [16]. So the defaults encode an assumption that nobody states: small fleets, short sections. A dozen agents each driving a five minute job through this lock will simply shed its tail.

Stale breaking is where the design trades safety for liveness, and the trade is worth naming. The next agent removes a lock older than `--stale`, 600 seconds by default, logs the break and proceeds [8]. That test is the directory's age, not whether the holder is still alive, so a command that outruns ten minutes has its lock deleted underneath it and gains a co-tenant [17]. The break lands in the log [7], which is how you find out, afterwards.

What makes the primitive worth the trouble is the thing it does not do. There is no lock file content to write and then read back as two separate operations: `mkdir` either creates the directory or fails because it exists, and release is `rmdir` [3][4]. The failure the post is guarding against is the interleaved command corrupting another agent's work mid-flight with no record of who held the resource [6], and a filesystem primitive settles that without asking twelve prompts to be polite to each other. The startup check closes the adjacent hole: if the lock directory is missing or not writable, the script fails loudly at second zero rather than running as a mutex that never locks anything [11].

One caveat on sourcing. The portability claim, that this holds on macOS, Linux, WSL, the BSDs and a NAS in a cupboard, and that there is no NFS weirdness, is the author's own [18]. Zero dependencies beyond the Python 3 standard library [10] makes it cheap to test that claim yourself on whatever shared storage your fleet actually mounts, which is where an atomic-looking `mkdir` stops being atomic.

What to watch

  • Whether mkdir really stays atomic on the NFS and SMB mounts the post waves through, since that is where this lock degrades silently rather than loudly.
  • Whether the tool gains a heartbeat or lock renewal so stale breaking stops keying on directory age alone.
  • Any report of the script under a real fleet from someone other than the author, since the log, the defaults and the portability claim all come from one post.

Clarity's read

What the record supports and how the coverage leans. The claims behind it follow.

Reality

Evidence52
Adoption10
Hype gap+28
Incentives58
Confidence57
Why these scores

Claim ledger

Ranked by verification strength, evidence, and original report placement.

  1. [1]

    The sample log shows agent-1 through agent-8 in 16 lines with no overlap: first acquire at 2026-08-22T23:05:52Z, last release at 2026-08-22T23:06:35Z, each hold zero or one second, and five to six seconds between a release and the next acquire.

  2. [2]

    A dev.to post headlined "Lock Folder Util - The Mutex Your Agent Swarm Desperately Needs" presents lock_folder_util.py, described as mutual exclusion for agents sharing one resource via an atomic mkdir.

    ReportedSupportedView cited source
  3. [3]

    The post states mkdir is atomic at the filesystem level: it either creates the directory or fails because it already exists, with no lock file content, and the lock is one empty directory.

    ReportedSupportedView cited source

Sources

1 independent publisher whose own reporting we read for this story.

  1. dev.to

    1 article · August 22, 2026

    🔒 Lock Folder Util — The Mutex Your Agent Swarm Desperately Needs

Share your take

Let Clarity write the post for you.

Signed-in readers get a short post drafted on this story in the register they choose — narrative, analytical, or a direct position — editable to the last word before it goes anywhere. The share buttons at the top of this story work without an account.

Topics and entities

Follow any of these and your For You feed starts watching them — no settings page required.

Topics

Entities

Loading related stories