server: Fixed mismatched new/delete; added proper shutdown procedure

This commit is contained in:
Justin Berger 2017-07-19 12:47:01 -06:00 committed by Brad King
parent 9bad65940c
commit 546a587469
6 changed files with 29 additions and 28 deletions

View File

@ -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*/)
{
}

View File

@ -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 <typename T>
static void on_close_delete(uv_handle_t* handle)
{
delete reinterpret_cast<T*>(handle);
}
protected:
std::string RawReadBuffer;

View File

@ -19,7 +19,8 @@ void cmPipeConnection::Connect(uv_stream_t* server)
uv_pipe_init(this->Server->GetLoop(), rejectPipe, 0);
uv_accept(server, reinterpret_cast<uv_stream_t*>(rejectPipe));
uv_close(reinterpret_cast<uv_handle_t*>(rejectPipe), &on_close_delete);
uv_close(reinterpret_cast<uv_handle_t*>(rejectPipe),
&on_close_delete<uv_pipe_t>);
return;
}
@ -28,7 +29,8 @@ void cmPipeConnection::Connect(uv_stream_t* server)
this->ClientPipe->data = static_cast<cmEventBasedConnection*>(this);
auto client = reinterpret_cast<uv_stream_t*>(this->ClientPipe);
if (uv_accept(server, client) != 0) {
uv_close(reinterpret_cast<uv_handle_t*>(client), &on_close_delete);
uv_close(reinterpret_cast<uv_handle_t*>(client),
&on_close_delete<uv_pipe_t>);
this->ClientPipe = nullptr;
return;
}
@ -65,15 +67,16 @@ bool cmPipeConnection::OnConnectionShuttingDown()
{
if (this->ClientPipe) {
uv_close(reinterpret_cast<uv_handle_t*>(this->ClientPipe),
&on_close_delete);
&on_close_delete<uv_pipe_t>);
this->WriteStream->data = nullptr;
}
uv_close(reinterpret_cast<uv_handle_t*>(this->ServerPipe), &on_close_delete);
uv_close(reinterpret_cast<uv_handle_t*>(this->ServerPipe),
&on_close_delete<uv_pipe_t>);
this->ClientPipe = nullptr;
this->ServerPipe = nullptr;
this->WriteStream = nullptr;
this->ReadStream = nullptr;
return cmConnection::OnConnectionShuttingDown();
return cmEventBasedConnection::OnConnectionShuttingDown();
}

View File

@ -416,9 +416,18 @@ static void __start_thread(void* arg)
server->Serve(&error);
}
static void __shutdownThread(uv_async_t* arg)
{
auto server = reinterpret_cast<cmServerBase*>(arg->data);
on_walk_to_shutdown(reinterpret_cast<uv_handle_t*>(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);
}

View File

@ -66,6 +66,7 @@ protected:
bool ServeThreadRunning = false;
uv_thread_t ServeThread;
uv_async_t ShutdownSignal;
uv_loop_t Loop;

View File

@ -62,14 +62,14 @@ bool cmStdIoConnection::OnConnectionShuttingDown()
if (usesTty) {
uv_read_stop(reinterpret_cast<uv_stream_t*>(this->Input.tty));
uv_close(reinterpret_cast<uv_handle_t*>(this->Input.tty),
&on_close_delete);
&on_close_delete<uv_tty_t>);
uv_close(reinterpret_cast<uv_handle_t*>(this->Output.tty),
&on_close_delete);
&on_close_delete<uv_tty_t>);
} else {
uv_close(reinterpret_cast<uv_handle_t*>(this->Input.pipe),
&on_close_delete);
&on_close_delete<uv_pipe_t>);
uv_close(reinterpret_cast<uv_handle_t*>(this->Output.pipe),
&on_close_delete);
&on_close_delete<uv_pipe_t>);
}
return true;