未验证 提交 3b7870b4 编写于 作者: 羽飞's avatar 羽飞 提交者: GitHub

fix exit bug (#57)

修复进程退出时,libevent 无法正常结束循环的BUG
上级 9ea8409a
diff --git a/src/observer/CMakeLists.txt b/src/observer/CMakeLists.txt
index 1e156f2..37f5f86 100644
--- a/src/observer/CMakeLists.txt
+++ b/src/observer/CMakeLists.txt
@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC})
ENDFOREACH (F)
-SET(LIBRARIES common pthread dl event jsoncpp)
+SET(LIBRARIES common pthread dl event_pthreads event jsoncpp)
# 指定目标文件位置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin)
diff --git a/src/observer/main.cpp b/src/observer/main.cpp
index 4fff6da..112255e 100644
--- a/src/observer/main.cpp
+++ b/src/observer/main.cpp
@@ -137,8 +137,6 @@ void *quit_thread_func(void *_signum)
LOG_INFO("Receive signal: %ld", signum);
if (g_server) {
g_server->shutdown();
- delete g_server;
- g_server = nullptr;
}
return nullptr;
}
@@ -169,4 +167,6 @@ int main(int argc, char **argv)
LOG_INFO("Server stopped");
cleanup();
+
+ delete g_server;
}
diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp
index e87ad0d..2c6cef6 100644
--- a/src/observer/net/server.cpp
+++ b/src/observer/net/server.cpp
@@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
+#include <event2/thread.h>
#include "common/lang/mutex.h"
#include "common/log/log.h"
@@ -411,6 +412,7 @@ int Server::start_unix_socket_server()
int Server::serve()
{
+ evthread_use_pthreads();
event_base_ = event_base_new();
if (event_base_ == nullptr) {
LOG_ERROR("Failed to create event base, %s.", strerror(errno));
@@ -425,19 +427,6 @@ int Server::serve()
event_base_dispatch(event_base_);
- return 0;
-}
-
-void Server::shutdown()
-{
- LOG_INFO("Server shutting down");
-
- // cleanup
- struct timeval exit_time;
- gettimeofday(&exit_time, nullptr);
- exit_time.tv_sec += 10;
- event_base_loopexit(event_base_, &exit_time);
-
if (listen_ev_ != nullptr) {
event_del(listen_ev_);
event_free(listen_ev_);
@@ -451,4 +440,16 @@ void Server::shutdown()
started_ = false;
LOG_INFO("Server quit");
+ return 0;
+}
+
+void Server::shutdown()
+{
+ LOG_INFO("Server shutting down");
+
+ // cleanup
+ if (event_base_ != nullptr && started_) {
+ started_ = false;
+ event_base_loopexit(event_base_, nullptr);
+ }
}
...@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC}) ...@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC})
ENDFOREACH (F) ENDFOREACH (F)
SET(LIBRARIES common pthread dl event jsoncpp) SET(LIBRARIES common pthread dl event_pthreads event jsoncpp)
# 指定目标文件位置 # 指定目标文件位置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin)
......
...@@ -137,8 +137,6 @@ void *quit_thread_func(void *_signum) ...@@ -137,8 +137,6 @@ void *quit_thread_func(void *_signum)
LOG_INFO("Receive signal: %ld", signum); LOG_INFO("Receive signal: %ld", signum);
if (g_server) { if (g_server) {
g_server->shutdown(); g_server->shutdown();
delete g_server;
g_server = nullptr;
} }
return nullptr; return nullptr;
} }
...@@ -169,4 +167,6 @@ int main(int argc, char **argv) ...@@ -169,4 +167,6 @@ int main(int argc, char **argv)
LOG_INFO("Server stopped"); LOG_INFO("Server stopped");
cleanup(); cleanup();
delete g_server;
} }
...@@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */ ...@@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <event2/thread.h>
#include "common/lang/mutex.h" #include "common/lang/mutex.h"
#include "common/log/log.h" #include "common/log/log.h"
...@@ -411,6 +412,7 @@ int Server::start_unix_socket_server() ...@@ -411,6 +412,7 @@ int Server::start_unix_socket_server()
int Server::serve() int Server::serve()
{ {
evthread_use_pthreads();
event_base_ = event_base_new(); event_base_ = event_base_new();
if (event_base_ == nullptr) { if (event_base_ == nullptr) {
LOG_ERROR("Failed to create event base, %s.", strerror(errno)); LOG_ERROR("Failed to create event base, %s.", strerror(errno));
...@@ -425,19 +427,6 @@ int Server::serve() ...@@ -425,19 +427,6 @@ int Server::serve()
event_base_dispatch(event_base_); event_base_dispatch(event_base_);
return 0;
}
void Server::shutdown()
{
LOG_INFO("Server shutting down");
// cleanup
struct timeval exit_time;
gettimeofday(&exit_time, nullptr);
exit_time.tv_sec += 10;
event_base_loopexit(event_base_, &exit_time);
if (listen_ev_ != nullptr) { if (listen_ev_ != nullptr) {
event_del(listen_ev_); event_del(listen_ev_);
event_free(listen_ev_); event_free(listen_ev_);
...@@ -451,4 +440,16 @@ void Server::shutdown() ...@@ -451,4 +440,16 @@ void Server::shutdown()
started_ = false; started_ = false;
LOG_INFO("Server quit"); LOG_INFO("Server quit");
return 0;
}
void Server::shutdown()
{
LOG_INFO("Server shutting down");
// cleanup
if (event_base_ != nullptr && started_) {
started_ = false;
event_base_loopexit(event_base_, nullptr);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册