@@ -388,18 +388,40 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
388
388
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string (run_in_same_thread_env) == " 1" );
389
389
390
390
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)
391
411
signal (SIGINT, [](int ) {
392
412
if (global_state.server ) {
393
413
global_state.server ->stop ();
394
414
}
415
+ global_state.is_running = false ; // Update the running state
395
416
});
396
-
417
+ #endif
418
+
397
419
// Run the server in the same thread
398
420
if (!global_state.server ->listen (host_str.c_str (), port)) {
399
421
global_state.is_running = false ;
400
422
throw IOException (" Failed to start HTTP server on " + host_str + " :" + std::to_string (port));
401
423
}
402
-
424
+
403
425
// The server has stopped (due to CTRL-C or other reasons)
404
426
global_state.is_running = false ;
405
427
} else {
0 commit comments