Skip to content

fix(server): let the Unix socket appear only once it listens with its mode - #16

Merged
rosylilly merged 1 commit into
mainfrom
fix/unix-socket-race
Nov 25, 2026
Merged

rosylilly merged 1 commit into
mainfrom
fix/unix-socket-race

Conversation

@rosylilly

Copy link
Copy Markdown
Member

Summary

listenUnix created the socket at its path with net.Listen and ran chmod on it afterwards, so the socket was at the path for a moment before it was ready:

  • Between bind and listen, connections were refused.
  • Between listen and chmod, connections were accepted, with the umask's permissions, not those of -socket-mode.

The second is a security hole, since -socket-mode decides who may connect. The first made the TestUnixSocket and TestToken tests flaky under parallel load.

Changes

  • The socket is created under a temporary name in the same directory (.sock-<random>). It is then chmod-ed and renamed to its path, so it appears at the path only once it is listening with its mode.
  • The rename also replaces a socket left behind by an earlier run in one step. A file that is not a socket is still refused.
  • The listener removes the socket at its path when it closes. The Go listener would remove the temporary name, which is gone by then, so that is turned off with SetUnlinkOnClose(false).
  • New test TestReplacesAStaleSocket: a stale socket is replaced, the new one accepts connections, and neither the socket nor the temporary file is left after shutdown.

Verified

  • go test -race -count=1 ./internal/..., which runs the packages in parallel: the old code failed 9 runs of 10, the new code 0 of 30.
  • scripts/lint.sh passes.

🤖 Generated with Claude Code

… mode

net.Listen made the socket at its path, where it refused connections before its
listen and accepted them, with the permissions of the umask, before its chmod.
The socket is now made under a temporary name in the same directory, chmod-ed,
and renamed to its path, which also replaces a stale socket in one step. The
listener removes the socket at its path when it closes.

The tests of the socket's mode and of the token failed under parallel load
(9 runs of 10 with -race over ./internal/...); with this, 0 of 30.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rosylilly
rosylilly merged commit 9848868 into main Nov 25, 2026
9 checks passed
@rosylilly
rosylilly deleted the fix/unix-socket-race branch November 25, 2026 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant