23:12 on a Tuesday. I was on my own NAS box, cleaning up after a media pipeline, when I typed the inspect line I type ten times a week:
$ docker inspect perm-helper --format \ '{{.State.Status}} {{.RestartCount}} {{.State.ExitCode}} {{.OOMKilled}}' restarting 800 1 false 800 restarts. Not a typo, and not a stale number either — I ran it again two minutes later and it had moved. OOMKilled=false, exit code 1. Whatever this was, it was a clean panic loop, once a minute, for the better part of a day.
The container's name had perm in it. The code inside it was a chmod helper — it exists so one container can fix ownership on files another container writes. The first log lines I scrolled past mentioned chown.
So I did what the name told me to do, and that was my first mistake.
The hour I spent on the wrong layer
I pulled my hair out over uid mappings for a solid hour. Checked how the container was mounted. Checked whether the volume came through with the right ACLs. Ran id inside it, compared the gid against the host directory, read a mountinfo dump I didn't need. For a while I was convinced this was a permission bug wearing a permission name, because that's how my brain works at midnight: the label on the door is the answer.
All of it was wasted. Here's the pair of commands that ended the argument:
$ docker logs perm-helper 2>&1 | grep -c 'Address already in use' 812 $ docker logs perm-helper 2>&1 | grep -ci 'permission denied' 0 812 to 0. The chown code never ran even once, because it lives behind a line the process never reached. I'd been debugging a code path that was unreachable, using a container name as my evidence.
What was actually happening
The daemon binds a unix socket, and the socket lives on a host-mounted volume — deliberately, so a second container on the same box can connect to it. The startup code looked like this:
SOCK = "/vol1/1000/docker/perm-helper/run/perm.sock" def main(): server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server_sock.bind(SOCK) # line 277, the one in all 812 tracebacks server_sock.listen(64) ... No cleanup before the bind. There was a cleanup() on SIGTERM, and it swallowed every OSError it met, so it never told anyone it had failed.
The sequence is boring once you see it. Something SIGKILLs the process (or the host reboots) — the socket file is not a TCP port, it's an inode on that btrfs volume, and nobody unlinks it. New process starts, binds the same path, gets Errno 98. Exits with 1. restart: unless-stopped starts it again. Sixty seconds later: 813.
The detail that should have caught my attention immediately: there is no traceback anywhere in that log before 10:13:41. A binary that panics on its own leaves a first crash. This one didn't. It was killed from outside, and every crash after that was the restart policy re-running a bind that could not possibly succeed.
Reproducing it without Docker at all
I didn't want to trust a one-off. This is pure standard library, Python 3.11.18, no container involved — I ran it again this morning:
first bind ok, file left on disk: True is_socket: True second bind -> 98 Address already in use probe after death -> refused(111) probe while server alive -> live Four lines, and the last two are the interesting ones. connect() tells you which kind of socket file you're looking at: ECONNREFUSED means the file is a fossil, a live listener means you must not touch it. That distinction is the whole fix.
The patch
Before the bind, and it has to be before:
def _reap_stale_socket(path): if not os.path.exists(path): return if not stat.S_ISSOCK(os.lstat(path).st_mode): os.unlink(path) # right name, wrong thing return probe = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) probe.settimeout(0.5) try: probe.connect(path) except OSError as e: if e.errno in (errno.ECONNREFUSED, errno.ENOENT): os.unlink(path) # nobody home: it's garbage else: raise else: sys.exit("FATAL: live instance still holds %s" % path) finally: probe.close() Three branches, and the third one is the one people skip. If a live instance is already listening, the new process must die loudly instead of unlinking a socket that another process is using — otherwise the fix becomes a second bug, and it's the kind that only shows up under a supervisor that starts two copies.
I also changed restart: unless-stopped to restart: on-failure:5 on this service. A socket daemon is not a one-shot job, so no is wrong; unlimited retries are also wrong. Five attempts is enough to survive a dependency ordering hiccup and not enough to burn fourteen hours.
After the restart:
2026-11-11T23:57:52.815 清理死 socket: /vol1/1000/docker/perm-helper/run/perm.sock 2026-11-11T23:57:52.840 perm-helper listening on /vol1/1000/docker/perm-helper/run/perm.sock Status=running, RestartCount=0, five minutes with no new traceback. The first line there is the fix telling on itself: it found a fossil, and it removed it.
Where I actually failed
The patch lives in the container's writable layer. The file it patches comes from a build context on the host, and I never got my hands on that build context — no ssh key to that box, no mount. So the hot fix is running, and the permanent fix does not exist.
Which means the next docker compose up --force-recreate reinstalls the old file, the socket file is still on that volume, and the whole thing starts over. I fixed the symptom at the layer I could reach and wrote down that it dies on the next rebuild. That's not a win, it's a deadline I set for myself and haven't paid.
My take, and I'll take the argument: the restart policy did more damage than the bug. A sixty-second startup bug became fourteen hours and 812 restarts, and the 6530 lines it generated buried the one line that mattered — I had to grep for it, and I only grepped after an hour of guessing. unless-stopped is a footgun on anything that binds a shared resource, because it converts "this can't start" into "this is trying really hard."
I've measured this on exactly one container, with Docker 20.10-era inspect fields and a btrfs volume, so my sample is one night long.
If your daemon binds a unix socket on a shared volume — Python 3.11, Docker 24.x, restart policy of your choice — what does your startup do when connect() fails with EACCES instead of ECONNREFUSED? That's the branch I didn't have to write and don't have a good answer for. If you think unless-stopped earned its keep somewhere, I want the case where it saved you — comments are open.
