@@ -383,12 +383,41 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
383383 });
384384
385385 string host_str = host.GetString ();
386- global_state.server_thread = make_uniq<std::thread>([host_str, port]() {
386+
387+ const char * run_in_same_thread_env = std::getenv (" DUCKDB_HTTPSERVER_FOREGROUND" );
388+ bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string (run_in_same_thread_env) == " 1" );
389+
390+ if (run_in_same_thread) {
391+ #ifdef _WIN32
392+ throw IOException (" Foreground mode not yet supported on WIN32 platforms." );
393+ #else
394+ // POSIX signal handler for SIGINT (Linux/macOS)
395+ signal (SIGINT, [](int ) {
396+ if (global_state.server ) {
397+ global_state.server ->stop ();
398+ }
399+ global_state.is_running = false ; // Update the running state
400+ });
401+
402+ // Run the server in the same thread
387403 if (!global_state.server ->listen (host_str.c_str (), port)) {
388404 global_state.is_running = false ;
389405 throw IOException (" Failed to start HTTP server on " + host_str + " :" + std::to_string (port));
390406 }
391- });
407+ #endif
408+
409+ // The server has stopped (due to CTRL-C or other reasons)
410+ global_state.is_running = false ;
411+ } else {
412+ // Run the server in a dedicated thread (default)
413+ global_state.server_thread = make_uniq<std::thread>([host_str, port]() {
414+ if (!global_state.server ->listen (host_str.c_str (), port)) {
415+ global_state.is_running = false ;
416+ throw IOException (" Failed to start HTTP server on " + host_str + " :" + std::to_string (port));
417+ }
418+ });
419+ }
420+
392421}
393422
394423void HttpServerStop () {
0 commit comments