Skip to content

Commit 54d3945

Browse files
authored
Add host information to timeout error messages in TCPSocket.new and Socket.tcp (ruby#15582)
This change adds host information to the error messages shown when a timeout occurs while passing timeout options to `TCPSocket.new` or `Socket.tcp`, for improved usability. (When the `fast_fallback option` is enabled, there may be multiple possible destinations, so the host name is shown instead of an IP address.) As part of this change, the error messages in `Addrinfo.getaddrinfo` and `Addrinfo#connect_internal`, both of which are used by `Socket.tcp`, have also been improved in the same way.
1 parent 1506c48 commit 54d3945

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

ext/socket/init.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ rsock_socket(int domain, int type, int proto)
473473

474474
/* emulate blocking connect behavior on EINTR or non-blocking socket */
475475
static int
476-
wait_connectable(VALUE self, VALUE timeout)
476+
wait_connectable(VALUE self, VALUE timeout, const struct sockaddr *sockaddr, int len)
477477
{
478478
int sockerr;
479479
socklen_t sockerrlen;
@@ -514,7 +514,10 @@ wait_connectable(VALUE self, VALUE timeout)
514514
VALUE result = rb_io_wait(self, RB_INT2NUM(RUBY_IO_READABLE|RUBY_IO_WRITABLE), timeout);
515515

516516
if (result == Qfalse) {
517-
rb_raise(rb_eIOTimeoutError, "Connect timed out!");
517+
VALUE rai = rsock_addrinfo_new((struct sockaddr *)sockaddr, len, PF_UNSPEC, 0, 0, Qnil, Qnil);
518+
VALUE addr_str = rsock_addrinfo_inspect_sockaddr(rai);
519+
VALUE message = rb_sprintf("user specified timeout for %" PRIsVALUE, addr_str);
520+
rb_raise(rb_eIOTimeoutError, "%" PRIsVALUE, message);
518521
}
519522

520523
int revents = RB_NUM2INT(result);
@@ -603,7 +606,7 @@ rsock_connect(VALUE self, const struct sockaddr *sockaddr, int len, int socks, V
603606
#ifdef EINPROGRESS
604607
case EINPROGRESS:
605608
#endif
606-
return wait_connectable(self, timeout);
609+
return wait_connectable(self, timeout, sockaddr, len);
607610
}
608611
}
609612
return status;

ext/socket/ipsocket.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ struct inetsock_arg
2727
};
2828

2929
void
30-
rsock_raise_user_specified_timeout(void)
30+
rsock_raise_user_specified_timeout(struct addrinfo *ai, VALUE host, VALUE port)
3131
{
32-
rb_raise(rb_eIOTimeoutError, "user specified timeout");
32+
VALUE message;
33+
34+
if (ai && ai->ai_addr) {
35+
VALUE rai = rsock_addrinfo_new((struct sockaddr *)ai->ai_addr, (socklen_t)ai->ai_addrlen, PF_UNSPEC, 0, 0, Qnil, Qnil);
36+
VALUE addr_str = rsock_addrinfo_inspect_sockaddr(rai);
37+
message = rb_sprintf("user specified timeout for %" PRIsVALUE, addr_str);
38+
} else {
39+
message = rb_sprintf("user specified timeout for %" PRIsVALUE " port %" PRIsVALUE, host, port);
40+
}
41+
42+
rb_raise(rb_eIOTimeoutError, "%" PRIsVALUE, message);
3343
}
3444

3545
static VALUE
@@ -149,7 +159,9 @@ init_inetsock_internal(VALUE v)
149159
} else {
150160
VALUE elapsed = rb_funcall(current_clocktime(), '-', 1, starts_at);
151161
timeout = rb_funcall(open_timeout, '-', 1, elapsed);
152-
if (rb_funcall(timeout, '<', 1, INT2FIX(0)) == Qtrue) rsock_raise_user_specified_timeout();
162+
if (rb_funcall(timeout, '<', 1, INT2FIX(0)) == Qtrue) {
163+
rsock_raise_user_specified_timeout(res, arg->remote.host, arg->remote.serv);
164+
}
153165
}
154166

