From 44ac0eead7329ff53e4bab14209916ce479ae86f Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 2 Sep 2026 23:44:26 +0200 Subject: [PATCH] Add ctrl-c handler to `run_subprocess` Catch ctrl-c in `odin run` to shutdown child process without taking down the compiler also. Fixes #7499 --- src/main.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 9bea6416f..3605717e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -194,6 +194,19 @@ extern char **environ; #endif #if defined(GB_SYSTEM_WINDOWS) +PROCESS_INFORMATION pi = {0}; + +BOOL WINAPI run_subprocess_ctrl_c_handler(DWORD signal) { + switch (signal) { + case CTRL_C_EVENT: + // Caught ctrl-c event from child process. + TerminateProcess(pi.hProcess, 0); + return true; + default: + return false; + } +} + int run_subprocess(String const &exe_name, wchar_t *after_double_dash_raw) { gbAllocator a = heap_allocator(); @@ -224,9 +237,10 @@ int run_subprocess(String const &exe_name, wchar_t *after_double_dash_raw) { cmd_line[n] = '\0'; STARTUPINFOW start_info = {gb_size_of(STARTUPINFOW)}; - PROCESS_INFORMATION pi = {0}; + int exit_code = 0; + SetConsoleCtrlHandler(run_subprocess_ctrl_c_handler, true); if (CreateProcessW(nullptr, cmd_line, nullptr, nullptr, true, 0, nullptr, nullptr, &start_info, &pi)) { @@ -241,6 +255,8 @@ int run_subprocess(String const &exe_name, wchar_t *after_double_dash_raw) { gb_free(a, cmd_line_utf8.text); exit_code = -1; } + SetConsoleCtrlHandler(run_subprocess_ctrl_c_handler, false); + return exit_code; } #else