From 546a58746967d10996075ca47a7198c1c375fdb2 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Wed, 19 Jul 2017 12:47:01 -0600 Subject: server: Fixed mismatched new/delete; added proper shutdown procedure --- Source/cmConnection.cxx | 5 ----- Source/cmConnection.h | 7 ++++++- Source/cmPipeConnection.cxx | 13 ++++++++----- Source/cmServer.cxx | 23 ++++++++++------------- Source/cmServer.h | 1 + Source/cmServerConnection.cxx | 8 ++++---- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Source/cmConnection.cxx b/Source/cmConnection.cxx index f3fc1ef..bc29e41 100644 --- a/Source/cmConnection.cxx +++ b/Source/cmConnection.cxx @@ -38,11 +38,6 @@ void cmEventBasedConnection::on_read(uv_stream_t* stream, ssize_t nread, delete[](buf->base); } -void cmEventBasedConnection::on_close_delete(uv_handle_t* handle) -{ - delete handle; -} - void cmEventBasedConnection::on_close(uv_handle_t* /*handle*/) { } diff --git a/Source/cmConnection.h b/Source/cmConnection.h index f9d50de..b1b51fe 100644 --- a/Source/cmConnection.h +++ b/Source/cmConnection.h @@ -100,7 +100,12 @@ public: uv_stream_t* WriteStream = nullptr; static void on_close(uv_handle_t* handle); - static void on_close_delete(uv_handle_t* handle); + + template + static void on_close_delete(uv_handle_t* handle) + { + delete reinterpret_cast(handle); + } protected: std::string RawReadBuffer; diff --git a/Source/cmPipeConnection.cxx b/Source/cmPipeConnection.cxx index b18a1d6..9e565f6 100644 --- a/Source/cmPipeConnection.cxx +++ b/Source/cmPipeConnection.cxx @@ -19,7 +19,8 @@ void cmPipeConnection::Connect(uv_stream_t* server) uv_pipe_init(this->Server->GetLoop(), rejectPipe, 0); uv_accept(server, reinterpret_cast(rejectPipe)); - uv_close(reinterpret_cast(rejectPipe), &on_close_delete); + uv_close(reinterpret_cast(rejectPipe), + &on_close_delete); return; } @@ -28,7 +29,8 @@ void cmPipeConnection::Connect(uv_stream_t* server) this->ClientPipe->data = static_cast(this); auto client = reinterpret_cast(this->ClientPipe); if (uv_accept(server, client) != 0) { - uv_close(reinterpret_cast(client), &on_close_delete); + uv_close(reinterpret_cast(client), + &on_close_delete); this->ClientPipe = nullptr; return; } @@ -65,15 +67,16 @@ bool cmPipeConnection::OnConnectionShuttingDown() { if (this->ClientPipe) { uv_close(reinterpret_cast(this->ClientPipe), - &on_close_delete); + &on_close_delete); this->WriteStream->data = nullptr; } - uv_close(reinterpret_cast(this->ServerPipe), &on_close_delete); + uv_close(reinterpret_cast(this->ServerPipe), + &on_close_delete); this->ClientPipe = nullptr; this->ServerPipe = nullptr; this->WriteStream = nullptr; this->ReadStream = nullptr; - return cmConnection::OnConnectionShuttingDown(); + return cmEventBasedConnection::OnConnectionShuttingDown(); } diff --git a/Source/cmServer.cxx b/Source/cmServer.cxx index c3e6811..f14e755 100644 --- a/Source/cmServer.cxx +++ b/Source/cmServer.cxx @@ -416,9 +416,18 @@ static void __start_thread(void* arg) server->Serve(&error); } +static void __shutdownThread(uv_async_t* arg) +{ + auto server = reinterpret_cast(arg->data); + on_walk_to_shutdown(reinterpret_cast(arg), nullptr); + server->StartShutDown(); +} + bool cmServerBase::StartServeThread() { ServeThreadRunning = true; + uv_async_init(&Loop, &this->ShutdownSignal, __shutdownThread); + this->ShutdownSignal.data = this; uv_thread_create(&ServeThread, __start_thread, this); return true; } @@ -464,8 +473,6 @@ void cmServerBase::OnDisconnect() void cmServerBase::OnServeStart() { - uv_signal_start(&this->SIGINTHandler, &on_signal, SIGINT); - uv_signal_start(&this->SIGHUPHandler, &on_signal, SIGHUP); } void cmServerBase::StartShutDown() @@ -485,11 +492,7 @@ void cmServerBase::StartShutDown() } Connections.clear(); - uv_stop(&Loop); - uv_walk(&Loop, on_walk_to_shutdown, nullptr); - - uv_run(&Loop, UV_RUN_DEFAULT); } bool cmServerBase::OnSignal(int signum) @@ -503,12 +506,6 @@ cmServerBase::cmServerBase(cmConnection* connection) { uv_loop_init(&Loop); - uv_signal_init(&Loop, &this->SIGINTHandler); - uv_signal_init(&Loop, &this->SIGHUPHandler); - - this->SIGINTHandler.data = this; - this->SIGHUPHandler.data = this; - AddNewConnection(connection); } @@ -516,7 +513,7 @@ cmServerBase::~cmServerBase() { if (ServeThreadRunning) { - StartShutDown(); + uv_async_send(&this->ShutdownSignal); uv_thread_join(&ServeThread); } diff --git a/Source/cmServer.h b/Source/cmServer.h index 9d8473d..93ac69e 100644 --- a/Source/cmServer.h +++ b/Source/cmServer.h @@ -66,6 +66,7 @@ protected: bool ServeThreadRunning = false; uv_thread_t ServeThread; + uv_async_t ShutdownSignal; uv_loop_t Loop; diff --git a/Source/cmServerConnection.cxx b/Source/cmServerConnection.cxx index 4891131..d6bf1a8 100644 --- a/Source/cmServerConnection.cxx +++ b/Source/cmServerConnection.cxx @@ -62,14 +62,14 @@ bool cmStdIoConnection::OnConnectionShuttingDown() if (usesTty) { uv_read_stop(reinterpret_cast(this->Input.tty)); uv_close(reinterpret_cast(this->Input.tty), - &on_close_delete); + &on_close_delete); uv_close(reinterpret_cast(this->Output.tty), - &on_close_delete); + &on_close_delete); } else { uv_close(reinterpret_cast(this->Input.pipe), - &on_close_delete); + &on_close_delete); uv_close(reinterpret_cast(this->Output.pipe), - &on_close_delete); + &on_close_delete); } return true; -- cgit v0.12