The world's dumbest semaphore, revisited

In two agents, one Proxmox box, I mentioned a file lock around the commands that eat the machine. That deserved more explanation than it got. “Use a lock” sounds like advice until both agents acquire different locks and congratulate themselves on coordinating.

I have one Proxmox box and two agents. I want them editing and reading at the same time. I do not need them installing dependencies, building projects, and running heavy test suites simultaneously. The box has a finite amount of enthusiasm, most of it stored in an SSD.

One door for the expensive work

The arrangement is a wrapper around the heavy commands. It acquires an exclusive lock, runs the command, and holds the lock until that command finishes. The other agent waits outside.

On Linux, flock does the uninteresting part. A command such as flock -w 600 /shared/agent-locks/heavy.lock npm run build waits up to ten minutes for the shared lock, then runs the build. If it cannot get the lock, it exits unsuccessfully. That is a queue with a limit, not permission to try the build without it.

The dependency install and the expensive tests use the same lock file. Three separate locks would let all three workloads run together, which is the exact problem I was pretending to solve.

Editing stays outside the queue. So do small checks that do not bother the other container. Serializing every tool call would turn two agents into one agent with an elaborate seating chart.

Shared means the same file

The agents live in separate containers. A file named /tmp/heavy.lock in each container is two files. Matching names do not make shared state, any more than two notebooks labeled “schedule” keep two crews from arriving at the same gate.

Both containers need the same underlying lock file on a shared filesystem with working file-lock semantics. In this setup, that means a host directory mounted into both containers, with permissions set so both can open the file. I keep it separate from either checkout. Cleaning a workspace should not replace the thing coordinating the workspaces.

I also leave the lock file alone after a job. Its existence does not mean a job is running. The lock is held through an open file descriptor, not through the impressive authority of a filename. Deleting and recreating the file can leave one process locking the old file while another locks the replacement.

That is a surprisingly productive way to break a very small mechanism.

The part the agent can still bypass

This is an advisory lock. It does not stop a process that ignores it. A reminder in the agent’s instructions helps, but a common command wrapper is a better place for the rule. The build instructions for a project like my texas-septic-toolkit should point to the agreed entry point, not leave each agent to invent its own courtesy policy.

I want the result to say whether the job ran, waited, or timed out waiting. A lock timeout is not a failed test suite. Mixing those together gives the next agent an invitation to fix code that was never tested.

Boring enough to check

The useful check starts a long-running dummy job through the wrapper, then starts another from the other container. The second must wait. Kill the first and check that the second proceeds. Repeat with the actual build path. Testing two processes in one container would miss the most embarrassing mistake.

I install septic systems near Santo. Coordinating access to one piece of equipment is not a new problem. Apparently I needed a language model to help me rediscover the sign-out sheet.


← back to all posts