RFC 0053 — Exit hooks: work done however the app ends
Status: implemented (2026-11-24) · Target: 0.54.0 · builds on windows & lifecycle, sidecar, updater · see Decisions
Summary
An app that starts something outside itself has to end it when it quits:
an ssh -N tunnel, a database server it launched, a lock file, a
half-written temp file. Elyra gives it no place to do that. The event loop
(tao) ends the process without running destructors, and the updater's
relaunch calls process::exit(0) directly. So whatever the app started
lives on after it, as ElyraSQL's tunnels do on macOS and Windows.
This RFC adds exit hooks. They run once, whichever way the process ends:
the last window closed, Quit, a relaunch into an update, the end of an
--mcp run, or a signal from the OS or a terminal.
App::new()
.on_exit(|ctx| ctx.get::<Tunnels>().close_all())
.run()?;
// A command-line tool built on the same crates, with no window (RFC 0054).
fn main() -> anyhow::Result<()> {
let _exit = elyra::exit::guard(); // hooks on drop, and on Ctrl-C/SIGTERM
elyra::exit::on_exit(tunnel::kill_all);
run_cli()
}
- One list, run once:
App::on_exitand the freeelyra::exit::on_exitadd to the same process-wide list. The first way out runs it, and every later way finds it empty. - Every way out the framework controls: the event loop's end, the
updater's relaunch,
--mcp's end, andSIGINT/SIGTERM/SIGHUP(Unix) and Ctrl-C, Ctrl-Break and closing the console (Windows), each followed by the exit it would have made. - The framework's own children too: sidecars the app spawned with
Sidecarare ended by a hook of the framework's, sosidecarstops leaving processes behind.
Motivation
- ElyraSQL, moving from its fork onto the framework, keeps its SSH
tunnels in
ssh -Nchildren. Its fork has a globalon_exit(fn())run onLoopDestroyed, and its updater calls the hooks before it relaunches. Without that here, moving to the framework would leave tunnels running after every quit. Sidecarhas the same bug today: its children are tokioChildren withoutkill_on_drop, and nothing drops them anyway, so a sidecar outlives the app that started it.Dropis the usual answer and doesn't work here. tao'srunnever returns, andprocess::exitskips destructors, so an app can't rely on one.
Current state (0.53)
shell/webview.rs:ControlFlow::Exitwhen the last window closes (lifetime::ends) and on Quit. tao then sendsEvent::LoopDestroyedand exits the process. Nothing handlesLoopDestroyed.- The updater:
updater.rs(apply_and_relaunch),updater/runtime.rsandshell/update.rsend withstd::process::exit(0)after arranging the relaunch. App::run_mcp(--mcp) returns when stdin closes. Its caller's destructors run, but there's no hook for state that lives elsewhere.- No signal handling: Ctrl-C in a terminal, or
kill, ends the process at once. Sidecarspawns withtokio::process::Command, withoutkill_on_drop(true), and kills a child only onsidecar.kill(id).
Design
The list
pub mod exit {
/// Run `hook` once when the process ends, whichever way. Hooks run in
/// the reverse order they were added (the last thing set up is the
/// first torn down), on the thread that's ending the process.
pub fn on_exit(hook: impl FnOnce() + Send + 'static);
/// Run the hooks now, once: what every way out calls. A second call
/// finds the list empty.
pub fn run();
/// For a `main` without `App::run`: runs the hooks when dropped, and
/// installs the signal handlers below. Hold it for the life of `main`.
pub fn guard() -> ExitGuard;
}
impl App {
/// `on_exit` with the app's `Ctx`, added when the app starts.
pub fn on_exit(self, hook: impl FnOnce(&Ctx) + Send + 'static) -> Self;
}
A hook that panics is caught. The error is logged and the rest still run.
Where it's called
| How the process ends | Where exit::run() is called |
|---|---|
| Last window closed, Quit, the tray's Quit, macOS ⌘Q | Event::LoopDestroyed in the event loop |
| A relaunch into an update | before each process::exit(0) in the updater |
--mcp ends (stdin closed) |
when run_mcp returns |
SIGINT, SIGTERM, SIGHUP (Unix) |
a signal thread: run the hooks, then exit with 128 + the signal |
| Ctrl-C, Ctrl-Break, console closed (Windows) | SetConsoleCtrlHandler: run the hooks, then exit |
A CLI's main returns or unwinds |
ExitGuard's Drop |
The signal handlers are installed by App::run and by exit::guard(),
and not by a library that only links Elyra. An app that installs its own
handlers can turn them off (App::exit_on_signals(false), open
question 3).
What it can't catch
SIGKILL, a force-quit from Activity Monitor or Task Manager, a crash, and
a power loss end the process with no chance to run anything. On Linux a
child can still be ended with the parent through PR_SET_PDEATHSIG. The
docs say this, and point a process that must never be left behind at its
own watchdog: the child exits when its stdin closes, as ssh -N can be set
up to.
Windows log-off and shutdown reach a GUI app (with no console) as
WM_QUERYENDSESSION/WM_ENDSESSION to its windows. tao doesn't pass them
on today, so they're open question 2.
Sidecars
Sidecar spawns with kill_on_drop(true) and registers one framework
hook that kills the children still running. So a sidecar ends with the
app, as the docs always implied.
Testing
- Unit: order (reverse), once only (
runtwice, a hook added duringrun), a panicking hook not stopping the others,App::on_exitgetting the app'sCtx. - Integration (Unix): a test binary that registers a hook writing a
file, then is sent
SIGTERM. The file is there, and the exit code is 143. The same withExitGuardon a normal return. - Windows: a console child sent
CTRL_BREAK_EVENT(GenerateConsoleCtrlEvent), checked the same way. - The running app in CI: the example app adds a hook that writes a marker file. The macOS, Linux and Windows walk-throughs quit it as they do now and check the file.
- Sidecar: a spawned
sleep 60is gone after the hooks run.
Implementation plan
framework/src/exit.rs: the list,run,guard, the Unix signal thread (signal-hook, or tokio'ssignalfeature), and the Windows console handler.- The calls:
LoopDestroyed, the updater's three exits,run_mcp.App::on_exit, andexit_on_signalsif decided. Sidecar:kill_on_dropand the framework's hook.- Tests, the example's marker and CI checks, docs (
windows.md#lifecycle,sidecar.md, a shortexit.md), CHANGELOG.
Open questions
Answered in Decisions.
- Hook signature:
FnOncewith or withoutCtx? Recommendation: both, as above.elyra::exit::on_exittakes a plainFnOnce()so a CLI and a library can use it without anApp.App::on_exithands over the app'sCtx, which is where an app keeps what it has to close. - Windows log-off and shutdown for a GUI app: handle
WM_ENDSESSIONthrough a hidden message window of our own, or wait for tao? Recommendation: our own message-only window, created on the event loop's thread, that runs the hooks onWM_ENDSESSION(wParam true). Without it, a tunnel survives every log-off, which is one of the cases ElyraSQL names. If it turns out to be more than a small, contained module, it moves to its own change and the docs say so. - Signal handlers by default?
Recommendation: yes in
App::runandexit::guard(), withApp::exit_on_signals(false)for an app that installs its own. Ctrl-C inrata devthen ends the app the way Quit does, which is what a developer expects. - A time limit for the hooks? A hook that hangs keeps the process alive. Recommendation: no limit in the framework. Hooks run in order on the exiting thread, and the docs ask them to be quick and to signal children rather than wait for them. A limit would mean killing a thread half-way through closing something, which is worse than a slow quit.
- Async hooks?
Recommendation: no. At exit the runtime may already be shutting down.
A hook that needs async work does
rt.block_onon a handle it kept, or sends a signal and returns.
Not in this RFC
- Asking before quitting ("unsaved changes"): that's
beforecloseon the page, and a window's close request. - Restoring state at the next launch.
- The
guifeature a CLI needs to build without tao and wry: RFC 0054.
Alternatives considered
Dropon a value in the container: never dropped, because the process exits without unwinding.- Only
LoopDestroyed, as the fork does: it misses the updater's relaunch (the fork calls the hooks there by hand), signals, and a CLI. PR_SET_PDEATHSIGeverywhere: Linux only, and only for children.
As built
As designed, with these specifics:
framework/src/exit.rs: a process-wide list ofFnOnce() + Send.run()takes it and runs it last first, again while hooks added during the run leave it non-empty; a panicking hook is caught and logged.App::on_exithooks are added inprepare, each with a clone of the app'sCtx.- Unix signals: a thread waits on
SIGINT,SIGTERMandSIGHUP(signal-hook), runs the hooks, and exits with 128 + the signal. - Windows:
SetConsoleCtrlHandlerfor Ctrl-C and Ctrl-Break (exitSTATUS_CONTROL_C_EXIT) and for close, log-off and shutdown (exit 0). The session's end comes from a hidden top-level window, not a message-only one as the design said: Windows broadcastsWM_QUERYENDSESSION/WM_ENDSESSIONto top-level windows only. The window is never shown, and lives on a thread of its own with its own message loop. - Where
run()is called:Event::LoopDestroyed, before each of the updater's threeprocess::exit(0)calls, and afterrun_mcpreturns.App::runinstalls the signal handlers unlessexit_on_signals(false);exit::guard()always does. Sidecar: children spawn withkill_on_drop(true), and their pids are kept while they run. A framework hook kills them synchronously by pid (kill(SIGKILL)on Unix,TerminateProcesson Windows), since their tasks wouldn't run again before the process ends.- Tests: the hooks' order, once-only semantics, a hook added during a
run, a panic, and a guard (
exit.rs); a sidecar child ended by pid (sidecar.rs);framework/tests/exit_hooks.rsstarts its own binary as a child with a hook, sendsSIGTERM(Unix) orCTRL_BREAK_EVENT(Windows), and checks the marker and the exit code; a guard inmain. The example app writesELYRA_EXIT_MARKERin its hook, and CI checks it after ending the app withSIGTERM(Linux, macOS) and after closing its window (Windows). Windows log-off isn't driven in CI.
Decisions
- Both signatures:
elyra::exit::on_exit(FnOnce())for a CLI or a library,App::on_exit(FnOnce(&Ctx))for an app. - Windows log-off and shutdown through a message-only window of our
own that runs the hooks on
WM_ENDSESSION; if it isn't a small, contained module, it moves to its own change and the docs say so. - Signal handlers by default in
App::runandexit::guard(), withApp::exit_on_signals(false)to leave them to the app. - No time limit: hooks run in order on the exiting thread, and the docs ask them to be quick.
- No async hooks.