Skip to content

Commit 80f7eba

Browse files
committed
SIGINT handler
1 parent c27a9dc commit 80f7eba

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/httpserver_extension.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,25 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
383383
});
384384

385385
string host_str = host.GetString();
386-
386+
387387
const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
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+
signal(SIGINT, [](int) {
392+
if (global_state.server) {
393+
global_state.server->stop();
394+
}
395+
});
396+
391397
// Run the server in the same thread
392398
if (!global_state.server->listen(host_str.c_str(), port)) {
393399
global_state.is_running = false;
394400
throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port));
395401
}
402+
403+
// The server has stopped (due to CTRL-C or other reasons)
404+
global_state.is_running = false;
396405
} else {
397406
// Run the server in a dedicated thread (default)
398407
global_state.server_thread = make_uniq<std::thread>([host_str, port]() {

0 commit comments

Comments
 (0)