From: nobu@... Date: 2020-12-29T04:43:36+00:00 Subject: [ruby-core:101802] [Ruby master Bug#17488] Regression in Ruby 3: Hash#key? is non-deterministic when argument uses DelegateClass Issue #17488 has been updated by nobu (Nobuyoshi Nakada). Seems commit:9e6e39c3512f7a962c44dc3729c98a0f8be90341 by bisect. ---------------------------------------- Bug #17488: Regression in Ruby 3: Hash#key? is non-deterministic when argument uses DelegateClass https://bugs.ruby-lang.org/issues/17488#change-89629 * 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: