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

Program crash after function call with too much parameters #82

Closed
CzaiStefanB opened this issue Mar 5, 2020 · 5 comments
Closed

Program crash after function call with too much parameters #82

CzaiStefanB opened this issue Mar 5, 2020 · 5 comments

Comments

@CzaiStefanB
Copy link

CzaiStefanB commented Mar 5, 2020

Hello,

I have a strange behaviour when calling embedded functions with too much parameters.

JavaScriptEngineSwitcher.ChakraCore.JsRt.JsUsageException: Runtime is active on another thread.
bei JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JsErrorCode errorCode)
bei JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass24_0.b__0()
bei JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.ScriptTaskWithResult1.Run() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.Invoke[T](Func1 func)
bei JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.InnerEvaluate(String expression, String documentName)
bei JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.InnerEvaluate[T](String expression, String documentName)
bei JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.InnerEvaluate[T](String expression)
bei JavaScriptEngineSwitcher.Core.JsEngineBase.Evaluate[T](String expression)
bei TestConsoleRun.Program.JavaScriptEngineSwitcherTest2() in Program.cs:Zeile 335.

I think something gets corrupted in background because with the next function call the program is crashing. When calling a function with too less parameters everything is working like expected. You can use the following code to reproduce this behaviour:

        IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
        engineSwitcher.EngineFactories.Add(new ChakraCoreJsEngineFactory());
        engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName;
        IJsEngine javaEngine = engineSwitcher.CreateEngine(ChakraCoreJsEngine.EngineName);
        javaEngine.EmbedHostObject("Sleep", (Action<int>)System.Threading.Thread.Sleep);

        Console.WriteLine("Sleep with correct parameters.");
        Console.WriteLine(javaEngine.Evaluate("Sleep(2000);"));
        try
        {
            Console.WriteLine("Sleep with too less parameters.");
            Console.WriteLine(javaEngine.Evaluate("Sleep();"));   // useful "parameter" exception
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        Console.WriteLine("Sleep with correct parameters.");
        Console.WriteLine(javaEngine.Evaluate("Sleep(2000);"));
        try
        {
            Console.WriteLine("Sleep with too much parameters.");
            Console.WriteLine(javaEngine.Evaluate("Sleep(2000, true);"));   // strange exception ("Runtime is active on another thread."), something is corrupted afterwards
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        Console.WriteLine("Sleep with correct parameters.");
        Console.WriteLine(javaEngine.Evaluate("Sleep(2000);")); // never called because of program crash
        Console.WriteLine("End");
@Taritsyn
Copy link
Owner

Taritsyn commented Mar 5, 2020

Hello!

This error:

Error: During invocation of the host delegate an error has occurred - "Parameter count mismatch.".
   at Global code (Script Document [2]:1:1) -> Sleep();

occurs because you embedded a delegate to JS engine, which can take only one integer parameter:

javaEngine.EmbedHostObject("Sleep", (Action<int>)System.Threading.Thread.Sleep);

You have only two variants for solving this problem:

  1. Embed a several delegates with different names.
  2. Embed a one object or type containing multiple overloads of the Sleep method.

JsUsageException occurs due to the fact that intercepting entire exception, you continue to use JS engine in which error occurred.

@CzaiStefanB
Copy link
Author

CzaiStefanB commented Mar 5, 2020

I'm expecting the first exception with "parameter count mismatch" when I call Sleep without parameter. From my point of view this is correct behaviour and the JS engine also is still working correctly after that call.

But when I call the Sleep with two parameters I get a different exception with "Runtime is active on another thread." and afterwards the JS engine is not working any longer. The whole program is crashing without any exception or comment (I guess due to problem in native code). Why I don't get the same "parameter count mismatch" exception when using too many parameters? I think the runtime should handle that the same way like when having to less parameters and the JS engine should also not be corrupted after such a call.

Taritsyn added a commit that referenced this issue Mar 5, 2020
…sh after function call with too much parameters”
@Taritsyn
Copy link
Owner

Taritsyn commented Mar 5, 2020

I found the cause of error. Tomorrow I will publish a new version.

Thanks for information!

Taritsyn added a commit that referenced this issue Mar 8, 2020
… “Program crash after function call with too much parameters”
@Taritsyn
Copy link
Owner

Taritsyn commented Mar 8, 2020

Hello!

This error is fixed in version 3.4.3.

Now the ChakraCore module, like other engines, discards extra parameters before calling the delegate. No exceptions occur during such call.

@CzaiStefanB
Copy link
Author

I have tested the functionality in the new version and it works like expected. I will close the issue.

Thanks for the quick bug fixing!

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