365 Commits
Author SHA1 Message Date
Hiroshi SHIBATAandClaude Opus 5.5 349d9d65bc Initialize the fast fallback waiter fd before anything can raise
An invalid open_timeout raised before the waiter was assigned, so the cleanup closed fd 0 left by memset and took the process's stdin with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 14:16:14 +09:00
Nobuyoshi Nakada 0302df2e41 Use adaptive sampling for memory leak tests
Temporary memory growth caused intermittent failures, while repeated
warmups made these tests slow.
2026-10-19 20:51:56 +09:00
William T. NelsonandClaude Opus 5.5 f33766720c Socket.tcp_with_fast_fallback: Fix typo in getaddrinfo family
ADDRESS_FAMILIES[:family_name] used the Symbol :family_name as the key
instead of the local variable family_name, so the address family was
always nil. This is a follow-up to b896f80598 [Bug #21512].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 23:19:44 +02:00
Tiago Cardoso 36f2a8e807 [Feature #13677] Add host to addrinfo resolution errors
hostname is added as a suffix to getaddrinfo error messages. for
example, a "name or service not known" error message becomes "name or
service not known for `invalidthost.com`.
2026-10-12 14:33:34 +02:00
Hiroshi SHIBATAandClaude Opus 5.5 4903be9159 Remove workarounds for Linux 2.6.18
Linux 2.6.18 was the kernel of RHEL 5, whose Extended Life-cycle Support ended in 2020. Every supported kernel honors O_CLOEXEC and F_DUPFD_CLOEXEC, so there is no need to detect at runtime whether they work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:00:26 +09:00
Hiroshi SHIBATAandClaude Opus 5 ffd032a60b Report the IPv4 resolution error after EAI_ADDRFAMILY for IPv6
EAI_ADDRFAMILY left the IPv6 result looking successful, so a later IPv4 resolution error was not recorded and TCPSocket.new raised Socket::ResolutionError with error_code 0. When the IPv6 notification failed, EAI_ADDRFAMILY was recorded instead and hid the IPv4 error.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-01 10:51:19 +09:00
Samuel Williams 11f6d3d68b Flush buffered data in BasicSocket#close_write before shutdown. (#18900)
When a socket has buffered output (`sync == false`) and `#close_write` is
called, the write side was shut down via `shutdown(SHUT_WR)` without first
flushing the buffer. This had two bad consequences:

- the buffered bytes were silently dropped (never delivered to the peer);
- a subsequent `#close` tried to flush the still-populated write buffer
  into the now `shutdown(SHUT_WR)` socket and raised `Errno::EPIPE`.

Flush the buffer before shutting down the write side, matching the
write-only branch (which flushes via `rb_io_close`) and the non-socket
`IO#close_write` path. `IO#close_write` for a plain `IO` wrapping a socket
fd is fixed in the same way.

Assisted-By: devx/2c064143-8655-4631-a7cd-6395bb241bdc
2026-09-18 03:36:26 +00:00
Hiroshi SHIBATAandClaude Opus 5 902d51208b Don't hang in ensure when TCPSocket tests fail to connect
server_thread.value waited for an accept that never returned once TCPSocket.new had raised, hiding the failure behind a hang. Use stop_accept_thread as the newer tests do.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 15:06:08 +09:00
Hiroshi SHIBATAandClaude Opus 5 f4433d6a40 Don't crash on non-Hash delay or error in test_mode_settings
Only test_mode_settings itself was checked to be a Hash, and its delay and error values were passed to rb_hash_aref as they were.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 15:06:08 +09:00
Hiroshi SHIBATAandClaude Opus 5 f1af5e5d90 Wait for the exceptfds of a connecting socket on Windows
Winsock reports a failed non-blocking connect(2) only through the
exceptfds of select(2), which IO#wait_writable never asks for.  So
Addrinfo#connect_internal kept waiting on a socket whose connection had
already been refused, and burned the whole connect_timeout before
raising Errno::ETIMEDOUT instead of the real error.

Ask for the priority event on Windows alone.  The M:N thread scheduler
refuses any wait carrying another event, so requesting it where it
cannot fire would take the wait off that scheduler for nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 19:50:19 +09:00
Hiroshi SHIBATAandClaude Opus 5 43405ac6f2 Translate SO_ERROR into an errno on Windows
Winsock leaves a WSA error code in SO_ERROR, while the socket library
reads it as an errno everywhere. wait_connectable() compares it with
ECONNREFUSED and friends, and Socket.tcp hands it to SystemCallError, so
a connection refused on Windows surfaced as a bare SystemCallError
rather than Errno::ECONNREFUSED.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 19:50:19 +09:00
Hiroshi SHIBATAandClaude Opus 5 802372c60f Re-enable the tests skipped on FreeBSD for [Bug #18613]
The signal handling bug behind these was fixed in FreeBSD 13.3, and 13 is
now EOL. Each of them ran 30 times, and the affected files together 20
times, on the rubyci FreeBSD 14.5 amd64 and 15.1 arm64 machines with no
failure.

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277429

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-11 08:30:28 +09:00
Nobuyoshi Nakada 937f6c22b3 Use assert_include instead of assert_includes 2026-08-08 14:46:58 +09:00
Hiroshi SHIBATAandClaude Fable 5 82294a714f [Bug #22223] Check SO_ERROR after waiting for nonblocking connect
Darwin 27 answers the retry connect(2) on a refused nonblocking socket
with EISCONN, so the retry idiom in Addrinfo#connect_internal returned
an unconnected socket. SO_ERROR still holds the real error, so consult
it after wait_writable, as wait_connectable() in init.c already does.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 15:23:21 +09:00
Misaki Shioi 6e055f5692 Fix: Avoid hangs in fast_fallback tests under scheduling jitter (#17889)
Some fast_fallback tests inject an artificial delay to test timing-sensitive behavior. Under CI's parallel test execution, threadscheduling jitter can make the delay longer than expected, and in that case these tests may fail to connect to the server process they are actually supposed to connect to.

For example:

1. The IPv6 name resolution is artificially delayed by 25ms, so the IPv4 name resolution succeeds first.
2. If the IPv6 name resolution succeeds within 25ms, the client starts connecting to the `::1` server process and the test passes. However, if the IPv6 name resolution is delayed by more than 50ms for some reason, fast_fallback starts connecting to `127.0.0.1` instead, by design.
3. If another test happens to have a server listening on `127.0.0.1` with the same port number, the connection accidentally succeeds.
4. The `::1` server process is left waiting, and the thread cleanup hangs.

To address this, the listening server process now receives the result of `accept` through a local variable instead of `Thread#value`. On success, it joins the thread with a short timeout; otherwise, it terminates the thread with `Thread#kill.join`.

This change is expected to prevent hangs even when such a wrong connection occurs.
2026-07-16 09:11:36 +09:00
Nobuyoshi Nakada 2a5394cee7 Dump FreeBSD socket credential data wholely 2026-06-05 22:38:34 +09:00
Nobuyoshi Nakada 07e52c81fc Fix TestSocket#test_bintime
`String#unpack` does not allow negative offset.
2026-04-02 12:56:53 +09:00
Nobuyoshi NakadaandKoichi Sasada 9224d90f12 Refactor socket timestamp tests
- Avoid unnecessarily binding sender sockets, as ephemeral ports can
  very occasionally conflict on Linux.  This investigation is assisted
  by Claude Code.
- Compare timestamps using a range instead of relying on `inspect`.
- Use the `timestamp_retry_rw` helper in `test_bintime` as well.
- Use `String#unpack1` with offset to extract the timestamp data.

Co-authored-by: Koichi Sasada <ko1@atdot.net>
2026-04-01 11:34:42 +00:00
Takashi Kokubun 6f90ca2823 Increase UDP nonblock test timeout to avoid CI flakes (#16569)
The 1-second Timeout.timeout for IO.select waiting on a localhost
empty UDP packet is too tight for loaded CI runners, causing spurious
Timeout::Error failures. Increase to 30 seconds, which is only a
safety net against indefinite hangs.

https://github.com/ruby/ruby/actions/runs/23585200531/job/68676546959
2026-03-26 14:28:32 -07:00
Takashi Kokubun eb80511851 test/socket: rescue Errno::ENOBUFS in test_udp_server (#16553)
The server thread's msg_src.reply can raise Errno::ENOBUFS
("No buffer space available") transiently on macOS CI. This
exception was not caught, propagating through th.join in the
ensure block and failing the test.

Rescue it in the server thread so the client side times out on
IO.select, raises RuntimeError, and hits the existing omit path.
2026-03-25 17:44:13 -07:00
Misaki Shioi 47244b0f30 Fix: Specifying 0 should cause an immediate timeout (#15641)
This change fixes a bug in which specifying 0 for timeout-related options (such as the `timeout` option of `Addrinfo.getaddrinfo`) incorrectly results in an infinite wait.

(This change overwrites https://github.com/ruby/ruby/pull/15626 .)
2025-12-19 15:44:35 +09:00
Misaki Shioi 7b5691c3b0 Socket.tcp and TCPSocket.new raises IO::TiemoutError with user specified timeout (#15602)
* `Socket.tcp` and `TCPSocket.new` raises `IO::TiemoutError` with user specified timeout

In https://github.com/ruby/ruby/pull/11880, `rsock_connect()` was changed to raise `IO::TimeoutError` when a user-specified timeout occurs.
However, when `TCPSocket.new` attempts to connect to multiple destinations, it does not use `rsock_connect()`, and instead raises `Errno::ETIMEDOUT` on timeout.
As a result, the exception class raised on timeout could differ depending on whether there were multiple destinations or not.

To align this behavior with the implementation of `rsock_connect()`, this change makes `TCPSocket.new` raise `IO::TimeoutError` when a user-specified timeout occurs.
Similarly, `Socket.tcp` is updated to raise `IO::TimeoutError` when a timeout occurs within the method.
(Note that the existing behavior of `Addrinfo#connect_internal`, which Socket.tcp depends on internally and which raises `Errno::ETIMEDOUT` on timeout, is not changed.)

* [ruby/net-http] Raise `Net::OpenTimeout` when `TCPSocket.open` raises `IO::TimeoutError`.

With the changes in https://github.com/ruby/ruby/pull/15602, `TCPSocket.open` now raises `IO::TimeoutError` when a user-specified timeout occurs.
This change updates #connect to handle this case accordingly.

https://github.com/ruby/net-http/commit/f64109e1cf
2025-12-17 15:02:26 +09:00
Nobuyoshi Nakada beb85e7eee [Bug #21705] Fix segfaults on Windows
It should check the type of the argument and coercion before
converting the encoding.
2025-11-23 13:55:15 +09:00
Takashi Kokubun 8e1b5cba70 test_socket.rb: Permit ProxyError for Windows
https://github.com/ruby/ruby/actions/runs/17410747983/job/49427074277
2025-09-02 10:38:37 -07:00
Takashi Kokubun 12ef01b52f Skip an unstable test for macOS
https://github.com/ruby/ruby/actions/runs/16983250181/job/48147338735
https://github.com/ruby/ruby/actions/runs/16994020689/job/48180211762
2025-08-15 10:53:58 -07:00
Misaki Shioi ba490059b4 [Feature #21347] Add open_timeout as an overall timeout option for TCPSocket.new (#13909)
* [Feature #21347] Add `open_timeout` as an overall timeout option for `TCPSocket.new`

With this change, `TCPSocket.new` now accepts the `open_timeout` option.
This option raises an exception if the specified number of seconds has elapsed since the start of the method call,
even if the operation is still in the middle of name resolution or connection attempts.

The addition of this option follows the same intent as the previously merged change to `Socket.tcp`.
[Feature #21347](https://bugs.ruby-lang.org/issues/21347)
https://github.com/ruby/ruby/pull/13368

* Tidy up: Extract rsock_raise_user_specified_timeout()

* Added a note to the documentation of `Socket.tcp`

* Fix `rsock_init_inetsock` for `FAST_FALLBACK_INIT_INETSOCK_IMPL`
2025-07-17 18:15:19 +09:00
Misaki Shioi c45c600e22 Add open_timeout as an overall timeout option for Socket.tcp (#13368)
* Add `open_timeout` as an overall timeout option for `Socket.tcp`

[Background]
Currently, `TCPSocket.new` and `Socket.tcp` accept two kind of timeout options:
- `resolv_timeout`, which controls the timeout for DNS resolution
- `connect_timeout`, which controls the timeout for the connection attempt

With the introduction of Happy Eyeballs Version 2 (as per [RFC 8305](https://datatracker.ietf.org/doc/html/rfc8305)) in[ Feature #20108](https://bugs.ruby-lang.org/issues/20108) and [Feature #20782](https://bugs.ruby-lang.org/issues/20782), both address resolution and connection attempts are now parallelized.
As a result, the sum of `resolv_timeout` and `connect_timeout` no longer represents the total timeout duration. This is because, in HEv2, name resolution and connection attempts are performed concurrently, causing the two timeouts to overlap.

Example:
When `resolv_timeout: 200ms` and `connect_timeout: 100ms` are set:
1. An IPv6 address  is resolved after the method starts immediately (IPv4 is still being resolved).
2. A connection attempt is initiated to the IPv6 address
3. After 100ms, `connect_timeout` is exceeded. However, since `resolv_timeout` still has 100ms left, the IPv4 resolution continues.
4. After 200ms from the start, the method raises a `resolv_timeout` error.

In this case, the total elapsed time before a timeout is 200ms, not the expected 300ms (100ms + 200ms).

Furthermore, in HEv2, connection attempts are also parallelized.
It starts a new connection attempts every 250ms for resolved addresses. This makes the definition of `connect_timeout` even more ambiguous—specifically, it becomes unclear from which point the timeout is counted.

Additionally, these methods initiate new connection attempts every 250ms (Connection Attempt Delay) for each candidate address, thereby parallelizing connection attempts. However, this behavior makes it unclear from which point in time the connect_timeout is actually measured.
Currently, a `connect_timeout` is raised only after the last connection attempt exceeds the timeout.

Example:
When `connect_timeout: 100ms` is set and 3 address candidates:
1. Start a connection attempt to the address `a`
2. 250ms after step 1, start a new connection attempt to the address `b`
3. 500ms after step 1, start a new connection attempt to the address `c`
4. 1000ms after step 3 (1000ms after starting the connection to `c`, 1250ms after starting the connection to `b,` and 1500ms after starting the connection to `a`) `connect_timeout` is raised

This behavior aims to favor successful connections by allowing more time for each attempt, but it results in a timeout model that is difficult to reason about.

These methods have supported `resolv_timeout` and `connect_timeout` options even before the introduction of HEv2. However, in many use cases, it would be more convenient if a timeout occurred after a specified duration from the start of the method. Similar functions in other languages (such as PHP, Python, and Go) typically allow specifying only an overall timeout.

[Proposal]
I propose adding an `open_timeout` option to `Socket.tcp` in this PR, which triggers a timeout after a specified duration has elapsed from the start of the method.

The name `open_timeout` aligns with the existing accessor used in `Net::HTTP`.
If `open_timeout` is specified together with `resolv_timeout` and `connect_timeout`, I propose that only `open_timeout` be used and the others be ignored. While it is possible to support combinations of `open_timeout`, `resolv_timeout`, and `connect_timeout`, doing so would require defining which timeout takes precedence in which situations. In this case, I believe it is more valuable to keep the behavior simple and easy to understand, rather than supporting more complex use cases.

If this proposal is accepted, I also plan to extend `open_timeout` support to `TCPSocket.new`.

While the long-term future of `resolv_timeout` and `connect_timeout` may warrant further discussion, I believe the immediate priority is to offer a straightforward way to specify an overall timeout.

[Outcome]
If `open_timeout` is also supported by `TCPSocket.new`, users would be able to manage total connection timeouts directly in `Net::HTTP#connect` without relying on `Timeout.timeout`.
https://github.com/ruby/ruby/blob/aa0f689bf45352c4a592e7f1a044912c40435266/lib/net/http.rb#L1657

---

* Raise an exception if it is specified together with other timeout options

> If open_timeout is specified together with resolv_timeout and connect_timeout, I propose that only open_timeout be used and the others be ignored.

Since this approach may be unclear to users, I’ve decided to explicitly raise an `ArgumentError` if these options are specified together.

* Add doc

* Fix: open_timeout error should be raised even if there are still addresses that have not been tried
2025-06-14 09:54:34 +09:00
Aaron Patterson 1c66124273 Make Addrinfo objects Ractor shareable
Allow Addrinfo objects to be shared among Ractors.  Addrinfo objects are
already immutable, so I think it's safe for us to tag them as
RUBY_TYPED_FROZEN_SHAREABLE shareable too.
2025-05-20 18:04:37 -07:00
Hiroshi SHIBATA 6bd5a51ab5 Removed Solaris conditions from test files
We no longer execute those files with Solaris platforms.
2025-04-02 16:24:47 +09:00
Nobuyoshi Nakada 35920f7a44 Refine TestSocket_TCPSocket#test_initialize_failure
* Use `assert_raise_kind_of` instead of `rescue` and `flunk`.
* Use `assert_include` for the pattern that may contain regexp meta
  characters.
2025-03-15 16:02:19 +09:00
Nobuyoshi Nakada 29c0ca58c2 Test for the crash 2025-03-15 15:50:46 +09:00
Nobuyoshi Nakada 4a67ef09cc [Feature #21116] Extract RJIT as a third-party gem 2025-02-13 18:01:03 +09:00
Misaki Shioi 1683dadb19 Do not save ResolutionError if resolution succeeds for any address family (#12678)
* Do not save ResolutionError if resolution succeeds for any address family

Socket with Happy Eyeballs Version 2 performs connection attempts and name resolution in parallel.

In the existing implementation, if a connection attempt failed for one address family while name resolution was still in progress for the other, and that name resolution later failed, the method would terminate with a name resolution error.
This behavior was intended to ensure that the final error reflected the most recent failure, potentially overriding an earlier error.

However, [Bug #21088](https://bugs.ruby-lang.org/issues/21088) made me realize that terminating with a name resolution error is unnatural when name resolution succeeded for at least one address family.

This PR modifies the behavior so that if name resolution succeeds for one address family, any name resolution error from the other is not saved.

This PR includes the following changes:

* Do not display select(2) as the system call that caused the raised error, as it is for internal processing

* Fix bug: Get errno with Socket::SO_ERROR in Windows environment with a workaround for tests not passing
2025-02-03 20:26:47 +09:00
Misaki Shioi b53a75230f Fix tests for fast_fallback (#12406)
* TCPSocket.new: Close resources in ensure

* TCPSocket.new: Remove unnecessary comments

* Socket.tcp: Make assert_separately in TestSocket more readable

* Socket.tcp: Returning instead of exiting

* Socket.tcp: Close resources in ensure

* Socket.tcp: Avoid test failures on hosts that only support IPv4
2024-12-20 18:55:26 +09:00
Misaki Shioi 9f924e2f13 Improve APIs for Globally Enabling/Disabling fast_fallback in Socket (#12257)
This change includes the following updates:
- Added an environment variable `RUBY_TCP_NO_FAST_FALLBACK` to control enabling/disabling fast_fallback
- Updated documentation and man pages
- Revised the implementation of Socket.tcp_fast_fallback= and Socket.tcp_fast_fallback, which previously performed dynamic name resolution of constants and variables. As a result, the following performance improvements were achieved:

(Case of 1000 executions of `TCPSocket.new` to the local host)

Rehearsal -----------------------------------------
before   0.031462   0.147946   0.179408 (  0.249279)
after    0.031164   0.146839   0.178003 (  0.346935)
-------------------------------- total: 0.178003sec

            user     system      total        real
before   0.027584   0.138712   0.166296 (  0.233356)
after    0.025953   0.127608   0.153561 (  0.237971)
2024-12-14 15:51:19 +09:00
Misaki Shioi 8f57204c19 Avoid test failures on hosts that only support IPv4 (#12213)
To verify the behavior of HEv2, some tests were prepared. But unexpected failures occur in certain environments.
This happens in environments where "localhost" resolves only to an IPv4 address during tests that verify connections to IPv6.

For example, the following situation can occur:

- The server process is bound to ::1.
- The client socket always resolves "localhost" to 127.0.0.1 and attempts to connect to 127.0.0.1.
- Since no server is bound to 127.0.0.1, an ECONNREFUSED error is raised.

In such situations, the behavior of `TCPSocket.new` remains unchanged from before the introduction of HEv2.
(The failures occur because tests explicitly binding to ::1 were added to verify HEv2 behavior.)

This change ensures that the affected tests are skipped in environments of this kind.
2024-12-02 21:47:51 +09:00
Misaki Shioi ff5fc4b5a1 Do not save the last error without sockets in the connection attempt (#12153)
* Do not save the last_error if there are no sockets waiting to be connected

In this implementation, the results of both name resolution and connection attempts are awaited using select(2).
When it returned, the implementation attempted to check for connections even if there were no sockets currently attempting to connect, treating the absence of connected sockets as a connection failure.
With this fix, it will no longer check for connections when there are no sockets waiting to be connected.

Additionally, the following minor fixes have been made:

* Handle failure of getsockopt(2) and removed unnecessary continue in the loop

* Tweak: Use common API to check in_progress_fds

* Safely call TCPServer.new in test

* Set empty writefds when there is no socket waiting to be connected

* Enable fast_fallback option
2024-11-25 14:10:54 +09:00
Misaki Shioi 84470d72c5 Disable HEv2 tests temporarily (#12097)
* Disable HEv2 tests temporarily

To suppress error log output in CI.
They should have been DISABLE in PR #12070.

---

Additionally, the following fixes have been made:

- Remove unnecessary `assert_separately` from the related tests
2024-11-15 18:12:37 +09:00
Misaki Shioi 51666c827b Make fast_fallback option false by default temporarily (#12070)
to suppress failing output in CI.
2024-11-15 09:18:09 +09:00
Misaki ShioiandNobuyoshi Nakada 4c270200db [Feature #120782] Introduction of Happy Eyeballs Version 2 (RFC8305) in TCPSocket.new (#11653)
* Introduction of Happy Eyeballs Version 2 (RFC8305) in TCPSocket.new

This is an implementation of Happy Eyeballs version 2 (RFC 8305) in `TCPSocket.new`.
See https://github.com/ruby/ruby/pull/11653

1. Background
Prior to this implementation, I implemented Happy Eyeballs Version 2 (HEv2) for `Socket.tcp` in https://github.com/ruby/ruby/pull/9374.
HEv2 is an algorithm defined in [RFC 8305](https://datatracker.ietf.org/doc/html/rfc8305), aimed at improving network connectivity.
For more details on the specific cases that HEv2 helps, please refer to https://bugs.ruby-lang.org/issues/20108.

2. Proposal & Outcome
This proposal implements the same HEv2 algorithm in `TCPSocket.new`.
Since `TCPSocket.new` is used more widely than `Socket.tcp`, this change is expected to broaden the impact of HEv2's benefits.
Like `Socket.tcp`, I have also added `fast_fallback` keyword argument to `TCPSocket.new`.
This option is set to true by default, enabling the HEv2 functionality.
However, users can explicitly set it to false to disable HEv2 and use the previous behavior of `TCPSocket.new`.

It should be noted that HEv2 is enabled only in environments where pthreads are available.
This specification follows the approach taken in https://bugs.ruby-lang.org/issues/19965 , where name resolution can be interrupted.
(In environments where pthreads are not available, the `fast_fallback` option is ignored.)

3. Performance
Below is the benchmark of 100 requests to `www.ruby-lang.org` with the fast_fallback option set to true and false, respectively.
While there is a slight performance degradation when HEv2 is enabled, the degradation is smaller compared to that seen in `Socket.tcp`.

```
~/s/build ❯❯❯ ../install/bin/ruby ../ruby/test.rb
Rehearsal --------------------------------------------------------
fast_fallback: true    0.017588   0.097045   0.114633 (  1.460664)
fast_fallback: false   0.014033   0.078984   0.093017 (  1.413951)
----------------------------------------------- total: 0.207650sec

                           user     system      total        real
fast_fallback: true    0.020891   0.124054   0.144945 (  1.473816)
fast_fallback: false   0.018392   0.110852   0.129244 (  1.466014)
```

* Update debug prints

Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>

* Remove debug prints

* misc

* Disable HEv2 in Win

* Raise resolution error with hostname resolution

* Fix to handle errors

* Remove warnings

* Errors that do not need to be handled

* misc

* Improve doc

* Fix bug on cancellation

* Avoid EAI_ADDRFAMILY for resolving IPv6

* Follow upstream

* misc

* Refactor connection_attempt_fds management

- Introduced allocate_connection_attempt_fds and reallocate_connection_attempt_fds for improved memory allocation of connection_attempt_fds
- Added remove_connection_attempt_fd to resize connection_attempt_fds dynamically.
- Simplified the in_progress_fds function to only check the size of connection_attempt_fds.

* Rename do_pthread_create to raddrinfo_pthread_create to avoid conflicting

---------

Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>
2024-11-12 10:06:48 +09:00
Jeremy Evans 08f14b8d4c Allow Errno::EACCES when testing connection timeout
Some packaging systems that include support for running tests,
such as OpenBSD's, do not allow outbound network connections
during testing for security reasons. EACCES is the error raised by
OpenBSD in this case.
2024-09-03 18:21:01 -07:00
Nobuyoshi Nakada d4e24021d3 Revise 9ec342e07d 2024-02-26 13:12:05 +09:00
Nobuyoshi Nakada a0f7de814a [Bug #20296] Fix the default assertion message 2024-02-26 12:29:23 +09:00
Misaki Shioi 9ec342e07d Introduction of Happy Eyeballs Version 2 (RFC8305) in Socket.tcp (#9374)
* Introduction of Happy Eyeballs Version 2 (RFC8305) in Socket.tcp

This is an implementation of Happy Eyeballs version 2 (RFC 8305) in Socket.tcp.

[Background]
Currently, `Socket.tcp` synchronously resolves names and makes connection attempts with `Addrinfo::foreach.`
This implementation has the following two problems.

1. In name resolution, the program stops until the DNS server responds to all DNS queries.
2. In a connection attempt, while an IP address is trying to connect to the destination host and is taking time, the program stops, and other resolved IP addresses cannot try to connect.

[Proposal]
"Happy Eyeballs" ([RFC 8305](https://datatracker.ietf.org/doc/html/rfc8305)) is an algorithm to solve this kind of problem. It avoids delays to the user whenever possible and also uses IPv6 preferentially.

I implemented it into `Socket.tcp` by using `Addrinfo.getaddrinfo` in each thread spawned per address family to resolve the hostname asynchronously, and using `Socket::connect_nonblock` to try to connect with multiple addrinfo in parallel.

[Outcome]

This change eliminates a fatal defect in the following cases.

Case 1. One of the A or AAAA DNS queries does not return

---
require 'socket'

class Addrinfo
  class << self
    # Current Socket.tcp depends on foreach
    def foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, timeout: nil, &block)
      getaddrinfo(nodename, service, Socket::AF_INET6, socktype, protocol, flags, timeout: timeout)
        .concat(getaddrinfo(nodename, service, Socket::AF_INET, socktype, protocol, flags, timeout: timeout))
        .each(&block)
    end

    def getaddrinfo(_, _, family, *_)
      case family
      when Socket::AF_INET6 then sleep
      when Socket::AF_INET then [Addrinfo.tcp("127.0.0.1", 4567)]
      end
    end
  end
end

Socket.tcp("localhost", 4567)
---

Because the current `Socket.tcp` cannot resolve IPv6 names, the program stops in this case. It cannot start to connect with IPv4 address.
Though `Socket.tcp` with HEv2 can promptly start a connection attempt with IPv4 address in this case.

 Case 2. Server does not promptly return ack for syn of either IPv4 / IPv6 address family

---
require 'socket'

fork do
  socket = Socket.new(Socket::AF_INET6, :STREAM)
  socket.setsockopt(:SOCKET, :REUSEADDR, true)
  socket.bind(Socket.pack_sockaddr_in(4567, '::1'))
  sleep
  socket.listen(1)
  connection, _ = socket.accept
  connection.close
  socket.close
end

fork do
  socket = Socket.new(Socket::AF_INET, :STREAM)
  socket.setsockopt(:SOCKET, :REUSEADDR, true)
  socket.bind(Socket.pack_sockaddr_in(4567, '127.0.0.1'))
  socket.listen(1)
  connection, _ = socket.accept
  connection.close
  socket.close
end

Socket.tcp("localhost", 4567)
---

The current `Socket.tcp` tries to connect serially, so when its first name resolves an IPv6 address and initiates a connection to an IPv6 server, this server does not return an ACK, and the program stops.
Though `Socket.tcp` with HEv2 starts to connect sequentially and in parallel so a connection can be established promptly at the socket that attempted to connect to the IPv4 server.

In exchange, the performance of `Socket.tcp` with HEv2 will be degraded.

---
100.times { Socket.tcp("www.ruby-lang.org", 80) }
---

This is due to the addition of the creation of IO objects, Thread objects, etc., and calls to `IO::select` in the implementation.

* Avoid NameError of Socket::EAI_ADDRFAMILY in MinGW

* Support Windows with SO_CONNECT_TIME

* Improve performance

I have additionally implemented the following patterns:

- If the host is single-stack, name resolution is performed in the main thread. This reduces the cost of creating threads.
- If an IP address is specified, name resolution is performed in the main thread. This also reduces the cost of creating threads.
- If only one IP address is resolved, connect is executed in blocking mode. This reduces the cost of calling IO::select.

Also, I have added a fast_fallback option for users who wish not to use HE.
Here are the results of each performance test.

```ruby
require 'socket'
require 'benchmark'

HOSTNAME = "www.ruby-lang.org"
PORT = 80

ai = Addrinfo.tcp(HOSTNAME, PORT)

Benchmark.bmbm do |x|
  x.report("Domain name") do
    30.times { Socket.tcp(HOSTNAME, PORT).close }
  end

  x.report("IP Address") do
    30.times { Socket.tcp(ai.ip_address, PORT).close }
  end

  x.report("fast_fallback: false") do
    30.times { Socket.tcp(HOSTNAME, PORT, fast_fallback: false).close }
  end
end
```

```
                           user     system      total        real
Domain name            0.015567   0.032511   0.048078 (  0.325284)
IP Address             0.004458   0.014219   0.018677 (  0.284361)
fast_fallback: false   0.005869   0.021511   0.027380 (  0.321891)
````

And this is the measurement result when executed in a single stack environment.

```
                           user     system      total        real
Domain name            0.007062   0.019276   0.026338 (  1.905775)
IP Address             0.004527   0.012176   0.016703 (  3.051192)
fast_fallback: false   0.005546   0.019426   0.024972 (  1.775798)
```

The following is the result of the run on Ruby 3.3.0.

(on Dual stack environment)

```
                 user     system      total        real
Ruby 3.3.0   0.007271   0.027410   0.034681 (  0.472510)
```

(on Single stack environment)

```
                 user     system      total        real
Ruby 3.3.0  0.005353   0.018898   0.024251 (  1.774535)
```

* Do not cache `Socket.ip_address_list`

As mentioned in the comment at https://github.com/ruby/ruby/pull/9374#discussion_r1482269186, caching Socket.ip_address_list does not follow changes in network configuration.
But if we stop caching, it becomes necessary to check every time `Socket.tcp` is called whether it's a single stack or not, which could further degrade performance in the case of a dual stack.
From this, I've changed the approach so that when a domain name is passed, it doesn't check whether it's a single stack or not and resolves names in parallel each time.

The performance measurement results are as follows.

require 'socket'
require 'benchmark'

HOSTNAME = "www.ruby-lang.org"
PORT = 80

ai = Addrinfo.tcp(HOSTNAME, PORT)

Benchmark.bmbm do |x|
  x.report("Domain name") do
    30.times { Socket.tcp(HOSTNAME, PORT).close }
  end

  x.report("IP Address") do
    30.times { Socket.tcp(ai.ip_address, PORT).close }
  end

  x.report("fast_fallback: false") do
    30.times { Socket.tcp(HOSTNAME, PORT, fast_fallback: false).close }
  end
end

                           user     system      total        real
Domain name            0.004085   0.011873   0.015958 (  0.330097)
IP Address             0.000993   0.004400   0.005393 (  0.257286)
fast_fallback: false   0.001348   0.008266   0.009614 (  0.298626)

* Wait forever if fallback addresses are unresolved, unless resolv_timeout

Changed from waiting only 3 seconds for name resolution when there is no fallback address available, to waiting as long as there is no resolv_timeout.
This is in accordance with the current `Socket.tcp` specification.

* Use exact pattern to match IPv6 address format for specify address family
2024-02-26 12:14:11 +09:00
KJ Tsanaktsidis da33c5ac9f Revert "Set AI_ADDRCONFIG when making getaddrinfo(3) calls for outgoing conns"
This reverts commit 673ed41c81.
2024-02-01 11:09:54 +11:00
KJ Tsanaktsidis 67404d657a Revert "always omit test_ai_addrconfig."
This reverts commit abf192eb16.
2024-02-01 11:09:54 +11:00
Yusuke Endoh 73b43fffa1 Prevent a warning: assigned but unused variable - status 2023-12-12 10:31:37 +09:00
Tanaka Akira abf192eb16 always omit test_ai_addrconfig. 2023-12-07 18:28:47 +09:00
KJ Tsanaktsidis d2ba8ea54a Set AI_ADDRCONFIG when making getaddrinfo(3) calls for outgoing conns (#7295)
When making an outgoing TCP or UDP connection, set AI_ADDRCONFIG in the
hints we send to getaddrinfo(3) (if supported). This will prompt the
resolver to _NOT_ issue A or AAAA queries if the system does not
actually have an IPv4 or IPv6 address (respectively).

This makes outgoing connections marginally more efficient on
non-dual-stack systems, since we don't have to try connecting to an
address which can't possibly work.

More importantly, however, this works around a race condition present
in some older versions of glibc on aarch64 where it could accidently
send the two outgoing DNS queries with the same DNS txnid, and get
confused when receiving the responses. This manifests as outgoing
connections sometimes taking 5 seconds (the DNS timeout before retry) to
be made.

Fixes #19144
2023-12-07 17:55:15 +09:00
Misaki Shioi 52c2660163 Fixup with review comment
https://github.com/ruby/ruby/pull/9088#discussion_r1411490445
2023-12-01 12:20:03 +09:00