JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-4565

Adding new key to a Hash during iteration should always raise an exception

    Details

    • Number of attachments :
      0

      Description

      New RubySpec enforces this:

      commit 2ae272118ce7e0068d5087cd321a8aa551ced581
      Author: Yusuke Endoh <mame@tsg.ne.jp>
      Date:   Wed Feb 17 01:29:16 2010 +0900
      
          adding new key into hash during iteration always raises an exception
      
          matz agreed.  [ruby-core:23614]
      
      jruby -S mspec ci core\hash
      jruby 1.5.0.dev (ruby 1.8.7 patchlevel 174) (2010-02-16 2770c7f) (Java HotSpot(TM) Client VM 1.6.0_18) [x86-java]
      ...............F....................F.........F........
      
      1)
      Hash#[]= raises RuntimeErrors if attempt to add new key during iteration FAILED
      Expected RuntimeError but no exception was raised
      D:/work/jruby-dev/rubyspec.git/core/hash/shared/store.rb:56
      D:/work/jruby-dev/rubyspec.git/core/hash/element_set_spec.rb:5
      D:/work/jruby-dev/rubyspec.git/core/hash/element_set_spec.rb:56:in `load'
      D:/work/jruby-dev/mspec.git/bin/mspec-ci:8
      
      2)
      Hash#merge! raises RuntimeErrors if attempt to add new key during iteration FAILED
      Expected RuntimeError but no exception was raised
      D:/work/jruby-dev/rubyspec.git/core/hash/merge_spec.rb:66
      D:/work/jruby-dev/rubyspec.git/core/hash/merge_spec.rb:58
      D:/work/jruby-dev/rubyspec.git/core/hash/merge_spec.rb:56:in `load'
      D:/work/jruby-dev/mspec.git/bin/mspec-ci:8
      
      3)
      Hash#store raises RuntimeErrors if attempt to add new key during iteration FAILED
      Expected RuntimeError but no exception was raised
      D:/work/jruby-dev/rubyspec.git/core/hash/shared/store.rb:56
      D:/work/jruby-dev/rubyspec.git/core/hash/store_spec.rb:5
      D:/work/jruby-dev/rubyspec.git/core/hash/store_spec.rb:56:in `load'
      D:/work/jruby-dev/mspec.git/bin/mspec-ci:8
      
      Finished in 0.781000 seconds
      

        Activity

        Hide
        Hiro Asari added a comment -

        I don't know when it's fixed (since I didn't run bisect), but this is fixed in trunk now.

        $ jruby -v -e 'hash = {1 => 2, 3 => 4, 5 => 6}; hash.each { hash.send(:store, :foo, :bar) }'
        jruby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2010-07-31 e46037a) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [x86_64-java]
        -e:1: can't add a new key into hash during iteration (RuntimeError)
        	from -e:1:in `each'
        	from -e:1
        
        $ jruby -v --1.9 -e 'hash = {1 => 2, 3 => 4, 5 => 6}; hash.each { hash.send(:store, :foo, :bar) }'
        jruby 1.6.0.dev (ruby 1.9.2dev trunk -1) (2010-07-31 e46037a) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [x86_64-java]
        -e:1: can't add a new key into hash during iteration (RuntimeError)
        	from -e:1:in `each'
        	from -e:1
        
        Show
        Hiro Asari added a comment - I don't know when it's fixed (since I didn't run bisect), but this is fixed in trunk now. $ jruby -v -e 'hash = {1 => 2, 3 => 4, 5 => 6}; hash.each { hash.send(:store, :foo, :bar) }' jruby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2010-07-31 e46037a) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [x86_64-java] -e:1: can't add a new key into hash during iteration (RuntimeError) from -e:1:in `each' from -e:1 $ jruby -v --1.9 -e 'hash = {1 => 2, 3 => 4, 5 => 6}; hash.each { hash.send(:store, :foo, :bar) }' jruby 1.6.0.dev (ruby 1.9.2dev trunk -1) (2010-07-31 e46037a) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [x86_64-java] -e:1: can't add a new key into hash during iteration (RuntimeError) from -e:1:in `each' from -e:1
        Hide
        codecraig added a comment -

        This still fails for me. I'm running jruby 1.6.1 (ruby-1.9.2p136) (Java HotSpot 64-Bit Server VM 1.6.0_21) [linux-amd64-java]

        The error occurs at org/jruby/RubyHash.java:895

        Anyone else seeing this?

        Show
        codecraig added a comment - This still fails for me. I'm running jruby 1.6.1 (ruby-1.9.2p136) (Java HotSpot 64-Bit Server VM 1.6.0_21) [linux-amd64-java] The error occurs at org/jruby/RubyHash.java:895 Anyone else seeing this?
        Hide
        Thomas E Enebo added a comment -

        I am getting the expected error to properly occur with Hiro's snippet above (we have some unfortunate noise above it from the new circular require logic). What error are you seeing? If it is a different error than expected; show us a snippet of code you used along with -Xbacktrace.style=raw flag on so we can see an internal stacktrace (a snippet is all we need).

        Show
        Thomas E Enebo added a comment - I am getting the expected error to properly occur with Hiro's snippet above (we have some unfortunate noise above it from the new circular require logic). What error are you seeing? If it is a different error than expected; show us a snippet of code you used along with -Xbacktrace.style=raw flag on so we can see an internal stacktrace (a snippet is all we need).
        Hide
        codecraig added a comment -

        Sorry for being a dolt, I completely misread this and thought that the error ("can't add a key into hash during iteration") was a problem, as opposed to, the expected behavior.

        I do get the error when running Hiro's snippet, but when running my Rails app I'm also seeing this error occur. Here is what I've seen in the logs:

        RuntimeError: can't add a new key into hash during iteration
           org/jruby/RubyHash.java:895:in `[]='
           /jruby/lib/ruby/1.9/webrick/utils.rb:146:in `register'
           /jruby/lib/ruby/1.9/webrick/utils.rb:111:in `register'
           /jruby/lib/ruby/1.9/webrick/utils.rb:110:in `register'
           /jruby/lib/ruby/1.9/webrick/utils.rb:166:in `timeout'
           /jruby/lib/ruby/1.9/webrick/httprequest.rb:347:in `_read_data'
           /jruby/lib/ruby/1.9/webrick/httprequest.rb:358:in `read_line'
           /jruby/lib/ruby/1.9/webrick/httprequest.rb:266:in `read_header'
           /jruby/lib/ruby/1.9/webrick/httprequest.rb:88:in `parse'
           /jruby/lib/ruby/1.9/webrick/httpserver.rb:57:in `run'
           /jruby/lib/ruby/1.9/webrick/server.rb:183:in `start_thread'
        
        Show
        codecraig added a comment - Sorry for being a dolt, I completely misread this and thought that the error ("can't add a key into hash during iteration") was a problem, as opposed to, the expected behavior. I do get the error when running Hiro's snippet, but when running my Rails app I'm also seeing this error occur. Here is what I've seen in the logs: RuntimeError: can't add a new key into hash during iteration org/jruby/RubyHash.java:895:in `[]=' /jruby/lib/ruby/1.9/webrick/utils.rb:146:in `register' /jruby/lib/ruby/1.9/webrick/utils.rb:111:in `register' /jruby/lib/ruby/1.9/webrick/utils.rb:110:in `register' /jruby/lib/ruby/1.9/webrick/utils.rb:166:in `timeout' /jruby/lib/ruby/1.9/webrick/httprequest.rb:347:in `_read_data' /jruby/lib/ruby/1.9/webrick/httprequest.rb:358:in `read_line' /jruby/lib/ruby/1.9/webrick/httprequest.rb:266:in `read_header' /jruby/lib/ruby/1.9/webrick/httprequest.rb:88:in `parse' /jruby/lib/ruby/1.9/webrick/httpserver.rb:57:in `run' /jruby/lib/ruby/1.9/webrick/server.rb:183:in `start_thread'
        Hide
        Thomas E Enebo added a comment -

        Ah yeah that is the expected error. Issues like these can be confusing because depending on the version of MRI you are using you may not see the error. These fall under the category Matz clarifications.

        Show
        Thomas E Enebo added a comment - Ah yeah that is the expected error. Issues like these can be confusing because depending on the version of MRI you are using you may not see the error. These fall under the category Matz clarifications.

          People

          • Assignee:
            Unassigned
            Reporter:
            Vladimir Sizikov
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: