Skip to content

Commit

Permalink
Save the error that occurred during name resolution (ruby#12155)
Browse files Browse the repository at this point in the history
even if a system call error happens after the name resolution failure in the child thread.

pipe and write(2) are used to notify the main thread of the name resolution results from the child thread.
After name resolution is completed in the child thread, if the call to write(2) fails, the main thread retrieves the resolved addresses.
However, when name resolution failed, the corresponding error was not being saved in `last_error`.
With this change, name resolution failures will now be saved in last_error even if the write(2) call in the child thread fails.
  • Loading branch information
shioimm authored Nov 23, 2024
1 parent 34e36a7 commit 8d575e4
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions ext/socket/ipsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,48 @@ init_fast_fallback_inetsock_internal(VALUE v)
status = wait_arg.status = 0;
}

if (!resolution_store.is_all_finised) {
if (!resolution_store.v6.finished && arg->getaddrinfo_entries[IPV6_ENTRY_POS]->has_syserr) {
resolution_store.v6.finished = true;

if (arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err) {
last_error.type = RESOLUTION_ERROR;
last_error.ecode = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err;
syscall = "getaddrinfo(3)";
resolution_store.v6.has_error = true;
} else {
resolution_store.v6.ai = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->ai;
}

if (resolution_store.v4.finished) {
resolution_store.is_all_finised = true;
resolution_delay_expires_at = NULL;
user_specified_resolv_timeout_at = NULL;
}
}
if (!resolution_store.v4.finished && arg->getaddrinfo_entries[IPV4_ENTRY_POS]->has_syserr) {
resolution_store.v4.finished = true;

if (arg->getaddrinfo_entries[IPV4_ENTRY_POS]->err) {
last_error.type = RESOLUTION_ERROR;
last_error.ecode = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->err;
syscall = "getaddrinfo(3)";
resolution_store.v4.has_error = true;
} else {
resolution_store.v4.ai = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->ai;
}

if (resolution_store.v6.finished) {
resolution_store.is_all_finised = true;
resolution_delay_expires_at = NULL;
user_specified_resolv_timeout_at = NULL;
} else {
set_timeout_tv(&resolution_delay_storage, 50, now);
resolution_delay_expires_at = &resolution_delay_storage;
}
}
}

if (!any_addrinfos(&resolution_store)) {
if (!in_progress_fds(arg->connection_attempt_fds_size) &&
resolution_store.is_all_finised) {
Expand All @@ -1113,34 +1155,6 @@ init_fast_fallback_inetsock_internal(VALUE v)
rb_raise(etimedout_error, "user specified timeout");
}
}

if (!resolution_store.is_all_finised) {
if (!resolution_store.v6.finished && arg->getaddrinfo_entries[IPV6_ENTRY_POS]->has_syserr) {
resolution_store.v6.ai = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->ai;
resolution_store.v6.finished = true;

if (resolution_store.v4.finished) {
resolution_store.is_all_finised = true;
wait_arg.readfds = NULL;
resolution_delay_expires_at = NULL;
user_specified_resolv_timeout_at = NULL;
}
}
if (!resolution_store.v4.finished && arg->getaddrinfo_entries[IPV4_ENTRY_POS]->has_syserr) {
resolution_store.v4.ai = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->ai;
resolution_store.v4.finished = true;

if (resolution_store.v6.finished) {
resolution_store.is_all_finised = true;
wait_arg.readfds = NULL;
resolution_delay_expires_at = NULL;
user_specified_resolv_timeout_at = NULL;
} else {
set_timeout_tv(&resolution_delay_storage, 50, now);
resolution_delay_expires_at = &resolution_delay_storage;
}
}
}
}

rb_thread_check_ints();
Expand Down

0 comments on commit 8d575e4

Please sign in to comment.