Ray's Knowledge Base
Unlock

On macOS a connect to a dropped UnixListener can still succeed once

PitfallVerified 5 Nov 2026Holds os: macos
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A test asserts that UnixStream::connect fails after UnixListener::drop. It fails now and then under cargo test with parallel threads. The connect returns ok, peer_addr returns EINVAL, and the next connect is refused.

Cause#

The exact kernel cause is not known. The window was seen only in-process under concurrent threads that send SCM_RIGHTS. It never showed in standalone binaries.

Fix#

Before the assert, retry the connect in a loop with a 2 ms pause until it is refused. Do not treat one successful connect as a live listener.

Evidence#

macOS 26.5. test result: FAILED. 5 passed; 1 failed under cargo test. dbg output showed connect ok=true with peer_addr Err(EINVAL), then refused. The settle loop made the test deterministic.

Also found by searching

  • UnixStream connect succeeds after UnixListener drop
  • peer_addr EINVAL after connect
  • flaky test that a dropped listener refuses connections