Skip to content

Use send_ctrlc to terminate child app process. - #5870

Open
azriel91 wants to merge 4 commits into
DioxusLabs:mainfrom
azriel91:bugfix/5157/gracefully-terminate-server
Open

azriel91 wants to merge 4 commits into
DioxusLabs:mainfrom
azriel91:bugfix/5157/gracefully-terminate-server

Conversation

@azriel91

Copy link
Copy Markdown
Contributor
  • Closes dx serve does not gracefully shutdown server on windows #5157.

  • Also suppresses this warning when stopping the dx cli:

    WARN Failed to send devserver message to client (build_id: Some(BuildId(1)), pid: Some(24552)): IO error: An existing connection was forcibly closed by the remote host. (os error 10054)
    

@nicoburns

Copy link
Copy Markdown
Member

This has issues for Dioxus Desktop on Windows:

  1. Windows desktop regression risk (main concern). Old path: taskkill /PID (no /F) sends WM_CLOSE to the app window → tao close → persist_window_state(). New path: CTRL_BREAK_EVENT → Rust has no default handler → ExitProcess, so no WM_CLOSE, no state save. connect_preserve_window_state_handler in app.rs:655-680 is #[cfg(unix)] only ("TODO: make this work on windows"), so nothing catches the console event. Worse, for apps built with windows_subsystem = "windows" (no console) GenerateConsoleCtrlEvent just fails; the error is ignored (_ = process.terminate()), then after the 1s wait kill_on_drop does a hard TerminateProcess. Suggested fixes: (a) on Windows fall back to taskkill when terminate() errs, or send both; and/or (b) add a SetConsoleCtrlHandler (the ctrlc crate handles CTRL_BREAK) in desktop's handler that fires UserWindowEvent::Shutdown — that would actually resolve the TODO and make the fix complete for desktop, not just fullstack servers/web (which is what dx serve does not gracefully shutdown server on windows #5157 is about).

  2. Nit: the doc comment in soft_kill still says "join the wait with a 100ms timeout" while the sleep is 1000ms (pre-existing); the new kill_on_drop comment is accurate since process is dropped at the end of soft_kill. The server.rs change (warn→debug for Shutdown) is fine; slightly cleaner would be to not log at all for Shutdown, but no objection.

`send_ctrlc` sends `CTRL_BREAK_EVENT` on Windows, which Rust handles with
`ExitProcess`, so desktop apps no longer received `WM_CLOSE` and skipped
saving their window state. It also does not reach apps without a console.

* CLI: also run `taskkill /PID` (no `/F`) on Windows to send `WM_CLOSE`,
  hiding its expected "success" / "not found" output.
* Desktop: handle Ctrl-C / Ctrl-Break on Windows via `ctrlc` and trigger
  `UserWindowEvent::Shutdown`.
* Fix stale timeout comment in `soft_kill`.
@azriel91
azriel91 force-pushed the bugfix/5157/gracefully-terminate-server branch from c68c6ec to c980db2 Compare September 30, 2026 18:05
@azriel91

Copy link
Copy Markdown
Contributor Author

cool; I think those are fixed -- I tested using dx serve --hot-patch false --example window_zoom --desktop --features=desktop and ctrl-c'ed in the dx terminal.

I suppressed some taskkill output, though it only suppresses English (not sure if taskkill outputs other languages in different language Windows).

This branch has not been deployed

No deployments
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.

dx serve does not gracefully shutdown server on windows

2 participants