155167
if (status >= 0) {
@@ -844,6 +856,10 @@ init_fast_fallback_inetsock_internal(VALUE v)
844856
if (!NIL_P(open_timeout)) {
845857
VALUE elapsed = rb_funcall(current_clocktime(), '-', 1, starts_at);
846858
timeout = rb_funcall(open_timeout, '-', 1, elapsed);
859+
860+
if (rb_funcall(timeout, '<', 1, INT2FIX(0)) == Qtrue) {
861+
rsock_raise_user_specified_timeout(NULL, arg->remote.host, arg->remote.serv);
862+
}
847863
}
848864
if (NIL_P(timeout)) {
849865
if (!NIL_P(connect_timeout)) {
@@ -1180,7 +1196,9 @@ init_fast_fallback_inetsock_internal(VALUE v)
11801196
}
11811197
}
11821198

1183-
if (is_timeout_tv(user_specified_open_timeout_at, now)) rsock_raise_user_specified_timeout();
1199+
if (is_timeout_tv(user_specified_open_timeout_at, now)) {
1200+
rsock_raise_user_specified_timeout(NULL, arg->remote.host, arg->remote.serv);
1201+
}
11841202

11851203
if (!any_addrinfos(&resolution_store)) {
11861204
if (!in_progress_fds(arg->connection_attempt_fds_size) &&
@@ -1203,7 +1221,7 @@ init_fast_fallback_inetsock_internal(VALUE v)
12031221
resolution_store.is_all_finished) &&
12041222
(is_timeout_tv(user_specified_connect_timeout_at, now) ||
12051223
!in_progress_fds(arg->connection_attempt_fds_size))) {
1206-
rsock_raise_user_specified_timeout();
1224+
rsock_raise_user_specified_timeout(NULL, arg->remote.host, arg->remote.serv);
12071225
}
12081226
}
12091227
}

ext/socket/lib/socket.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def connect_internal(local_addrinfo, timeout=nil) # :yields: socket
6262
break
6363
when :wait_writable
6464
sock.wait_writable(timeout) or
65-
raise Errno::ETIMEDOUT, 'user specified timeout'
65+
raise Errno::ETIMEDOUT, "user specified timeout for #{self.ip_address}:#{self.ip_port}"
6666
end while true
6767
else
6868
sock.connect(self)
@@ -905,7 +905,9 @@ def self.tcp_with_fast_fallback(host, port, local_host = nil, local_port = nil,
905905
end
906906
end
907907

908-
raise(IO::TimeoutError, 'user specified timeout') if expired?(now, user_specified_open_timeout_at)
908+
if expired?(now, user_specified_open_timeout_at)
909+
raise(IO::TimeoutError, "user specified timeout for #{host}:#{port}")
910+
end
909911

910912
if resolution_store.empty_addrinfos?
911913
if connecting_sockets.empty? && resolution_store.resolved_all_families?
@@ -918,7 +920,7 @@ def self.tcp_with_fast_fallback(host, port, local_host = nil, local_port = nil,
918920

919921
if (expired?(now, user_specified_resolv_timeout_at) || resolution_store.resolved_all_families?) &&
920922
(expired?(now, user_specified_connect_timeout_at) || connecting_sockets.empty?)
921-
raise IO::TimeoutError, 'user specified timeout'
923+
raise(IO::TimeoutError, "user specified timeout for #{host}:#{port}")
922924
end
923925
end
924926
end

ext/socket/raddrinfo.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,15 @@ rb_getaddrinfo(const char *hostp, const char *portp, const struct addrinfo *hint
597597

598598
if (need_free) free_getaddrinfo_arg(arg);
599599

600-
if (timedout) rsock_raise_user_specified_timeout();
600+
if (timedout) {
601+
if (arg->ai) {
602+
rsock_raise_user_specified_timeout(arg->ai, Qnil, Qnil);
603+
} else {
604+
VALUE host = rb_str_new_cstr(hostp);
605+
VALUE port = rb_str_new_cstr(portp);
606+
rsock_raise_user_specified_timeout(NULL, host, port);
607+
}
608+
}
601609

602610
// If the current thread is interrupted by asynchronous exception, the following raises the exception.
603611
// But if the current thread is interrupted by timer thread, the following returns; we need to manually retry.

ext/socket/rubysocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void free_fast_fallback_getaddrinfo_shared(struct fast_fallback_getaddrinfo_shar
454454
#endif
455455

456456
unsigned int rsock_value_timeout_to_msec(VALUE);
457-
NORETURN(void rsock_raise_user_specified_timeout(void));
457+
NORETURN(void rsock_raise_user_specified_timeout(struct addrinfo *ai, VALUE host, VALUE port));
458458

459459
void rsock_init_basicsocket(void);
460460
void rsock_init_ipsocket(void);

0 commit comments

Comments
 (0)