Issue #17488 has been updated by marcandre (Marc-Andre Lafortune).


I didn't run a git bisect, but this was the case already in `ruby 3.0.0preview1`

----------------------------------------
Bug #17488: Regression in Ruby 3: Hash#key? is non-deterministic when argument uses DelegateClass
https://bugs.ruby-lang.org/issues/17488#change-89620

* Author: myronmarston (Myron Marston)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Upon upgrading a library to run on Ruby 3.0, I have observed that `Hash#key?` has non-deterministic behavior when the argument uses `DelegateClass`. This non-deterministic behavior was not present in Ruby 2.7.

Reproducing this is slightly difficult; the behavior appears to be deterministic (but not necessarily correct) within a single ruby process. To reproduce the non-determinism, you need to start ruby many times to observe different results.  My script below does this.

## Reproduction script

``` ruby
puts "Running on Ruby: #{RUBY_DESCRIPTION}"

program = <<~EOS
  require "delegate"
  TypeName = DelegateClass(String)

  hash = {
    "Int" => true,
    "Float" => true,
    "String" => true,
    "Boolean" => true,
    "WidgetFilter" => true,
    "WidgetAggregation" => true,
    "WidgetEdge" => true,
    "WidgetSortOrder" => true,
    "WidgetGrouping" => true,
  }

  puts hash.key?(TypeName.new("WidgetAggregation"))
EOS

iterations = 20
results = iterations.times.map { `ruby -e '#{program}'`.chomp }.tally

puts "Results of checking `Hash#key?` #{iterations} times: #{results.inspect}"
```

Put this in a file like `ruby3_hash_bug.rb`, and run it using either Ruby 2.7 (to see `Hash#key?` consistently return `true`) or Ruby 3.0 (to see `Hash#key?` produce non-deterministic behavior).

## Ruby 2.7 results

```
Running on Ruby: ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Results of checking `Hash#key?` 20 times: {"true"=>20}
```

## Ruby 3.0 results

```
Running on Ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
Results of checking `Hash#key?` 20 times: {"true"=>12, "false"=>8}
```

Note that the ratio of `true` to `false` is non-deterministic; here are a couple other runs on Ruby 3.0 with different results:

```
Running on Ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
Results of checking `Hash#key?` 20 times: {"false"=>7, "true"=>13}
```

```
Running on Ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
Results of checking `Hash#key?` 20 times: {"true"=>11, "false"=>9}
```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>