Skip to content

Fix daemon start on modern FastAPI and stale PID files on Windows - #15

Open
ericwang915 wants to merge 3 commits into
mainfrom
cursor/fix-windows-daemon-start-fastapi-ws-route-5e8b
Open

ericwang915 wants to merge 3 commits into
mainfrom
cursor/fix-windows-daemon-start-fastapi-ws-route-5e8b

Conversation

@ericwang915

Copy link
Copy Markdown
Owner

Fixes #13
Fixes #14

Both reports come from the same repro on Windows 11 / Python 3.14.7 / PythonClaw 0.7.2 / FastAPI 0.165.0: pythonclaw start, then open http://localhost:7788/.

Root cause: #13 (daemon.log traceback, 'FastAPI' object has no attribute 'add_websocket_route')

create_app() registered the chat WebSocket with app.add_websocket_route(...). That method was never FastAPI's; it was a shortcut inherited from starlette.applications.Starlette, and Starlette 1.0 removed it (together with on_event, add_event_handler, ...). FastAPI dropped its starlette<1.0 pin in 0.133 and requires starlette>=1.6 from 0.164, so any fresh install today (the reporter's 0.165.0 resolves Starlette 1.10) hits the AttributeError in the background daemon. The daemon died within its first second, which is why the parent start could never serve the dashboard.

Fix: use FastAPI's own add_api_websocket_route, which exists across the project's declared range (fastapi>=0.100). The channel startup hook in main.py used the sibling @app.on_event("startup") shortcut on the same path; FastAPI still ships its own copy of that with a DeprecationWarning, so it had not broken yet, but it is the same removal class, so create_app() now accepts an on_startup coroutine and wires it through the lifespan API (available since FastAPI 0.93) instead.

Root cause: #14 (terminal traceback, OSError: [WinError 11] from _is_alive)

daemon._is_alive() probed the PID from pythonclaw.pid with os.kill(pid, 0) and only caught ProcessLookupError / PermissionError. On Windows a stale/foreign PID surfaces as a plain OSError carrying a WinError code, so the exception escaped start_daemon() and pythonclaw start crashed with a traceback (the stale PID file was left behind by the #13 crash).

Fix:

  • _is_alive() now catches the whole OSError family and never raises; POSIX keeps using os.kill(pid, 0) with the same PermissionError -> False semantics as before.
  • On Windows, os.kill(pid, 0) is also the wrong tool: per the os.kill docs, any signal other than CTRL_C_EVENT/CTRL_BREAK_EVENT is forwarded to TerminateProcess, so pythonclaw status or a second pythonclaw start would have killed a live daemon had the PID been valid. Windows now probes with OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION) + GetExitCodeProcess == STILL_ACTIVE via ctypes, which is read-only.
  • start_daemon() prints Stale PID file (process N not running). Cleaning up. instead of silently proceeding.
  • stop_daemon() tolerates the same OSError family around its signal sends and uses getattr(signal, "SIGKILL", signal.SIGTERM) since SIGKILL does not exist on Windows (previously an AttributeError on that branch).

No version bump, no CHANGELOG entry (prior bugfix PR #12 followed the same convention; CHANGELOG is updated at release time).

Tests

New tests/test_daemon_and_web.py (19 tests):

  • create_app() builds with DeprecationWarning escalated to an error and registers / and /ws/chat.
  • TestClient: GET / returns 200 HTML, /api/status responds, /ws/chat accepts a connection and replies; the on_startup hook runs exactly once via lifespan.
  • _is_alive: live PID, non-positive PID, ProcessLookupError, PermissionError, and a generic OSError with winerror=11 all return a bool without raising; the win32 branch never calls os.kill.
  • _is_alive_windows: running / exited / OpenProcess failure / GetExitCodeProcess failure / DLL load failure, with a fake kernel32 injected through ctypes.WinDLL.
  • start_daemon, daemon_status, print_status, stop_daemon with a stale PID file whose probe raises the Windows-style OSError: no crash, clear message, PID/meta cleaned up, new daemon spawned once.

Verification done here:

  • Reproduced web\app.py #13 on Linux with Python 3.14.10 + FastAPI 0.165.0 + Starlette 1.10.0 (identical AttributeError from create_app) before the fix.
  • Full suite: 209 passed on Python 3.14.10 / FastAPI 0.165.0 and on Python 3.10.22 / FastAPI 0.100.0 / Starlette 0.27.0 (bottom of the declared range).
  • ruff check pythonclaw/ clean.
  • End-to-end on 3.14 with an isolated PYTHONCLAW_HOME: seeded a stale PID file, ran pythonclaw start (stale file cleaned, daemon spawned), curl http://127.0.0.1:<port>/ returned 200 text/html, then status, a second start ("Already running"), stop, and status again all behaved.

Not reproducible without Windows

  • The exact [WinError 11] and the TerminateProcess side effect of os.kill(pid, 0) cannot be triggered on Linux; the POSIX tests simulate the former by monkeypatching os.kill to raise OSError(winerror=11), and the Windows probe is exercised only against a fake kernel32 (CI has no Windows runner). The ctypes call shapes follow the documented Win32 signatures; a quick pythonclaw start / status / start / stop cycle on a Windows box would be a welcome confirmation.
Open in Web Open in Cursor 

cursoragent and others added 3 commits November 29, 2026 01:50
…an (#13)

Starlette 1.0 removed the add_websocket_route/on_event shortcuts from the
application class. FastAPI >= 0.133 no longer pins Starlette < 1.0, so a
fresh install (e.g. FastAPI 0.165.0) crashes in create_app with
AttributeError: 'FastAPI' object has no attribute 'add_websocket_route'.

Use FastAPI's own add_api_websocket_route (available across the declared
fastapi>=0.100 range) and move the channel startup hook from the
deprecated @app.on_event('startup') to a lifespan passed into create_app.

Co-authored-by: Eric Wang <wangchen2007915@gmail.com>
… Windows (#14)

_is_alive only caught ProcessLookupError/PermissionError, but on Windows
os.kill(pid, 0) on a stale PID raises a plain OSError (WinError 11), so
'pythonclaw start' died with a traceback. Catch the whole OSError family.

On Windows os.kill() is not a probe at all: any signal other than
CTRL_C/CTRL_BREAK is forwarded to TerminateProcess, so 'status' or a second
'start' would kill a live daemon. Probe with OpenProcess +
GetExitCodeProcess there instead; POSIX keeps using os.kill(pid, 0).

Also print a clear message when a stale PID file is cleaned up on start,
and make stop_daemon tolerate OSError / missing SIGKILL on Windows.

Co-authored-by: Eric Wang <wangchen2007915@gmail.com>
…dling (#13, #14)

Co-authored-by: Eric Wang <wangchen2007915@gmail.com>
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.

pythonclaw start - no daemon running web\app.py

2 participants