Skip to content

Commit

Permalink
sentry: handle errors when accessing socket endpoints (#2306)
Browse files Browse the repository at this point in the history
Fixes #2300
  • Loading branch information
canepat authored Sep 6, 2024
1 parent cbc4aeb commit 1f12a80
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 2 additions & 3 deletions silkworm/sentry/rlpx/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Task<std::unique_ptr<Peer>> Client::connect(
try {
attempt_num++;
co_await stream.socket().async_connect(endpoint, use_awaitable);
const auto remote_endpoint = stream.socket().remote_endpoint();
log::Trace("sentry") << "rlpx::Client connected to " << remote_endpoint;
is_connected = true;
} catch (const boost::system::system_error& ex) {
if (ex.code() == boost::system::errc::operation_canceled)
Expand All @@ -71,9 +73,6 @@ Task<std::unique_ptr<Peer>> Client::connect(
}
}

auto remote_endpoint = stream.socket().remote_endpoint();
log::Trace("sentry") << "rlpx::Client connected to " << remote_endpoint;

co_return std::make_unique<Peer>(
client_context,
std::move(stream),
Expand Down
2 changes: 2 additions & 0 deletions silkworm/sentry/rlpx/peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Peer::Peer(
bool is_inbound,
bool is_static)
: stream_(std::move(stream)),
local_endpoint_(stream_.socket().local_endpoint()),
remote_endpoint_(stream_.socket().remote_endpoint()),
node_key_(std::move(node_key)),
client_id_(std::move(client_id)),
node_listen_port_(node_listen_port),
Expand Down
6 changes: 4 additions & 2 deletions silkworm/sentry/rlpx/peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ class Peer {
}

boost::asio::ip::tcp::endpoint local_endpoint() const {
return stream_.socket().local_endpoint();
return local_endpoint_;
}

boost::asio::ip::tcp::endpoint remote_endpoint() const {
return stream_.socket().remote_endpoint();
return remote_endpoint_;
}

bool is_inbound() const { return is_inbound_; };
Expand Down Expand Up @@ -113,6 +113,8 @@ class Peer {
Task<void> ping_periodically(framing::MessageStream& message_stream);

SocketStream stream_;
boost::asio::ip::tcp::endpoint local_endpoint_;
boost::asio::ip::tcp::endpoint remote_endpoint_;
EccKeyPair node_key_;
std::string client_id_;
uint16_t node_listen_port_;
Expand Down
9 changes: 7 additions & 2 deletions silkworm/sentry/rlpx/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ Task<void> Server::run(
throw;
}

auto remote_endpoint = stream.socket().remote_endpoint();
log::Debug("sentry") << "rlpx::Server client connected from " << remote_endpoint;
try {
const auto remote_endpoint = stream.socket().remote_endpoint();
log::Debug("sentry") << "rlpx::Server client connected from " << remote_endpoint;
} catch (const boost::system::system_error& ex) {
log::Debug("sentry") << "rlpx::Server client immediately disconnected [" + std::string{ex.what()} + "]";
continue;
}

auto peer = std::make_shared<Peer>(
client_executor,
Expand Down

0 comments on commit 1f12a80

Please sign in to comment.