From 6c85573d12f1c0a8a6fa8a14e1f531ca8920c6c8 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 4 Aug 2026 11:55:20 +0200 Subject: [PATCH] Add comments explaining Windows Service console attachment flow in os_term --- os/mingw.c | 8 +++++--- os/os.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/os/mingw.c b/os/mingw.c index afb23ff4..3b1611de 100644 --- a/os/mingw.c +++ b/os/mingw.c @@ -2801,6 +2801,8 @@ pid_t os_wait(pid_t pid, int* status) * Child processes spawned via os_spawn() with CREATE_NO_WINDOW have an invisible * console host allocated by the OS. Attaching to the child's console allows * sending a CTRL_BREAK_EVENT signal so SnapRAID CLI catches it and flushes state/checkpoints. + * When called from a background Windows Service (which has no console), FreeConsole() + * detaches from the child's console host and leaves the service in its original state. */ int os_term(pid_t pid) { @@ -2812,10 +2814,10 @@ int os_term(pid_t pid) return -1; } - /* detach from current console (if any) */ + /* detach from current console (if any; background service has no console) */ FreeConsole(); - /* attach to the child's console */ + /* attach to the child's invisible console host */ if (!AttachConsole(id)) { /* fallback: terminate process forcibly if console attachment fails */ if (!TerminateProcess(h, 1)) { @@ -2831,7 +2833,7 @@ int os_term(pid_t pid) /* this will now reach the child's SetConsoleCtrlHandler */ GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, id); - /* clean up */ + /* detach from child console, returning calling service process to its console-less state */ FreeConsole(); return 0; diff --git a/os/os.h b/os/os.h index 6e76b205..5495c180 100644 --- a/os/os.h +++ b/os/os.h @@ -134,7 +134,7 @@ pid_t os_spawn(char** argv, int* stdout_read_fd, int* stderr_read_fd, const char pid_t os_wait(pid_t pid, int* status); /** - * Terminate gracefully a process. + * Terminate gracefully a process. Intended for daemon/service processes controlling child processes. * The caller must subsequently call os_wait() to reap process resources and close handles. * \param pid Process ID of the process to terminate. * \return 0 on success, -1 on failure.