Skip to content

Commit

Permalink
CXXCBC-633: ensure DNS-SRV resolver reports timeouts (#703)
Browse files Browse the repository at this point in the history
* catch asio::error::operation_aborted in every handler, and return
  immediately giving chance to deadline_ handler to report timeout error
  code

* make sure that UDP round will always fallback to TCP in case of UDP
  timeout.
  • Loading branch information
avsej authored Dec 9, 2024
1 parent 5355b0f commit fe4f36f
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions core/io/dns_client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
bytes_transferred1);
if (ec1) {
self->udp_deadline_.cancel();
if (ec1 == asio::error::operation_aborted) {
return;
}
CB_LOG_DEBUG(
"DNS UDP write operation has got error, retrying with TCP, address=\"{}:{}\", ec={}",
self->address_.to_string(),
Expand All @@ -121,6 +124,9 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>

self->udp_deadline_.cancel();
if (ec2) {
if (ec2 == asio::error::operation_aborted) {
return;
}
CB_LOG_DEBUG(
"DNS UDP read operation has got error, retrying with TCP, address=\"{}:{}\", ec={}",
self->address_.to_string(),
Expand Down Expand Up @@ -151,11 +157,12 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
if (ec == asio::error::operation_aborted) {
return;
}
self->udp_.cancel();
CB_LOG_DEBUG("DNS UDP deadline has been reached, cancelling UDP operation and fall back to "
"TCP, address=\"{}:{}\"",
self->address_.to_string(),
self->port_);
self->udp_.cancel();
return self->retry_with_tcp();
});

deadline_.expires_after(total_timeout);
Expand All @@ -172,6 +179,7 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
if (self->tcp_.is_open()) {
self->tcp_.cancel();
}
return self->handler_({ errc::common::unambiguous_timeout });
});
}

Expand All @@ -195,6 +203,9 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
const asio::ip::tcp::endpoint endpoint(address_, port_);
tcp_.async_connect(endpoint, [self = shared_from_this()](std::error_code ec1) mutable {
if (ec1) {
if (ec1 == asio::error::operation_aborted) {
return;
}
self->deadline_.cancel();
CB_LOG_DEBUG("DNS TCP connection has been aborted, address=\"{}:{}\", ec={}",
self->address_.to_string(),
Expand All @@ -220,14 +231,14 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
ec2 ? ec2.message() : "ok",
bytes_transferred2);
if (ec2) {
if (ec2 == asio::error::operation_aborted) {
return;
}
CB_LOG_DEBUG("DNS TCP write operation has been aborted, address=\"{}:{}\", ec={}",
self->address_.to_string(),
self->port_,
ec2.message());
self->deadline_.cancel();
if (ec2 == asio::error::operation_aborted) {
ec2 = errc::common::unambiguous_timeout;
}
return self->handler_({ ec2 });
}
asio::async_read(
Expand All @@ -244,6 +255,9 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
reinterpret_cast<std::uint8_t*>(&self->recv_buf_size_) +
bytes_transferred3));
if (ec3) {
if (ec3 == asio::error::operation_aborted) {
return;
}
CB_LOG_DEBUG(
"DNS TCP buf size read operation has been aborted, address=\"{}:{}\", ec={}",
self->address_.to_string(),
Expand All @@ -259,7 +273,6 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
self->tcp_,
asio::buffer(self->recv_buf_),
[self](std::error_code ec4, std::size_t bytes_transferred4) mutable {
self->deadline_.cancel();
CB_LOG_PROTOCOL(
"[DNS, TCP, IN] host=\"{}\", port={}, rc={}, bytes_received={}{:a}",
self->address_.to_string(),
Expand All @@ -269,6 +282,10 @@ class dns_srv_command : public std::enable_shared_from_this<dns_srv_command>
spdlog::to_hex(self->recv_buf_.data(),
self->recv_buf_.data() + bytes_transferred4));

if (ec4 == asio::error::operation_aborted) {
return;
}
self->deadline_.cancel();
if (ec4) {
CB_LOG_DEBUG(
"DNS TCP read operation has been aborted, address=\"{}:{}\", ec={}",
Expand Down

0 comments on commit fe4f36f

Please sign in to comment.