9655 Commits
Author SHA1 Message Date
Douglas Eichelberger 50f6624862 Reset the fork lock in the child without unlocking it
A thread that is waiting for the fork lock when another thread forks
exists only in the parent, but on macOS the child's copy of the lock
still counts it. After the child unlocks its copy,
pthread_rwlock_destroy() and pthread_rwlock_init() fail with EBUSY, so
the child aborts with
"[BUG] pthread_rwlock_destroy: Resource busy (EBUSY)". Lookup threads
wait for the lock whenever a fork holds it, so a process that resolves
names in background threads while it forks loses a child now and then.

Reset the child's copy by assigning PTHREAD_RWLOCK_INITIALIZER, as
rb_internal_thread_event_hooks_rw_lock_atfork() does for its lock, and
do not unlock it first. With a waiter counted, macOS's unlock enters the
kernel to wake it, and the state that leaves behind for the lock's
address survives the assignment, so a later waiter in the child can
sleep forever although no thread holds the lock. FreeBSD already skipped
the unlock in the child (dc8c382266), and on glibc the child's unlock
does not release the write lock anyway.

[Bug #22445]
2026-10-17 20:37:04 +09:00
Douglas Eichelberger 56c3e7ea7b Join getaddrinfo and getnameinfo threads before fork
A getaddrinfo thread calls getaddrinfo(3) inside rb_thread_prevent_fork,
but on glibc its thread exit takes the resolver configuration lock again
after the thread has released the fork lock. A fork that lands then
leaves the child with that lock held, and every later name lookup in the
child blocks forever. getnameinfo(3) attaches the same resolver state,
even for a name in /etc/hosts, and its thread ran outside
rb_thread_prevent_fork altogether.

Make the lookup threads joinable and have fork join any that may still
be exiting. As a lookup thread finishes, still inside
rb_thread_prevent_fork, it swaps itself into a single slot with an
atomic exchange and joins the thread it displaced, which has usually
exited by then, and rb_thread_acquire_fork_lock joins whatever is left
in the slot. Once lookups settle, at most one finished thread is left
unjoined. The slot holds pthread_t as a pointer, with NULL for empty,
which assumes pthread_t is scalar and never 0.

getnameinfo threads now run within rb_thread_prevent_fork and register
as they exit, as getaddrinfo threads do, and where getnameinfo runs
without a helper thread it is called within rb_thread_prevent_fork too.
So fork also waits for a reverse lookup in progress, as it already did
for a forward one. raddrinfo_pthread_create now always creates joinable
threads, which drops its pthread_detach fallback for platforms without
pthread_attr_setdetachstate.

The slot is kept across fork, so every fork path that keeps running
Ruby code has to take the fork lock. On the BSDs, Process.daemon forks
with daemon(3) and did not take it, so the daemon would inherit the
registered thread, and its next fork would abort in pthread_join with
ESRCH. Take and release the fork lock around daemon(3) as rb_fork_ruby
does. daemon(3) can fail in the child as well as in the parent, so
compare pids to tell which process returned.

[Bug #22406]

(cherry picked from commit 44f7c6b00d)
2026-10-17 14:24:19 +09:00
mugitti9 c9c050a24d [Bug #22269] Make coverage_enabled: false effective for compile/compile_file (#18533)
Use it in Coverage.line_stub so compiling the stub does not overwrite already-collected coverage data.

Fixes: [Bug #22269]
2026-09-06 16:56:18 +09:00
nagachika aac3e36dd4 merge revision(s) 82294a714f: [Backport #22223]
[PATCH] [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-09 16:09:11 +09:00
Randy Stauner 07836e0b2b Avoid reading TypedData type after dfree
[Bug #22101] (Backport)
2026-06-29 19:11:30 +09:00
nagachika 7fa5883808 Bump resolv.gem to v0.7.1. [Backport #22017] 2026-05-24 17:31:27 +09:00
Kazuki Yamaguchi b1420a81f9 Merge openssl-3.3.3
The changes can be found at:

    https://github.com/ruby/openssl/compare/v3.3.2...v3.3.3
2026-05-16 10:23:35 +09:00
Kazuki Yamaguchi 08a3e58cab Merge openssl-3.3.2
The changes can be found at:

    https://github.com/ruby/openssl/compare/v3.3.1...v3.3.2
2026-05-16 10:23:35 +09:00
nagachika 3e9494c70b Bump zlib version to 3.2.3. 2026-03-07 14:50:06 +09:00
John HawthornandChristian Bruckmayer 153fa85994 objspace_dump: Skip invalid CME when dumping CCs
When a CC is invalidated only the klass field is set to 0. After it's
invalidated it isn't safe to access the CME, as it may have been freed.

I made a similar change in Ruby 4.0 in
640a2f1dc7, but assumed it was due to the
changes we'd made to callcaches making klass a weak-reference.

Co-authored-by: Christian Bruckmayer <christian.bruckmayer@shopify.com>
2026-03-07 14:17:15 +09:00
Takashi Kokubun 252b718582 merge revision(s) bc2a8a002a, c26057ebaf: [Backport #21779]
[PATCH] [Bug #21779] Uniquify `InitVM` functions as well as `Init`

	Avoid possible name conflict when `--with-static-linked-ext`.

	[PATCH] [Bug #21779] Do not export InitVM functions

	Fix ruby/io-console#105.
2025-12-16 15:53:03 -08:00
Takashi Kokubun 955decc1da merge revision(s) beb85e7eee: [Backport #21705]
[PATCH] [Bug #21705] Fix segfaults on Windows

	It should check the type of the argument and coercion before
	converting the encoding.
2025-12-08 14:46:06 -08:00
John Hawthorn 7482835d0b Avoid spawning thread for trivial getnameinfo calls
When calling getnameinfo we spawn a thread because it may do a slow,
blocking reverse-DNS lookup. Spawning a thread is relatively fast (~20µs
on my Linux machine) but still an order of magnitude slower than when
getnameinfo is simply translating to a numeric IP or port, which, at
least in my tests on Linux, doesn't even make a syscall.

This commit adds a fast path for when reverse DNS isn't required: either
host isn't being fetched or NI_NUMERICHOST is set AND either the
service name isn't required or NI_NUMERICSERV is set. The service name
should only need to read /etc/services, which should be fast-ish, but
is still I/O so I kept the existing behaviour (it could be on a network
fs I guess).

I tested with:

    s = TCPSocket.open("www.ruby-lang.org", 80)
    500_000.times { Socket.unpack_sockaddr_in(s.getpeername) }

Before: 12.935s
After: 0.338s
2025-11-17 13:28:01 -08:00
Luke Gruber 9e4829699b ext/socket: Set raddrinfo thread as detached before thread start (#15194)
Backport of https://github.com/ruby/ruby/pull/15142

[Bug #21679]
2025-11-14 19:48:40 +00:00
Nobuyoshi Nakada a7eba54539 [Backport #21625] Allow io/wait methods with IO#ungetc in text mode 2025-11-06 10:25:54 -08:00
Bo Anderson fce44db5eb Update openssl gem to 3.3.1 for Ruby 3.4 (#14792)
Update openssl gem to 3.3.1

[Backport #21631]
2025-10-09 07:32:47 -07:00
Takashi Kokubun 18e176659e merge revision(s) 2bb6fe3854: [Backport #21629]
[PATCH] [Bug #21629] Initialize `struct RString`

which appears to be missed in the previous commit for some reason.
2025-10-08 16:50:58 -07:00
Nobuyoshi Nakada d2264185fa [ruby/json] [ruby/json] Run have_func with the header providing the declarations
https://github.com/ruby/json/commit/95fb084027

https://github.com/ruby/json/commit/9d080765cc
2025-07-15 09:23:42 -07:00
Nobuyoshi Nakada 8a9c6e0304 [ruby/strscan] Run have_func with the header providing the declarations
https://github.com/ruby/strscan/commit/18c0a59b65
2025-07-15 09:23:42 -07:00
Nobuyoshi Nakada 2473d0b6f7 [ruby/strscan] Update extconf.rb
(https://github.com/ruby/strscan/pull/158)

- `have_func` includes "ruby.h" by default.
- include "ruby/re.h" where `rb_reg_onig_match` is declared.

https://github.com/ruby/strscan/commit/1ac96f47e9
2025-07-15 09:23:42 -07:00
Nobuyoshi Nakada 05a7d345ce [ruby/openssl] [ruby/openssl] Run have_func with the header providing the declarations
https://github.com/ruby/openssl/commit/b6f56c4540

https://github.com/ruby/openssl/commit/5277ca1431
2025-07-15 09:23:42 -07:00
Hiroshi SHIBATA 9a11bc5a24 Merge etc 1.4.6 2025-07-15 09:23:42 -07:00
Hiroshi SHIBATA 84d4932270 Merge io-nonblock 0.3.2 2025-07-15 09:23:42 -07:00
Hiroshi SHIBATA 854787a8b2 Merge io-wait 0.3.2 2025-07-15 09:23:42 -07:00
Hiroshi SHIBATA 282cbf68f0 Merge io-console 0.8.1 2025-07-15 09:23:42 -07:00
Takashi Kokubun 782aef10bb merge revision(s) d77e02bd85: [Backport #21497]
[Bug #21497] [ruby/socket]: add full prototype
	MIME-Version: 1.0
	Content-Type: text/plain; charset=UTF-8
	Content-Transfer-Encoding: 8bit

	otherwise, gcc 15 will complain:

	> init.c:573:19: error: too many arguments to function ‘Rconnect’; expected 0, have 3
	>   573 |     return (VALUE)Rconnect(arg->fd, arg->sockaddr, arg->len);
	>       |                   ^~~~~~~~ ~~~~~~~
	> In file included from init.c:11:
	> rubysocket.h:294:5: note: declared here
	>   294 | int Rconnect();
	>       |     ^~~~~~~~

	> sockssocket.c:33:9: error: too many arguments to function ‘SOCKSinit’; expected 0, have 1
	>    33 |         SOCKSinit("ruby");
	>       |         ^~~~~~~~~ ~~~~~~
	> In file included from sockssocket.c:11:
	> rubysocket.h:293:6: note: declared here
	>   293 | void SOCKSinit();
	>       |      ^~~~~~~~~

	Signed-off-by: Z. Liu <zhixu.liu@gmail.com>
2025-07-14 14:48:27 -07:00
Takashi Kokubun 02744e58f3 merge revision(s) c1877d431e: [Backport #21437]
[ruby/date] [Bug #21437] Date#hash for large years

	Addresses https://bugs.ruby-lang.org/issues/21437

	Signed-off-by: Dmitry Dygalo <dmitry.dygalo@workato.com>

	https://github.com/ruby/date/commit/31f07bc576
2025-07-14 14:31:20 -07:00
Hiroshi SHIBATA 5cd408a52e Bump up resolv-0.6.2 for Ruby 3.4 (#13818) 2025-07-08 09:32:36 -07:00
Misaki Shioi c57efbfb3a Fix heap-use-after-free in free_fast_fallback_getaddrinfo_entry (#13231)
This change addresses the following ASAN error:

```
==36597==ERROR: AddressSanitizer: heap-use-after-free on address 0x512000396ba8 at pc 0x7fcad5cbad9f bp 0x7fff19739af0 sp 0x7fff19739ae8
  WRITE of size 8 at 0x512000396ba8 thread T0
  [643/756] 36600=optparse/test_summary
      #0 0x7fcad5cbad9e in free_fast_fallback_getaddrinfo_entry /home/runner/work/ruby-dev-builder/ruby-dev-builder/ext/socket/raddrinfo.c:3046:22
      #1 0x7fcad5c9fb48 in fast_fallback_inetsock_cleanup /home/runner/work/ruby-dev-builder/ruby-dev-builder/ext/socket/ipsocket.c:1179:17
      #2 0x7fcadf3b611a in rb_ensure /home/runner/work/ruby-dev-builder/ruby-dev-builder/eval.c:1081:5
      #3 0x7fcad5c9b44b in rsock_init_inetsock /home/runner/work/ruby-dev-builder/ruby-dev-builder/ext/socket/ipsocket.c:1289:20
      #4 0x7fcad5ca22b8 in tcp_init /home/runner/work/ruby-dev-builder/ruby-dev-builder/ext/socket/tcpsocket.c:76:12
      #5 0x7fcadf83ba70 in vm_call0_cfunc_with_frame /home/runner/work/ruby-dev-builder/ruby-dev-builder/./vm_eval.c:164:15
...
```

A `struct fast_fallback_getaddrinfo_shared` is shared between the main thread and two child threads.
This struct contains an array of `fast_fallback_getaddrinfo_entry`.

`fast_fallback_getaddrinfo_entry` and `fast_fallback_getaddrinfo_shared` were freed separately, and if `fast_fallback_getaddrinfo_shared` was freed first and then an attempt was made to free a `fast_fallback_getaddrinfo_entry`, a `heap-use-after-free` could occur.

This change avoids that possibility by separating the deallocation of the addrinfo memory held by `fast_fallback_getaddrinfo_entry` from the access and lifecycle of the `fast_fallback_getaddrinfo_entry` itself.
2025-06-18 11:09:20 -07:00
Misaki Shioi 584365af7b Ensure that memory is not freed before calling free_fast_fallback_getaddrinfo_* (#12661)
Ensure that `getaddrinfo_entry` and `getaddrinfo_shared` exist before free them in the main thread.
2025-06-18 11:09:20 -07:00
Luke Jahnke 7e093fb43a Fix crash in TCPSocket.open
Fix segfault crash observable with TCPSocket.open(nil, nil)
2025-04-09 16:02:37 -07:00
Jean Boussier e3fc29a9b7 Improve backtrace of errors raised by Socket.tcp_with_fast_fallback
[Bug #21211]

Socket errors raised from background threads are hard to track down because
their backtrace starts from the spawned thread.

To solve this we can raise a new error with the old one as `cause`.
2025-04-02 09:16:27 -07:00
Jean Boussier 0e416faf45 Fix Socket.tcp_with_fast_fallback to be usable from a Ractor
[Bug #21179]

```
socket.rb:1046:in 'Socket::HostnameResolutionStore#get_addrinfo': can not access non-shareable objects in constant
Socket::HostnameResolutionStore::PRIORITY_ON_V6 by non-main ractor. (Ractor::IsolationError)
	from socket.rb:724:in 'block in Socket.tcp_with_fast_fallback'
	from socket.rb:720:in 'Socket.tcp_with_fast_fallback'
```
2025-03-11 09:16:22 -07:00
Takashi Kokubun 319c3c7038 merge revision(s) 1683dadb19: [Backport #21088]
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-13 21:13:47 -08:00
Takashi Kokubun 2ee25f7b47 merge revision(s) 2b6fc9ea72: [Backport #21092]
[Bug #21092] Fallback variables after execonf has done

	When reading from a dummy makefile, the global variables initialized
	in `init_mkmf` may not be overridden.
2025-02-13 21:11:06 -08:00
Nobuyoshi Nakada 2acbf45d36 [DOC] Monitor 2024-12-25 13:16:14 +09:00
Nobuyoshi Nakada 871124c3ea [DOC] Stop document Ripper::Lexer
`:nodoc:` seems not working for inner classes.
2024-12-25 13:07:22 +09:00
Nobuyoshi Nakada e46314edd1 [DOC] Monitor 2024-12-25 10:30:40 +09:00
Misaki Shioi 3be1baab82 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.
2024-12-25 03:06:02 +09:00
Misaki Shioi 34e6bb48af Improve doc for Socket::ResolutionError (#12434)
Also, a topic about Socket::ResolutionError is added to NEWS
2024-12-23 15:05:00 +09:00
Koichi Sasada c695536cc8 use st_update to prevent table extension
to prevent the following scenario:

1. `delete_unique_str()` can be called while GC (sweeping)
2. it calls `st_insert()` to decrement the counter
3. `st_insert()` can try to extend the table even if the key exists
4. `xmalloc` while GC and cause BUG
2024-12-23 11:05:34 +09:00
Kazuki Yamaguchi 9e3e1c7fc9 [ruby/openssl] Ruby/OpenSSL 3.3.0
https://github.com/ruby/openssl/commit/e5153dbbb4
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi 4862462097 [ruby/openssl] digest: remove optional parameter from OpenSSL::Digest#finish
OpenSSL::Digest#finish overrides Digest::Instance#finish and is called
from the Digest::Class framework in the digest library. This method is
not supposed to take any arguments, as suggested by the RDoc comment for
Digest::Instance#finish.

It is a private method and not exposed to users. Let's remove it.

This optional parameter exists since r15602 in Ruby trunk, the commit
which converted OpenSSL::Digest to a subclass of Digest::Class.

https://github.com/ruby/openssl/commit/dcb2a4f30b
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi 9de2b407d7 [ruby/openssl] digest: make output buffer String independent in #finish
Likewise, OpenSSL::Digest#finish needs to make the output buffer
independent before writing to it.

https://github.com/ruby/openssl/commit/9cc8a83466
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi 637f019f1f [ruby/openssl] cipher: make output buffer String independent
OpenSSL::Cipher#update accepts a String as the second argument to be
used as the output buffer. The buffer must be directly writable, in
other words, it must not be frozen and not a shared string.

rb_str_resize() does not make the String independent if the String
already has the intended length. Use the rb_str_modify() family instead
to check it.

Fixes: https://bugs.ruby-lang.org/issues/20937

https://github.com/ruby/openssl/commit/1de3b80a46
2024-12-22 03:33:03 +09:00
Kazuki Yamaguchi c79b435407 [ruby/openssl] pkcs12: add PKCS12#set_mac
Add a binding for PKCS12_set_mac() to set MAC parameters and
(re-)calculate MAC for the content.

This allows generating PKCS #12 with consistent MAC parameters with
different OpenSSL versions. OpenSSL 3.0 changed the default hash
function used for HMAC and the KDF from SHA-1 to SHA-256.

Fixes: https://github.com/ruby/openssl/issues/772

https://github.com/ruby/openssl/commit/f5ed2a74b6
2024-12-22 03:33:03 +09:00
Peter Zhu a58675386c Prefix asan_poison_object with rb 2024-12-19 09:14:34 -05:00
Jean Boussier 91efe7b64f [ruby/json] Release 2.9.1
https://github.com/ruby/json/commit/f745ec145e
2024-12-19 08:45:31 +09:00
Naohisa Goto 979b19b741 [ruby/json] Add support for Solaris 10 which lacks strnlen()
Check for existence of strnlen() and use alternative code if it is missing.

https://github.com/ruby/json/commit/48d4bbc3a0
2024-12-19 08:45:31 +09:00
Hiroshi SHIBATA 3edbf6cfc5 [ruby/psych] Bump up v5.2.2
https://github.com/ruby/psych/commit/746e1ad24d
2024-12-18 10:27:03 +09:00