Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime exception with ConditionalWeakTable, Unity Mono IL2CPP #1279

Closed
CreepGin opened this issue Sep 2, 2022 · 3 comments
Closed

Runtime exception with ConditionalWeakTable, Unity Mono IL2CPP #1279

CreepGin opened this issue Sep 2, 2022 · 3 comments

Comments

@CreepGin
Copy link
Contributor

CreepGin commented Sep 2, 2022

After PR #1227, we are getting the following runtime exception on AOT platforms like Unity IL2CPP.

InvalidCastException: Unable to cast object of type 'FileInfo' to type 'ObjectWrapper'.
  at System.Runtime.CompilerServices.ConditionalWeakTable`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.InteropOptions+<>c.<.ctor>b__59_0 (Jint.Engine engine, System.Object target) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.DefaultObjectConverter.TryConvert (Jint.Engine engine, System.Object value, Jint.Native.JsValue& result) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Native.JsValue.FromObject (Jint.Engine engine, System.Object value) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Native.Object.ObjectInstance.Get (Jint.Native.JsValue property, Jint.Native.JsValue receiver) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Runtime.Interop.ObjectWrapper.Get (Jint.Native.JsValue property, Jint.Native.JsValue receiver) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Engine.GetValue (Jint.Runtime.References.Reference reference, System.Boolean returnReferenceToPool) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue (Jint.Runtime.Interpreter.EvaluationContext context) [0x00000] in <00000000000000000000000000000000>:0 
  at Jint.Runtime.Interpreter.Expressions.JintLogicalOrExpression.EvaluateInternal (Jint.Runtime.Interpreter.EvaluationContext context) [0x00000] in <00000000000000000000000000000000>:0 
...

I think the offending line is

if (engine._objectWrapperCache.TryGetValue(target, out var wrapped))

After a bit of digging, I'm beginning to suspect that Unity/Mono doesn't work well with ConditionalWeakTable.

@lahma
Copy link
Collaborator

lahma commented Sep 2, 2022

I'm at lost with these as I don't do Unity development, is it enough for you to disable the functionality with the switch engine.Options.Interop.TrackObjectWrapperIdentity = false ?

@CreepGin
Copy link
Contributor Author

CreepGin commented Sep 2, 2022

Yes, that is what we did for the time-being. And I understand there's nothing much you can do in this case.

The exception with ConditionalWeakTable doesn't happen in Unity Editor. Only happens in deployed builds (and doesn't reproduce consistently since Unity/Mono's ConditionalWeakTable gets "corrupted" differently on difference machines).

So I think Unity devs using Jint will get tripped by this eventually. But at least now they can search Google and arrive at this thread. 😀

_engine = new Jint.Engine(opts => {
    // Jint uses ConditionalWeakTable for tracking ObjectWrapper identity, but Unity is buggy with ConditionalWeakTable.
    opts.Interop.TrackObjectWrapperIdentity = false;
    ...
}

@CreepGin CreepGin changed the title Regression with the ObjectWrapper identity PR on AOT platforms Unity Mono IL2CPP runtime exception with ConditionalWeakTable Sep 2, 2022
@CreepGin CreepGin changed the title Unity Mono IL2CPP runtime exception with ConditionalWeakTable Runtime exception with ConditionalWeakTable, Unity Mono IL2CPP Sep 2, 2022
@CreepGin CreepGin closed this as completed Sep 2, 2022
@lahma
Copy link
Collaborator

lahma commented Sep 2, 2022

When hopefully eventually have some documentation, we can have some sort unity specific section for best practices..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants