diff options
author | Misaki Shioi <[email protected]> | 2024-12-25 03:06:02 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2024-12-25 03:06:02 +0900 |
commit | 3be1baab82d8627ce52391030160bcbca69db01d (patch) | |
tree | 3b53e5be0a80a22ca22e510cc6b4fba2d74f187a /ext/socket/ipsocket.c | |
parent | 07e89bde4693fa6b8da4c152f1cf91686d1823f1 (diff) |
Introduce a timeout to prevent `rb_thread_fd_select` from hanging with write(2) failure (#12457)
Rarely, there are cases where a write(2) call from a child thread
to notify the main thread of the completion of name resolution fails.
If this happens while the main thread is waiting in `rb_thread_fd_select`,
rb_thread_fd_select may not notice that the name resolution has completed and end up hanging.
This issue becomes a problem when there are no sockets currently being connected,
no addresses ready for immediate connection attempts,
and name resolution has already completed for one address family
while the main thread is waiting for the name resolution of the other address family.
(If name resolution is not completed for either address family,
the chances of write(2) failing in both child threads are likely low.)
To avoid this issue, a timeout is introduced to rb_thread_fd_select under the above conditions.
This way, even if the issue occurs,
the completion of name resolution should still be detected
in the subsequent `if (!resolution_store.is_all_finished) ...` block.
Notes
Notes:
Merged-By: shioimm <[email protected]>
Diffstat (limited to 'ext/socket/ipsocket.c')
-rw-r--r-- | ext/socket/ipsocket.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index 05f71343e3..3e497a43be 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -854,7 +854,18 @@ init_fast_fallback_inetsock_internal(VALUE v) delay = tv_to_timeout(ends_at, now); delay_p = &delay; } else { - delay_p = NULL; + if (((resolution_store.v6.finished && !resolution_store.v4.finished) || + (resolution_store.v4.finished && !resolution_store.v6.finished)) && + !any_addrinfos(&resolution_store) && + !in_progress_fds(arg->connection_attempt_fds_size)) { + /* A limited timeout is introduced to prevent select(2) from hanging when it is exclusively + * waiting for name resolution and write(2) failure occurs in a child thread. */ + delay.tv_sec = 0; + delay.tv_usec = 50000; + delay_p = &delay; + } else { + delay_p = NULL; + } } nfds = 0; @@ -1040,6 +1051,7 @@ init_fast_fallback_inetsock_internal(VALUE v) status = 0; } + /* For cases where write(2) fails in child threads */ if (!resolution_store.is_all_finised) { if (!resolution_store.v6.finished && arg->getaddrinfo_entries[IPV6_ENTRY_POS]->has_syserr) { resolution_store.v6.finished = true; |