Backstory: I’m trying to use DRb for some in-house utility code. DRb
itself seems to work fine, but I found that when I misspelled a method
name, instead of reporting back a NoMethodError, the IronRuby process
crashed immediately to the console.
This is using a relatively recent build of IronRuby from Github
Steps to repro:
e = RuntimeError.new ‘xyz’
dumped = Marshal.dump e
e2 = Marshal.load dumped
I would expect e2 to be equivalent to e, but instead the process crashes
with this exception
_InvokeConstructor': Exception has been thrown by the target of an invocation. (System::Reflection::TargetInvocationException) from mscorlib:0:inInvokeConstructor’
Invoke' from (ir):1:inload’
Note: This also happens with any CLR type eg System::DateTime.
I looked through IronRuby’s marshalling code, and it appears that the
behaviour of Marshal.dump and Marshal.load don’t align properly.
Marshal.dump is it’s own self-contained set of code which essentially
writes data to a BinaryStream.
For ruby types, it ends up writing a series of values in a format that
looks a lot like what I remember CRuby’s marshal writing.
For CLR types, this just writes the Type name and no instance data (clr
objects don’t have ruby instance variables after all)
Marshal.load does 2 things:
- Reads any ruby instance variables out into an Attributes dictionary
- Uses reflection to find any non-public Constructor(SerializationInfo,
StreamingContext) and invoke it.
For ruby types, this finds the protected RubyClass(SerializationInfo,
StreamingContext) ctor, which calls RubyOps.DeserializeObject, which in
turn reads the attributes dictionary and it’s all fine.
For CLR types, this finds whatever constructor might exist for the CLR
object, which does whatever it does for that type.
Unfortunately because the data that is getting passed into the CLR
deserialization constructor came from Marshal.dump which has no
whatsoever of CLR serialization, the whole thing crashes.
I’m no expert on CLR serialization, so I’d really appreciate some
on this, as I’m not sure what to do here.
As far as I can guess however, I can see two solutions:
Implement Marshal.dump on top of the CLR serialization code so it
matches Marshal.load and should therefore be able to handle CLR types
Don’t allow marshalling of CLR types, and put some special-case code
into any Ruby types that are backed by CLR types (such as Exception) so
these at least can be serialized?
I’m going to have a crack at #1, but I’m not sure how successful this
Again, any feedback would be greatly appreciated.