Someone else's pid

The check asked whether pid 1400 was alive, not whether it was my server.

On December 3, my Mac mini rebooted twice, first at 9:47 am and again at 9:50 am. The board my agents work from did not come back for the next 13 hours.

One writer #

The board is a small Node server on that mini, where my agents read and write their tasks. Two servers writing the same files would clobber each other, so every start takes a lock first.

The lock is a file that holds the running server’s process id, its pid. A new start reads that pid, asks the system whether the process is alive, and refuses if it is.

In Node that question is one line, process.kill(pid, 0). It sends no signal and only tells you whether any process has that number.

It means any process with that number, not your process.

Pid 1400 #

The lock was written at 9:48, during the short first boot, and it named pid 1400. After the second boot, pid 1400 belonged to appplaceholdersyncd, a macOS daemon with nothing to do with my board.

launchd started the server again about every 5 seconds, all day long: 9,012 failed starts before anyone looked. Each start found pid 1400 alive, printed “pid 1400 already serves this board”, and exited with code 1.

The check asked whether pid 1400 was alive, not whether it was my server. Those two questions always had the same answer, until a reboot handed the number to someone else.

Nobody was told #

The job restarts on exit with a 5-second throttle, so one stale pid became thousands of lines in a log. Nothing reached me or any agent. My nightly planning check found it just after 11 pm, because the board did not answer.

The repair that night was one step: rename the old lock aside. The board answered again at 11:08 pm, about 13 hours after the second boot.

Alive and ours #

The real fix changes what it means for a lock to be held. Under it, a lock counts as held when its pid is alive and running node server.js.

Anything else is stale. The start renames that lock aside and takes a fresh one.

The review #

A separate reviewer agent read the first version and failed it in round one. To read a command line the check calls ps, and when ps itself failed, the check answered “not ours”.

That is the dangerous way for a lock check to be wrong. With the real server alive and ps failing, a second server would take the lock and both would write.

The reviewer showed it by making ps fail on purpose while a live process held the lock. The second start took the lock anyway.

The second version treats an error from ps as a lock that is held. If the check cannot tell whose pid it is, the new start refuses, and refusing is the safe mistake.

It passed the second round that night. The reviewer could not run ps in its own sandbox, so it ran the no-ps case by hand, and the lock stayed held.

Still open #

As of December 4 the reviewed fix is not installed yet, and the live lock check is still the old one. The next reboot that lands on a reused pid can stop the board the same way.

Two limits are written next to the fix:

  • Two starts reclaiming one stale lock at the same moment can both end up holding it.
  • Matching node and server.js is a guess, so a renamed server looks stale and a stranger can look held.

A pid alone does not name a process. A fix merged on December 4 to my terminal app lists each pane’s pids with their start times before it ends them. On macOS, ps -o lstart= gives you that start time, and a reused number cannot match it.