Skip to content

Commit 8d575e4

Browse files
authored
Save the error that occurred during name resolution (ruby#12155)
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.
1 parent 34e36a7 commit 8d575e4

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

ext/socket/ipsocket.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,48 @@ init_fast_fallback_inetsock_internal(VALUE v)
10871087
status = wait_arg.status = 0;
10881088
}
10891089

1090+
if (!resolution_store.is_all_finised) {
1091+
if (!resolution_store.v6.finished && arg->getaddrinfo_entries[IPV6_ENTRY_POS]->has_syserr) {
1092+
resolution_store.v6.finished = true;
1093+
1094+
if (arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err) {
1095+
last_error.type = RESOLUTION_ERROR;
1096+
last_error.ecode = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err;
1097+
syscall = "getaddrinfo(3)";
1098+
resolution_store.v6.has_error = true;
1099+
} else {
1100+
resolution_store.v6.ai = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->ai;
1101+
}
1102+
1103+
if (resolution_store.v4.finished) {
1104+
resolution_store.is_all_finised = true;
1105+
resolution_delay_expires_at = NULL;
1106+
user_specified_resolv_timeout_at = NULL;
1107+
}
1108+
}
1109+
if (!resolution_store.v4.finished && arg->getaddrinfo_entries[IPV4_ENTRY_POS]->has_syserr) {
1110+
resolution_store.v4.finished = true;
1111+
1112+
if (arg->getaddrinfo_entries[IPV4_ENTRY_POS]->err) {
1113+
last_error.type = RESOLUTION_ERROR;
1114+
last_error.ecode = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->err;
1115+
syscall = "getaddrinfo(3)";
1116+
resolution_store.v4.has_error = true;
1117+
} else {
1118+
resolution_store.v4.ai = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->ai;
1119+
}
1120+
1121+
if (resolution_store.v6.finished) {
1122+
resolution_store.is_all_finised = true;
1123+
resolution_delay_expires_at = NULL;
1124+
user_specified_resolv_timeout_at = NULL;
1125+
} else {
1126+
set_timeout_tv(&resolution_delay_storage, 50, now);
1127+
resolution_delay_expires_at = &resolution_delay_storage;
1128+
}
1129+
}
1130+
}
1131+
10901132
if (!any_addrinfos(&resolution_store)) {
10911133
if (!in_progress_fds(arg->connection_attempt_fds_size) &&
10921134
resolution_store.is_all_finised) {
@@ -1113,34 +1155,6 @@ init_fast_fallback_inetsock_internal(VALUE v)
11131155
rb_raise(etimedout_error, "user specified timeout");
11141156
}
11151157
}
1116-
1117-
if (!resolution_store.is_all_finised) {
1118-
if (!resolution_store.v6.finished && arg->getaddrinfo_entries[IPV6_ENTRY_POS]->has_syserr) {
1119-
resolution_store.v6.ai = arg->getaddrinfo_entries[IPV6_ENTRY_POS]->ai;
1120-
resolution_store.v6.finished = true;
1121-
1122-
if (resolution_store.v4.finished) {
1123-
resolution_store.is_all_finised = true;
1124-
wait_arg.readfds = NULL;
1125-
resolution_delay_expires_at = NULL;
1126-
user_specified_resolv_timeout_at = NULL;
1127-
}
1128-
}
1129-
if (!resolution_store.v4.finished && arg->getaddrinfo_entries[IPV4_ENTRY_POS]->has_syserr) {
1130-
resolution_store.v4.ai = arg->getaddrinfo_entries[IPV4_ENTRY_POS]->ai;
1131-
resolution_store.v4.finished = true;
1132-
1133-
if (resolution_store.v6.finished) {
1134-
resolution_store.is_all_finised = true;
1135-
wait_arg.readfds = NULL;
1136-
resolution_delay_expires_at = NULL;
1137-
user_specified_resolv_timeout_at = NULL;
1138-
} else {
1139-
set_timeout_tv(&resolution_delay_storage, 50, now);
1140-
resolution_delay_expires_at = &resolution_delay_storage;
1141-
}
1142-
}
1143-
}
11441158
}
11451159

11461160
rb_thread_check_ints();

0 commit comments

Comments
 (0)