Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentry: handle errors when accessing socket endpoints #2306

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
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
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
Loading