Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add strand to protect internals of asio - develop #6976

Merged
merged 2 commits into from
Mar 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ namespace eosio {
transaction_state_index trx_state;
optional<sync_state> peer_requested; // this peer is requesting info from us
std::shared_ptr<boost::asio::io_context> server_ioc; // keep ioc alive
socket_ptr socket;
boost::asio::io_context::strand strand;
socket_ptr socket;

fc::message_buffer<1024*1024> pending_message_buffer;
fc::optional<std::size_t> outstanding_read_bytes;
Expand Down Expand Up @@ -730,6 +731,7 @@ namespace eosio {
trx_state(),
peer_requested(),
server_ioc( my_impl->server_ioc ),
strand( app().get_io_service() ),
socket( std::make_shared<tcp::socket>( std::ref( *my_impl->server_ioc ))),
node_id(),
last_handshake_recv(),
Expand All @@ -755,6 +757,7 @@ namespace eosio {
trx_state(),
peer_requested(),
server_ioc( my_impl->server_ioc ),
strand( app().get_io_service() ),
socket( s ),
node_id(),
last_handshake_recv(),
Expand Down Expand Up @@ -976,7 +979,8 @@ namespace eosio {
std::vector<boost::asio::const_buffer> bufs;
buffer_queue.fill_out_buffer( bufs );

boost::asio::async_write(*socket, bufs, [c, priority]( boost::system::error_code ec, std::size_t w ) {
boost::asio::async_write(*socket, bufs,
boost::asio::bind_executor(strand, [c, priority]( boost::system::error_code ec, std::size_t w ) {
app().post(priority, [c, priority, ec, w]() {
try {
auto conn = c.lock();
Expand Down Expand Up @@ -1016,7 +1020,7 @@ namespace eosio {
fc_elog( logger,"Exception in do_queue_write to ${p}", ("p",pname) );
}
});
});
}));
}

void connection::cancel_sync(go_away_reason reason) {
Expand Down Expand Up @@ -1859,7 +1863,7 @@ namespace eosio {
connection_wptr weak_conn = c;
// Note: need to add support for IPv6 too

resolver->async_resolve( query,
resolver->async_resolve( query, boost::asio::bind_executor( c->strand,
[weak_conn, this]( const boost::system::error_code& err, tcp::resolver::iterator endpoint_itr ) {
app().post( priority::low, [err, endpoint_itr, weak_conn, this]() {
auto c = weak_conn.lock();
Expand All @@ -1871,7 +1875,7 @@ namespace eosio {
("peer_addr", c->peer_name())( "error", err.message()) );
}
} );
} );
} ) );
}

void net_plugin_impl::connect(const connection_ptr& c, tcp::resolver::iterator endpoint_itr) {
Expand All @@ -1883,7 +1887,8 @@ namespace eosio {
++endpoint_itr;
c->connecting = true;
connection_wptr weak_conn = c;
c->socket->async_connect( current_endpoint, [weak_conn, endpoint_itr, this]( const boost::system::error_code& err ) {
c->socket->async_connect( current_endpoint, boost::asio::bind_executor( c->strand,
[weak_conn, endpoint_itr, this]( const boost::system::error_code& err ) {
app().post( priority::low, [weak_conn, endpoint_itr, this, err]() {
auto c = weak_conn.lock();
if( !c ) return;
Expand All @@ -1902,7 +1907,7 @@ namespace eosio {
}
}
} );
} );
} ) );
}

bool net_plugin_impl::start_session(const connection_ptr& con) {
Expand Down Expand Up @@ -2052,6 +2057,7 @@ namespace eosio {
++conn->reads_in_flight;
boost::asio::async_read(*conn->socket,
conn->pending_message_buffer.get_buffer_sequence_for_boost_async_read(), completion_handler,
boost::asio::bind_executor( conn->strand,
[this,weak_conn]( boost::system::error_code ec, std::size_t bytes_transferred ) {
app().post( priority::medium, [this,weak_conn, ec, bytes_transferred]() {
auto conn = weak_conn.lock();
Expand Down Expand Up @@ -2133,7 +2139,7 @@ namespace eosio {
close( conn );
}
});
});
}));
} catch (...) {
string pname = conn ? conn->peer_name() : "no connection name";
fc_elog( logger, "Undefined exception handling reading ${p}",("p",pname) );
Expand Down