Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Inconsistencies with MRI regarding hashes and arrays affecting rails #411

Closed
jvshahid opened this Issue Nov 26, 2012 · 12 comments

Comments

Projects
None yet
3 participants
Contributor

jvshahid commented Nov 26, 2012

Hi there,

I'm currently working on a drop in replacement gem for ruby-pg under JRuby. While running the active record test suite on the gem, I realized the following inconsistencies between JRuby and MRI:

  1. Let's start with the easy one less important:
#!/usr/bin/env ruby

hash = {:foo => :bar}
hash.freeze
hash[:foo] = ":bazz"

Under MRI this code will throw the following error:

./test_hash.rb:5:in `[]=': can't modify frozen Hash (RuntimeError)
    from ./test_hash.rb:5:in `<main>'

Under JRuby it will throw the following:

RuntimeError: can't modify frozen hash
     []= at org/jruby/RubyHash.java:903
  (root) at ./test_hash.rb:5

What's relevant here is the case of Hash vs. hash. Surprisingly, there's a test in rails that test the actual error message and it's not case insensitive. I guess the question now is what's the right course of action here.

  1. This is more relevant, since it affects the behavior of active record's associations behavior under JRuby, which is currently faulty for the following reasons:

First the bug, is in Array::delete. Although the ruby docs say that Array::delete(obj) will return obj if an object was found (and deleted) that's equal to obj. But the actual implementation returns the last object in the array that was equal to obj instead of obj (as seen in the source code and docs here). On the other hand JRuby do the right thing and return obj. I know, I said JRuby is doing the right thing, but it's still a difference between the two implementations and MRI is the defacto standard. I also don't know what's the right course of action which is why I'm posting this here to discuss.

A test case to explain the difference in behavior is:

#!/usr/bin/env ruby

class Foo
  attr_reader :name, :age

  def initialize name, age
    @name = name
    @age = age
  end

  def == other
    other.name == name
  end
end

foo1 = Foo.new "John Shahid", 27
foo2 = Foo.new "John Shahid", 28
array = [foo1]
temp = array.delete foo2
puts "#{temp.age} should equal 28"

Output under MRI: 27 should equal 28
Under JRuby: 28 should equal 28

In case you were wondering where this is relevant or someone had the same problem in rails. The problem is in activerecord/lib/active_record/associations/collection_association.rb:422 where the code tries to merge in memory records that could possibly be modified with records that were just retrieved from the db (as shown below). In MRI mem_record will hold the memory record since we're deleting from the memory array, while JRuby will return the object we're trying to delete which is the persisted record. The symptom of this bug is that you loose your memory modifications if you don't save the record to the db, since everytime you merge the persisted data will override what's in memory.

persisted.map! do |record|
  if mem_record = memory.delete(record)
    puts "record.attribute_names & mem_record.attribute_names: #{record.attribute_names & mem_record.attribute_names}"
    puts "mem_record.changes: #{mem_record.changes.keys}"

    ((record.attribute_names & mem_record.attribute_names) - mem_record.changes.keys).each do |name|
      mem_record[name] = record[name]
    end

    mem_record
  else
    record
  end
end

Sorry for this lengthy issue, I tried to be thorough as much as I can. I'm happy to write the specs in ruby-specs and send a patch to make the spec pass. I don't know which implementation should be fixed and want everyone to be on the same page (including the MRI guys if this will involve a change in their implementation) before I go on.

Owner

BanzaiMan commented Nov 26, 2012

Please avoid aggregating disparate issues into one ticket. It makes discussion really confusing. I can fixed the first issue (since it's a simple fix to align error message more closely) with aa2e98c, so there is no need to split this one.

Contributor

jvshahid commented Nov 26, 2012

I apologize for aggregating the issues and thanks for the fix. Should we add a spec for this case in ruby spec or it's not worth the effort ?

Owner

BanzaiMan commented Nov 26, 2012

It's not necessary. It is a bad idea to rely on error message, but I guess there is no distinction between this kind of RuntimeError (attempts to modify frozen object, a Hash) and others.

@BanzaiMan BanzaiMan closed this in fe138bf Nov 26, 2012

Contributor

jvshahid commented Nov 26, 2012

Thanks for fixing those bugs.

I have a problem though. The reason I opened this issue is to discuss which implementation is wrong and try to fix it. The changes you pushed aren't tested which means that someone can change this behavior without knowing in the future.

The second issue, is that the ruby documentation is not right, so either MRI has to change to match the docs or JRuby has to change its behavior (which is the path you chose) and the docs have to be fixed.

Owner

BanzaiMan commented Nov 26, 2012

@jvshahid Oh. I see what you are saying now. I apologize for the confusion. I think this is an MRI bug, after all. I'm going to revert the changes, and open a bug with MRI.

@BanzaiMan BanzaiMan reopened this Nov 26, 2012

BanzaiMan added a commit that referenced this issue Nov 26, 2012

@ghost ghost assigned BanzaiMan Nov 26, 2012

Owner

BanzaiMan commented Nov 26, 2012

By the way, the test should go into RubySpec.

Owner

headius commented Nov 26, 2012

Looks like the behavior change was explicit and intentional, so we'll need to follow suit with MRI here.

Owner

BanzaiMan commented Nov 26, 2012

I expect MRI to retain its current behavior and correct the documentation. Once we have that confirmed, I will re-apply the fix, and propose the spec to RubySpec (or inherit MRI's new test).

BanzaiMan added a commit that referenced this issue Nov 26, 2012

Array#delete should return the last object *inside* the array, not th…
…e one that is given as the argument to it, even when the objects are equal via '=='.

This addresses #411.
Owner

BanzaiMan commented Nov 26, 2012

Re-pushed the fix. Spec will be pushed to RubySpec.

@BanzaiMan BanzaiMan closed this Nov 26, 2012

Contributor

jvshahid commented Nov 27, 2012

Thanks.

prathamesh-sonpatki added a commit to prathamesh-sonpatki/jruby that referenced this issue Dec 3, 2012

Array#delete should delete the first object *inside* the array,
not the one that is given as the argument to it, even when the objects
are equal via '=='.

This fixes #411.

prathamesh-sonpatki added a commit to prathamesh-sonpatki/jruby that referenced this issue Dec 3, 2012

prathamesh-sonpatki added a commit to prathamesh-sonpatki/jruby that referenced this issue Dec 3, 2012

prathamesh-sonpatki added a commit to prathamesh-sonpatki/jruby that referenced this issue Dec 3, 2012

Array#delete should return the last object *inside* the array, not th…
…e one that is given as the argument to it, even when the objects are equal via '=='.

This addresses #411.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment