Skip to content

Commit d6e0d88

Browse files
authored
Windows SIGINT handler
1 parent a66d9ef commit d6e0d88

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/httpserver_extension.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,18 +388,40 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
388388
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");
389389

390390
if (run_in_same_thread) {
391+
#ifdef _WIN32
392+
// Windows-specific handler for Ctrl+C
393+
BOOL WINAPI consoleHandler(DWORD signal) {
394+
if (signal == CTRL_C_EVENT) {
395+
if (global_state.server) {
396+
global_state.server->stop();
397+
}
398+
global_state.is_running = false; // Update the running state
399+
return TRUE; // Indicate that the signal was handled
400+
}
401+
return FALSE;
402+
}
403+
404+
// Set the console control handler for Windows
405+
if (!SetConsoleCtrlHandler(consoleHandler, TRUE)) {
406+
std::cerr << "Error setting up signal handler" << std::endl;
407+
throw IOException("Failed to set up signal handler");
408+
}
409+
#else
410+
// POSIX signal handler for SIGINT (Linux/macOS)
391411
signal(SIGINT, [](int) {
392412
if (global_state.server) {
393413
global_state.server->stop();
394414
}
415+
global_state.is_running = false; // Update the running state
395416
});
396-
417+
#endif
418+
397419
// Run the server in the same thread
398420
if (!global_state.server->listen(host_str.c_str(), port)) {
399421
global_state.is_running = false;
400422
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
401423
}
402-
424+
403425
// The server has stopped (due to CTRL-C or other reasons)
404426
global_state.is_running = false;
405427
} else {

0 commit comments

Comments
 (0)