Skip to content

fix(python): preserve socket paths when no child was spawned - #3005

Merged
inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/python-preserve-unstarted-socket
Nov 14, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/python-preserve-unstarted-socket

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

A Python client whose constructor failed before it spawned a server (a missing binary, for example) deleted whatever was at its socket= path: ManagedServer.close() unlinked the path unconditionally. It now removes the path only when a child process was created.

Related issues

Closes #3004.

Type of change

  • fix — bug fix

Cause

ManagedServer.close() (python/src/mlxcel/_server.py) ended with an unconditional os.unlink(self.uds_path) when the path existed. Before a spawn, this manager has put nothing there.

Change

  • The unlink is guarded by proc is not None. Shutdown of a server that was started is unchanged: terminate, wait, join the log thread, remove the socket.
  • python/tests/test_socket_cleanup.py (new): six cases, a regular file and a live Unix listener, each through close() on an unstarted manager, a failed LLM(...) and a failed AsyncLLM(...). They check the file's contents, or make a new connection to the listener, afterwards.

Before / after

Linux aarch64, Python 3.12.3, openai 3.49.0 / httpx 0.28.1, fixtures in a temporary directory.

main This PR
the six new cases 6 failed: the path was removed each time 6 passed
whole suite (-m "not e2e") 57 passed, 2 deselected

A separate probe of the same six situations shows the path removed on main and kept with this change, the live listener still accepting.

Test plan

Run on this PR's source (one commit on 955335a38, tree c2b33dd1e26b).

  • python -m pytest -m "not e2e" in python/: 57 passed, 2 deselected
  • The new test file on main: 6 failed
  • ruff check python, ruff format --check python, mypy python/src: pass
  • Together with five other Python client fixes proposed separately, on Python 3.12.3 and 3.9.25 with the declared lower bounds (openai 1.40.0 / httpx 0.27.0) and with current releases: 101 passed, 2 deselected in each of the four environments. This PR on its own was run on 3.12 with the current releases only
  • The two live-model e2e tests: not run
  • Rust gates: not applicable, no Rust file changes
  • Windows and macOS: not run

Notes for reviewers

  • Scope. Only the cleanup of a manager that never spawned a process. What start() does with an existing socket path is not changed here.
  • Implemented and validated with AI assistance (Claude). Every number above comes from the logs of those runs.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed
  • Updated // Used by: ... comments: not applicable (Python)
  • No secrets, credentials, or .env files committed

`ManagedServer.close()` unlinked the configured Unix socket path whether or
not this manager had ever started a server. A constructor that failed before
the spawn (a missing binary, for example) ran that cleanup too, so an
existing file, or another server's live socket, at the `socket=` path was
deleted by a client that never created it.

The path is now removed only when a child process was created.
@inureyes

Copy link
Copy Markdown
Member

Thanks for the focused fix. Reviewed against #3004: close() now unlinks the socket path only when this manager spawned a child, so a constructor that fails in binary discovery leaves another owner's file or live listener alone, and normal shutdown still removes the socket the manager bound. No security or performance concerns. The remaining case of a spawned child that never bound because something else held the path is what #3016 / #3017 cover.

I merged current main into the branch (no conflicts). Locally on macOS: 81 passed, 2 deselected on Python 3.11 and 3.9; ruff and mypy clean. No further changes were needed. The test (Python 3.9) CI job fails only in setup-python (no 3.9 build on ubuntu-latest, tracked by #3013), so 3.9 was verified locally.

@inureyes
inureyes merged commit bd444a3 into lablup:main Nov 14, 2026
34 of 35 checks passed
@inureyes inureyes added the status:done Completed label Nov 14, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:done Completed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(python): a client that never spawned a server deletes whatever is at its socket path

2 participants