-
Notifications
You must be signed in to change notification settings - Fork 58
rpc: fix asio bug that causes coredump #263
Conversation
asio_rpc_session::set_options()里面也要加write_lock |
set_options 只有初始化的时候会调,不需要加锁 |
不一定,如果是client端的socket,set_options()在_socket->async_connect()的回调函数里面被调用,此时有可能在其他线程调用close(),还是有潜在风险的。 |
set_options 在 server 端只有这里用到了 void asio_rpc_session::connect()
{
if (set_connecting()) {
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4(_remote_addr.ip()),
_remote_addr.port());
add_ref();
_socket->async_connect(ep, [this](boost::system::error_code ec) {
if (!ec) {
dinfo("client session %s connected", _remote_addr.to_string());
set_options();
set_connected();
on_send_completed();
start_read_next();
} else {
derror("client session connect to %s failed, error = %s",
_remote_addr.to_string(),
ec.message().c_str());
on_failure(true);
}
release_ref();
});
}
} 只有 set_connecting 返回 true 才会 async_connect,才会 set_options |
set_connecting() 加锁并不影响set_options(),两者在asio_rpc_session::connect()中是被平行调用的关系,且一个在回调函数前,一个在回调函数内。
|
public: | ||
virtual void connect() override; | ||
~asio_rpc_session() override; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加这么多空行干什么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个应该是 convention? public functions 之间加 new line,留白给以后加注释。
Fixed apache/incubator-pegasus#307.
The core problem is when the socket is reading/writing, the other threads which encounter a network error will close that socket, and may cause its internal
descriptor_data
to be null, then the thread w/r using the wild pointer will coredump in this case.The solution is to add write-lock on close and read-lock on r/w, so that will ensure thread-safety of the Boost-ASIO socket.