Repository navigation
fix(server): keep a running listener's Unix socket and non-socket paths - #3021
Merged
inureyes merged 3 commits intoNov 14, 2026
Merged
Conversation
`bind_unix` removed whatever existed at the `--host <path>.sock` path before it bound. That is right for the socket an earlier run left behind, and the server leaves its own behind when it stops. It also took the path away from a server still listening on it, which kept running unreachable, and deleted a regular file or a symlink found there. The path is now looked at first. An entry that is not a socket is left alone and the start fails. A socket is probed with one non-blocking connect: only "connection refused" (or a path that vanished meanwhile) reads as nobody there, and that socket is removed and the path bound, as before, so a restart on the same path keeps working. An accepted or queued connection, or a full accept queue on Linux, means a listener; any other result (no permission to connect, a socket of another type) leaves the question open. In both cases the start fails and the socket is left as it is. The check and the removal are two steps: two servers started on one stale path at the same instant can both pass it, and the later bind takes the path, as it always did. macOS answers "connection refused" for a full accept queue as well, so a saturated listener cannot be recognised there.
10 of 14 tasks
The socket probe now reads EINPROGRESS as a listener that answered, so a connection still being set up never makes `bind_unix` remove a live socket. Neither Linux nor macOS reports it for a Unix stream connect today; the case is there so the probe stays conservative. Adds a test that a directory at the socket path is refused and left intact. Validated on macOS (M3 Ultra): the 9 Unix bind tests and the listen tests pass, `cargo clippy --lib --tests -D warnings` is clean, and with a real mlxcel-server a second start on a live socket exits 1 with the in-use error while the first keeps answering `/health`; a restart on the leftover socket after SIGTERM succeeds. Refs lablup#3020
inureyes
pushed a commit
that referenced
this pull request
Nov 14, 2026
…path (#3017) ## Summary `ManagedServer.start()` unlinked whatever existed at the Unix socket path before it spawned the server, so a regular file there was deleted and a running server's socket was taken away from it. It now removes only a socket nobody listens on; anything else at the path fails the start with `MlxcelServerError` and is left as it is. ## Related issues Closes #3016. Stacked on #3005 (the fix for #3004): the first commit of this branch is that PR, and only the second commit is new. Without #3005 the constructor's error cleanup would delete the path that this change has just refused to touch. ## Type of change - [x] `fix`: bug fix ## Cause `python/src/mlxcel/_server.py`, `ManagedServer.start()`: `if self.uds_path is not None and os.path.exists(self.uds_path): os.unlink(self.uds_path)` ran right before `subprocess.Popen`, with no look at what the entry was. ## Change - New `_clear_stale_socket(path)`, called from `start()`: - nothing at the path: nothing to do; - an entry that is not a socket (`lstat`, so a symlink counts as a symlink): `MlxcelServerError`, entry untouched; - a socket: one non-blocking `connect`. `ECONNREFUSED` (or the path vanishing) means nobody listens, and the socket is removed as before. A connection that is accepted or queued, or a full accept queue (`EAGAIN`), means a listener: `MlxcelServerError` ("in use by a running listener"), entry untouched. Any other error, such as `EACCES` or `EPROTOTYPE` for a socket of another type, leaves the question open: `MlxcelServerError` ("Cannot tell whether … is still in use"), entry untouched. - The error is raised before the child is spawned. - `python/README.md`: one paragraph describing this. - `python/tests/test_startup_path.py` (new), each case through `ManagedServer.start()`, `LLM(...)` and `AsyncLLM(...)`: - `test_startup_keeps_an_entry_that_is_not_a_stale_socket`: regular file, directory, dangling symlink, live listener, datagram socket, a leftover socket the user may not connect to, and (Linux) a listener with a full accept queue. The child only writes a marker file, so the test can tell that no process was started and that the entry's device, inode and mode are unchanged; the live listener must still accept a new connection. - `test_startup_replaces_a_socket_nobody_listens_on`: the child binds the path itself and would fail with "address already in use" if the leftover socket were still there. - `test_managed_client_serves_on_a_path_with_a_stale_socket`: a full request through the suite's fake server, sync and async. ## Before / after Linux aarch64. The snippet from the issue, run as written: | | `main` | This PR | |---|---|---| | a listener at the path | path removed; a new connection by path fails with `FileNotFoundError` | `MlxcelServerError: Unix socket path '…' is in use by a running listener; it is left as it is. …`; a new connection succeeds | | a regular file at the path | deleted | `MlxcelServerError: Unix socket path '…' exists and is not a socket; it is left as it is. …`; file kept | New test file, on the parent commit and on this PR, in both environments (Python 3.9.25 with openai 1.40.0 / httpx 0.27.0, and Python 3.12.3 with openai 3.49.0 / httpx 0.28.1): | | parent (#3005) | This PR | |---|---|---| | the 26 new cases | 21 failed, 5 passed (the five stale-socket cases, which already worked) | 26 passed | | whole suite (`-m "not e2e"`) | 57 passed, 2 deselected (without the new file) | 83 passed, 2 deselected | Three mutations of the fix, same two environments: with the stale socket recognised but not removed, the three `test_startup_replaces_a_socket_nobody_listens_on` cases fail; with an undetermined probe result treated as stale, the six datagram-socket and no-permission cases fail; with a full accept queue treated as stale, the three full-backlog cases fail. ## Test plan Run on this PR's source (the second commit of the branch; tree `bedb3af550cc`). - [x] `python -m pytest -m "not e2e"` in `python/`: 83 passed, 2 deselected, on Python 3.9.25 with the declared lower bounds and on Python 3.12.3 with current releases - [x] The new file on the parent commit: 21 failed, 5 passed, both environments - [x] The three mutations above fail the cases named, both environments - [x] `ruff check python`, `ruff format --check python`: pass; `mypy python/src` (Python 3.9 target): pass - [x] What a non-blocking `connect` returns for each kind of socket, measured on Linux 7.0 (aarch64) and on macOS 26.2 with the standard library: stale `ECONNREFUSED`, live listener connects, datagram socket `EPROTOTYPE`, a socket with mode 000 `EACCES` whether or not it is live; a full accept queue gives `EAGAIN` on Linux and `ECONNREFUSED` on macOS - [ ] The test suite on macOS and Windows: not run (the new tests skip on Windows, where the client uses TCP) - [ ] The two live-model e2e tests: not run - [ ] Rust gates: not applicable, no Rust file changes ## Notes for reviewers - **Behavior change.** A start on a path that holds a file or a live socket used to delete it and go on; it now raises. A start on a leftover socket from a crashed server works as before. - **Limits.** The check and the removal are two steps, so this is not a lock: two starts on the same stale path at the same moment can still both proceed. A socket that is bound but not yet listening answers `ECONNREFUSED` and reads as stale. On macOS a listener whose accept queue is completely full answers `ECONNREFUSED` as well, so it cannot be told from a stale socket there and loses its path as it does on `main`; the full-backlog test is Linux-only for that reason. - The Rust server's bind removes an existing path unconditionally as well; that is not changed here. #3021 applies the same rule (stale, in use, unknown from one non-blocking connect) to the Rust `bind_unix`. - **Maintainer follow-up (2144c70), found running the suite on macOS.** A child that exits while the readiness probe is queued makes httpx raise `RemoteProtocolError` on macOS (Linux reports `ReadError`); `_probe_once` did not catch it, so `start()` leaked a raw httpx exception instead of `MlxcelServerError`. It is now treated as "still starting" and the liveness check reports the dead child. The live-listener fixture also listens with a backlog of 8, since the probe's queued connection filled a backlog of 1 and macOS refused the test's own reachability check. After merging `main`: 104 passed, 2 deselected on macOS with Python 3.11 and 3.9. ## Checklist - [x] PR title uses a Conventional Commits prefix - [x] One logical change per PR - [x] Updated `docs/`: `python/README.md` - [ ] Updated `// Used by: ...` comments: not applicable (Python) - [x] No secrets, credentials, or `.env` files committed
Member
|
Thanks for the careful fix and the measurements, @Jaeyeong-CHOI. I reviewed it against #3020 and ran it on macOS (M3 Ultra), which covers the gap in your test plan. Pushed on top (df89278, plus a merge of current
macOS validation:
Security and performance: the probe is one non-blocking connect at startup on a path the operator chose, nothing on the request path. The check-then-remove race is documented and no worse than before. Windows is unaffected: every new item is The error wording matches the Python client in #3017. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
bind_unixremoved whatever existed at the--host <path>.sockpath before it bound. That is right for the socket an earlier run left behind (the server leaves its own behind when it stops). It also took the path from a server still listening on it, which kept running unreachable, and deleted a regular file or a symlink found there. The path is now looked at first: only a socket nobody listens on is replaced.Related issues
Closes #3020.
Type of change
fix— bug fixCause
src/server/listen.rs,bind_unix:if path.exists() { std::fs::remove_file(path) }, with no look at what the entry is or whether anything answers on it.Change
bind_unixtakessymlink_metadataof the path. Nothing there: bind. Not a socket: fail, the entry is left as it is. A socket: probe it; remove it and bind as before only if the probe says nobody is there, otherwise fail and leave it.probe_unix_socket(new) makes one non-blocking connection attempt and returns one of three answers. Stale: "connection refused", or the path vanished meanwhile. In use: the connection was accepted or queued, or the accept queue is full (EAGAINon Linux); the error says "in use by a running listener". Unknown: anything else, such asEACCESorEPROTOTYPEfor a socket of another type; the error says "cannot tell whether the socket is still in use" and carries the OS error. Non-blocking, because a blocking connect to a full accept queue waits for the listener to accept.docs/server-features.mdnext to the.socksentence.Before / after
GB10 host, release builds of
955335a38(main, the base of this branch) and of this PR. No model is loaded (serve --models-dir <empty> --no-models-autoload --offline --no-webui --host <path> --port 0); every fixture lives in a temporary directory and every child process was reaped.main/health200/health200/health200Two real servers on one path:
main/healthon the path while both run/healthon the pathTest plan
Run on this PR's commit (
155ac62cb, one commit on955335a38, treefcec3af3c201) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warnings(the Linux spelling of the template's line)cargo build --release --features cudacargo test --profile test-fast --features cuda --lib server::listen::listen_unix_tests -- --test-threads=1: 9 passed. The cases: the server's own leftover socket, a socket whose owner is gone, a live listener (which still accepts afterwards), a listener with a full accept queue (Linux only, see the notes), a regular file, a live and a dangling symlink, a bound datagram socket, a leftover socket the user may not connect to, a new nested pathbind_unixput back as it is onmain, 6 of the 9 fail (regular file, symlinks, live listener, full accept queue, datagram socket, no permission) and the 3 that describe whatmainalready did right pass (own leftover, owner gone, new path)make verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 186 of 187 test binaries pass (15502 tests passed, 1 failed, 496 ignored), the 9 new tests among them. The one failure isnative_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a cleanmain(order-dependent; test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)ci.ymljobs a pull request from a fork does not get, run by hand:clippy(cargo clippy -p mlxcel --lib --tests -- -D warnings, default features), the 9 new tests on that CPU build, andxla-compile(cargo check --features cuda,xla-iree --all-targets,cargo check --no-default-features --features xla-diagnostics --all-targets): all passmake verify-python-tooling(219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refscargo deny check: not run and not applicable (no manifest, lockfile ordeny.tomlchange;socket2is already a dependency withall)connectanswers for each kind of socket, measured with the Python standard library on Linux 7.0 (aarch64) and macOS 26.2: staleECONNREFUSED, bound but not listeningECONNREFUSED, live listener connects, datagram socketEPROTOTYPE, mode 000EACCES(live or stale); full accept queueEAGAINon Linux andECONNREFUSEDon macOSNotes for reviewers
listenyet also answers "connection refused".tokio'sUnixListener::bindbinds and listens in one call, so for mlxcel's own servers that state is not observable from outside; a foreign program that binds early and listens much later could still lose its path, as it does today.EAGAIN, which the probe counts as in use (tested). macOS answers "connection refused", the same as for a stale socket, so a listener that is saturated at that moment is treated as stale there and loses its path, as it does onmain. The test for this case is#[cfg(target_os = "linux")]for that reason, and the docs paragraph says so.mainremoved a leftover socket owned by another user if the directory allowed it; the probe getsEACCESfor such a socket whether it is live or not, so the start now fails with the "cannot tell" message and the user removes it or picks another path.Checklist
docs/:docs/server-features.md// Used by: ...comments: on the new function.envfiles committed