Commit graph

20692 commits

Author SHA1 Message Date
Jean Boussier
83702f7157 [ruby/pp] Handle BasicObject
Right now attempting to pretty print a BasicObject or any other
object lacking a few core Object methods will result in an error

```
Error: test_basic_object(PPTestModule::PPInspectTest): NoMethodError: undefined method `is_a?' for an instance of BasicObject
lib/pp.rb:192:in `pp'
lib/pp.rb:97:in `block in pp'
lib/pp.rb:158:in `guard_inspect_key'
lib/pp.rb:97:in `pp'
test/test_pp.rb:131:in `test_basic_object'
     128:
     129:   def test_basic_object
     130:     a = BasicObject.new
  => 131:     assert_match(/\A#<BasicObject:0x[\da-f]+>\n\z/, PP.pp(a, ''.dup))
     132:   end
     133: end
     134:
```

With some fairly small changes we can fallback to `Object#inspect`
which is better than an error.

4e9f6c2de0
2024-11-12 02:13:15 +00:00
Jean Boussier
107a4da122 [ruby/pp] Data#pretty_print handle privated or removed members
[Bug #20808]

The previous implementation assumed all members are accessible,
but it's possible for users to change the visibility of members or
to entirely remove the accessor.

fb19501434
2024-11-12 02:11:43 +00:00
Misaki Shioi
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
Peter Zhu
64827b15e8 [ruby/json] Fix $VERBOSE restore
Manually sync this commit that was missed.

6ae9155
2024-11-11 15:13:24 -05:00
tomoya ishida
25d17868de [ruby/reline] Refactor perform_completon
(https://github.com/ruby/reline/pull/778)

Flatten recursive method
Remove CompletionState::COMPLETE

aa5b278f3d
2024-11-11 15:31:04 +00:00
Jean Boussier
1b6a26e23a [ruby/json] JSON.load_file: explictly load the file as UTF-8
Fix: https://github.com/ruby/json/issues/697

This way even if `Encoding.default_external` is set to a weird value
the document will be parsed just fine.

3a8505a8fa
2024-11-11 09:40:11 +09:00
Sutou Kouhei
a23c6db5c5 [ruby/fiddle] Accept Symbol as Function name again
GitHub: fix https://github.com/ruby/fiddle/pull/159

It's used by FFI test. So Symbol may be used by other use cases.

https://github.com/ruby/fiddle/pull/139 introduced the "Function name is String" limitation. This
commit removed the limitation.

Reported by Mamoru TASAKA. Thanks!!!

cea30fe5f9
2024-11-11 09:19:48 +09:00
tomoya ishida
5eaa4c76c6 [ruby/reline] Remove unused things from reline/unicode.rb
(https://github.com/ruby/reline/pull/759)

* Remove garbage(nil) from Reline::Unicode.split_by_width result

* Remove unused width from Reline::Unicode vi_ ed_ em_ method return value

* Remove unused height from Unicode.split_by_width return value

* Rename split_by_width to split_line_by_width and add legacy split_by_width for IRB

f32446ebc4
2024-11-10 19:11:58 +00:00
KJ Tsanaktsidis
2694585fb3 Fix race condition in test_self_stop
This test was relying on a sleep to synchronise the parent and child
processes. By having the child be the process that stops itself with
SIGSTOP, instead of the parent, we can actually properly wait for that
using waitpid2 and be notified of the stop.

This use of sleep to synchronise processes is potentially flaky and
caused failures under rr's `--chaos` mode.
2024-11-08 22:13:15 -07:00
John Hawthorn
1f6dd9071c Fix update_coderange for binary strings
Although a binary (aka ASCII-8BIT) string will never have a broken
coderange, it still has to differentiate between "valid" and "7bit".

On Ruby 3.4/trunk this problem is masked because we now clear the
coderange more agressively in rb_str_resize, and we happened to always
be strinking this string, but we should not assume that.

On Ruby 3.3 this created strings where `ascii_only?` was true in cases
it shouldn't be as well as other problems.

Fixes [Bug #20883]

Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
Co-authored-by: Matthew Draper <matthew@trebex.net>
2024-11-08 17:39:19 -08:00
Peter Zhu
51ffef2819 Fix memory leak in prism when syntax error in iseq compilation
If there's a syntax error during iseq compilation then prism would leak
memory because it would not free the pm_parse_result_t.

This commit changes pm_iseq_new_with_opt to have a rb_protect to catch
when an error is raised, and return NULL and set error_state to a value
that can be raised by calling rb_jump_tag after memory has been freed.

For example:

    10.times do
      10_000.times do
        eval("/[/=~s")
      rescue SyntaxError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    39280
    68736
    99232
    128864
    158896
    188208
    217344
    246304
    275376
    304592

After:

    12192
    13200
    14256
    14848
    16000
    16000
    16000
    16064
    17232
    17952
2024-11-08 15:43:41 -05:00
Haldun Bayhantopcu
6767117b07 [ruby/prism] Fix splat after kwsplat
70c1cd480f
2024-11-08 15:50:54 +00:00
Yusuke Endoh
fde6d189b2 Avoid calculating large powers
... for slow CI machines like macOS.
2024-11-08 20:53:21 +09:00
Yusuke Endoh
45cd4a8296 Do not round a**b to infinity
... instead, just calculate the value unless it is too big.
Also, this change raises an ArgumentError if it is expected to exceed
16 GB in a 64-bit environment.

(It is possible to calculate it straightforward, but it would likely be
out-of-memory, so I didn't think it would make sense.)

[Feature #20811]
2024-11-08 19:48:56 +09:00
Hiroshi SHIBATA
d7c65398e0 [ruby/uri] Added more fallback constants like URI::PARTTERN and URI::REGEXP
Fixed https://github.com/ruby/uri/issues/125

1f3d3df02a
2024-11-08 06:06:30 +00:00
YO4
b99c17a95b [ruby/reline] Windows fix (https://github.com/ruby/reline/pull/775)
* test_yamatanooroti: close tempfile before unlink

* test_yamatanooroti: omit because of windows does not support job control

* test_yamatanooroti: change startup message detection for windows

* windows.rb: can call win32api using nil as NULL for pointer argument

Exception occurred when interrupted with Ctrl+C on legacy conhost

* windows.rb: fix get_screen_size

return [window height, buffer width] insted of [buffer height, buffer width]

* windows.rb: import scroll_down() from ansi.rb

* windows.rb: add auto linewrap control if VT output not supported (legacy console)

* unfreeze WIN32API pointer arguments

They internally duplicate arguments so api functions write to another place.
This breaks the console mode detection with ruby-head.

* remove useless code from Win32API#call

argument repacking and return value tweaking is not needed for Reline::Windows requirements.

* Correctly handle top of console viewport

* Revert "remove useless code from Win32API#call"

This reverts commit 060ba140ed.

* Revert "windows.rb: can call win32api using nil as NULL for pointer argument"

This reverts commit 93a23bc5d0.

47c1ffbabe
2024-11-08 03:28:57 +00:00
Kaíque Kandy Koga
9523f53465
[ruby/benchmark] Adjust ljust Benchmark#bm with labels was not using the highest length among the labels to adjust the correct ljust. Instead of printing the result during the report generation, now it is waiting to print the result once it is generated.
Benchmark.bm { |x|
  x.item("aaaa") { 1 }
  x.item("aaaaaaaa") { 0 }
}

After

              user     system      total        real
aaaa      0.000005   0.000002   0.000007 (  0.000003)
aaaaaaaa  0.000001   0.000001   0.000002 (  0.000002)

Before

       user     system      total        real
aaaa  0.000005   0.000001   0.000006 (  0.000003)
aaaaaaaa  0.000002   0.000001   0.000003 (  0.000003)

3e74533ead
2024-11-08 11:32:30 +09:00
Nobuyoshi Nakada
ee29aade1a
[Bug #20869] Discard read buffer and encoding converters at seeking 2024-11-07 21:03:37 +09:00
Nobuyoshi Nakada
1701ddea73
[Bug #20802] Copy encodings at reopen as well 2024-11-07 19:33:41 +09:00
Yusuke Endoh
f87ec81e08 Prevent method redefinition warnings in test 2024-11-07 13:18:57 +09:00
Yusuke Endoh
6a39e6fc2d Prevent a warning: ambiguous / 2024-11-07 13:18:57 +09:00
Hartley McGuire
342455e56f [ruby/logger] Enable subclasses to configure level isolation
(https://github.com/ruby/logger/pull/103)

`Logger#with_level` was recently added to enable configuring a
`Logger`'s level for the duration of a block. However, the configured
level is always tied to the currently running `Fiber`, which is not
always ideal in applications that mix `Thread`s and `Fiber`s.

For example, Active Support has provided a similar feature
(`ActiveSupport::Logger#log_at`) which, depending on configuration, can
be isolated to either `Thread`s or `Fiber`s.

This commit enables subclasses of `Logger` to customize the level
isolation. Ideally, it will enable replacing most of Active Support's
`#log_at`, since both methods end up serving the same purpose.

dae2b832cd
2024-11-07 00:17:16 +00:00
Nobuyoshi Nakada
8254f6492c [ruby/json] Categorize deprecated warning
1acce7aceb
2024-11-06 23:31:30 +01:00
Kasumi Hanazuki
35bf660337
io_buffer.c: Allow copies between overlapping buffers with #copy and #set_string (#11640)
The current implementation of `IO::Buffer#copy` and `#set_string` has
an undefined behavior when the source and destination memory overlaps,
due to the underlying use of the `memcpy` C function.

This patch guarantees the methods to be safe even when copying between
overlapping buffers by replacing `memcpy` with `memmove`,

Fixes: [Bug #20745]
2024-11-06 20:55:39 +00:00
Peter Zhu
56ecc243e2 [Bug #20868] Fix Method#hash to not change after compaction
The hash value of a Method must remain constant after a compaction, otherwise
it may not work as the key in a hash table.

For example:

    def a; end

    # Need this method here because otherwise the iseq may be on the C stack
    # which would get pinned and not move during compaction
    def get_hash
      method(:a).hash
    end

    puts get_hash # => 2993401401091578131

    GC.verify_compaction_references(expand_heap: true, toward: :empty)

    puts get_hash # => -2162775864511574135
2024-11-06 10:34:20 -05:00
KJ Tsanaktsidis
96e695ad00 Fix flakiness in TestGc#test_thrashing_for_young_objects
I caught a reproduction of this test failing under rr, and was able to
replay it to isolate the failure. The call to
`before_stat_heap = GC.stat_heap` is itself allocating a hash, which in
unlucky circumstances can result in a new page being allocated and thus
`before_stats[:heap_allocated_pages]` no longer equals
`after_stats[:heap_allocated_pages]`.

The solution is to use the form of GC.stat/stat_heap which takes a hash
as an argument, and thus needs to perform no Ruby allocations itself.
2024-11-06 09:58:47 -05:00
David Rodríguez
350baed6a9 [rubygems/rubygems] Fix manifest in gem package using incorrect platform sometimes
If a gem package is built from a specification whose platform has been
modified, it will include metadata using the old platform.

This change should fix the problem by making sure `original_platform` is
always properly set.

ecd5cd4547
2024-11-06 11:37:29 +00:00
Samuel Williams
3b9896acfc
Revert "Introduce Fiber Scheduler blocking_region hook. (#11963)" (#12013)
This reverts some of commit 87fb44dff6.

We will rename and propose a slightly different interface.
2024-11-06 22:19:40 +13:00
Hiroshi SHIBATA
a2dd471d9e
macOS 15 is the stable version today 2024-11-06 15:45:32 +09:00
Nobuyoshi Nakada
d71be7274b
[Bug #20873] Consider -FIXNUM_MIN overflow
`-FIXNUM_MIN` is usually greater than `FIXNUM_MAX` on platforms using
two's complement representation.
2024-11-06 13:31:38 +09:00
Koichi Sasada
ab7ab9e450 Warning[:strict_unused_block]
to show unused block warning strictly.

```ruby
class C
  def f = nil
end

class D
  def f = yield
end

[C.new, D.new].each{|obj| obj.f{}}
```

In this case, `D#f` accepts a block. However `C#f` doesn't
accept a block. There are some cases passing a block with
`obj.f{}` where `obj` is `C` or `D`. To avoid warnings on
such cases, "unused block warning" will be warned only if
there is not same name which accepts a block.
On the above example, `C.new.f{}` doesn't show any warnings
because there is a same name `D#f` which accepts a block.

We call this default behavior as "relax mode".

`strict_unused_block` new warning category changes from
"relax mode" to "strict mode", we don't check same name
methods and `C.new.f{}` will be warned.

[Feature #15554]
2024-11-06 11:06:18 +09:00
Jean Boussier
4120f2babd [ruby/json] Appease ruby/ruby CI
bc2c970ce4
2024-11-05 18:00:36 +01:00
Jean Boussier
633f65e471 [ruby/json] Strip whitespaces
e85107197b
2024-11-05 18:00:36 +01:00
Jean Boussier
ca8f21ace8 [ruby/json] Resync 2024-11-05 18:00:36 +01:00
Benoit Daloze
e8522f06b5 [ruby/json] Re-enable passing test
82d21f01c5
2024-11-05 18:00:36 +01:00
Jean Boussier
f664e7eaab [ruby/json] Add tests for the behavior of JSON.generate with base types subclasses
Ref: https://github.com/ruby/json/pull/674
Ref: https://github.com/ruby/json/pull/668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.

614921dcef
2024-11-05 18:00:36 +01:00
Jean Boussier
ed22e68379 [ruby/json] JSON::Ext::Parser mark the name cache entries when not on the heap
This is somewhat dead code as unless you are using `JSON::Parser.new`
direcltly we never allocate `JSON::Ext::Parser` anymore.

But still, we should mark all its reference in case some code out there
uses that.

Followup: #675

8bf74a977b
2024-11-05 18:00:36 +01:00
Nobuyoshi Nakada
74fcf43767
Use wide character startup
Mingw crt-git 12.0.0.r369.g0d4221712-1 now prohibits "command line
contains characters that are not supported in the active code page".
0d42217123/

Already Ruby builds `argv` in `rb_w32_sysinit`, instead of mswin- or
mingw-made `argv`.  Just bypass the conversion in mingw crt.
2024-11-05 23:04:49 +09:00
Nobuyoshi Nakada
348a534153 [ruby/stringio] Copy from the relocated string
When ungetting the string same as the same buffer string, extending
the buffer can move the pointer in the argument.  Reported by manun
Manu (manun) at https://hackerone.com/reports/2805165.

95c1194832
2024-11-05 05:01:03 +00:00
Nobuyoshi Nakada
e4f5fd8e5f [ruby/date] Refine assertions
1970b740d0
2024-11-05 04:19:39 +00:00
Dmitrii Zudin
ee4599dbe7 [ruby/date] Fix incorrect argc2 decrement in datetime_s_iso8601 function
Replace the decrement (argc2--) with an increment (argc2++) for
the correct number of arguments when opt is provided.

b6974b00d8
2024-11-05 03:38:13 +00:00
Alexis Bernard
7ae4b858b3 [ruby/rdoc] Display class ancestors in the sidebar #1183
50dda13426
2024-11-04 17:34:27 +00:00
Takashi Kokubun
478e0fc710
YJIT: Replace Array#each only when YJIT is enabled (#11955)
* YJIT: Replace Array#each only when YJIT is enabled

* Add comments about BUILTIN_ATTR_C_TRACE

* Make Ruby Array#each available with --yjit as well

* Fix all paths that expect a C location

* Use method_basic_definition_p to detect patches

* Copy a comment about C_TRACE flag to compilers

* Rephrase a comment about add_yjit_hook

* Give METHOD_ENTRY_BASIC flag to Array#each

* Add --yjit-c-builtin option

* Allow inconsistent source_location in test-spec

* Refactor a check of BUILTIN_ATTR_C_TRACE

* Set METHOD_ENTRY_BASIC without touching vm->running
2024-11-04 11:14:28 -05:00
Nobuyoshi Nakada
4dc120cb7f
Fix Dir.mktmpdir argument
The temporary directory is the second argument.
2024-11-04 23:32:25 +09:00
Nobuyoshi Nakada
09874e90bf
Mingw: Exclude failing tests due to the crt change
Mingw crt-git 12.0.0.r369.g0d4221712-1 now prohibits "command line
contains characters that are not supported in the active code page".
0d42217123/

Provisionally exclude tests that fail by passing such characters.
2024-11-04 21:12:49 +09:00
David Rodríguez
e7518a7859 Validate user input encoding
If the user has the encoding of her system messed up, she may end up
sending us incorrectly encoding input, causing "invalid byte sequence in
UTF-8" errors at random places.

These errors can be forced on a system without encoding issues with
something like:

```
$ gem install$(echo -e "\xFF") foo
/Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/config_file.rb:534:in `block in set_config_file_name': invalid byte sequence in UTF-8 (ArgumentError)
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/config_file.rb:530:in `each'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/config_file.rb:530:in `set_config_file_name'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/config_file.rb:177:in `initialize'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/gem_runner.rb:71:in `new'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/gem_runner.rb:71:in `do_configuration'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/lib/ruby/site_ruby/3.2.0/rubygems/gem_runner.rb:33:in `run'
	from /Users/deivid/.asdf/installs/ruby/3.2.1/bin/gem:10:in `<main>'
```

This commit makes RubyGems print a better error in this case:

```
$ ruby -Ilib bin/gem install$(echo -e "\xFF") foo
/Users/deivid/Code/rubygems/rubygems/lib/rubygems/gem_runner.rb:75:in `validate_encoding': invalid argument: 'install�' has invalid encoding (Gem::OptionParser::InvalidArgument)
	from /Users/deivid/Code/rubygems/rubygems/lib/rubygems/gem_runner.rb:31:in `run'
	from bin/gem:10:in `<main>'
```
2024-11-04 10:18:55 +00:00
David Rodríguez
9ce1b5e11f [rubygems/rubygems] Fix commands with 2 MFA requests when webauthn is enabled
If a command requires two MFA authenticated requests, and webauthn is
enabled, then first one will succeed but the second one will fail
because it tries to reuse the OTP code from the first request and that
does not work.

This happens when you have not yet logged in to rubygems.org, or when
you have an API key with invalid scopes for the current operation. In
that case, we need:

* An API request to get a token or change scopes for the one that you
  have.
* Another API request to perform the actual operation.

Instead of trying to reuse the token, make sure it's cleared so we are
asked to authenticate again. We only do this when webauthn is enabled
because reusing TOPT tokens otherwise is allowed and I don't want to
break that.

669e343935
2024-11-04 10:04:58 +00:00
David Rodríguez
1b190b342b [rubygems/rubygems] TermError should inherit from SystemExit
The `gem owner` command rescues standard errors, but does not rescue
SystemExit errors. If TermError is a standard error, not a system exit,
tests don't behave like realworld for this command.

cf7d500f4d
2024-11-04 10:04:58 +00:00
Kevin Newton
21af248f92 [ruby/prism] Error for def ivar
232a02acef
2024-11-03 18:43:18 +00:00
Peter Zhu
3e2ee99057 Skip TestProc#test_hash_does_not_change_after_compaction if compaction is not supported 2024-11-02 17:25:16 -04:00