Taritsyn / JavaScriptEngineSwitcher

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines. This library allows you to quickly and easily switch to using of another JavaScript engine.
Apache License 2.0
440 stars 49 forks source link

Process is terminating due to StackOverflowException. #48

Closed stuart-beattie closed 6 years ago

stuart-beattie commented 6 years ago

Hi, I'm referencing the 3.0.0.0-alpha 9 prerelease of the javascript engine switcher library within an application hosted in a Linux container running latest ASP.NET core and it works! - except that the process crashes sometime after a call to the engine's evaluate function :(

Is there anything I can do to resolve this? Do you have any ideas as to why this might be happening?

I should say that I am referencing the "JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64" nuget package and directly instantiating the ChakraCoreJsEngine.

My service code is really simple and I'm not executing any complex JS although the string passed may contain a reasonably large json object for the script to work against. Am I cleaning things up correctly?

public class JsService : IJsService
{
        public JsService()
        {
        }

        public T Execute<T>(string script)
        {
            using (var context = CreateContext())
            {
                return context.Execute<T>(script);
            }
        }

        public JsContext CreateContext()
        {
            return new JsContext();
        }
}

public class JsContext : IDisposable
{
        IJsEngine Engine { get; }

        public JsContext()
        {
            Engine = new ChakraCoreJsEngine(new ChakraCoreSettings { DisableEval = true });
        }

        public T Execute<T>(string script)
        {
            return Engine.Evaluate<T>(script);
        }

        public void Dispose()
        {
            Engine.Dispose();
        }
}

Thanks, Stuart

Taritsyn commented 6 years ago

Hello, Stuart!

Am I cleaning things up correctly?

Everything looks normal.

You didn't think, that the reason of stack overflow might be in your JS code? Show me a full error message.

stuart-beattie commented 6 years ago

I'm certainly wrapping the code passed in within a JS try catch block, and actually it doesn't bring the process down straight away, the process ends abruptly sometime afterwards. Unfortunately that's the only error message I'm seeing in the console log - "Process is terminating due to StackOverflowException". I've just realised that I'm using the prerelease version of the library when I don't actually have to so first of all I'm reverting to v2.4.12.

Taritsyn commented 6 years ago

Unfortunately that's the only error message I'm seeing in the console log - "Process is terminating due to StackOverflowException".

It does not look like, that this error is caused by the ChakraCoreJsEngine.

I'm certainly wrapping the code passed in within a JS try catch block, …

Try instead to handle errors as follows:

…
public T Execute<T>(string script)
{
    T result = default(T);

    try
    {
        result = Engine.Evaluate<T>(script);
    }
    catch (JsException e)
    {
        // Your error handling code
    }

    return result;
}
…

In addition, you need to think about reusing a JS engine instances, and not create a instance for each request. Try to use the JavaScript Engine Switcher library with the JSPool (compatible with version 2.X).

Divide your JS code into two parts:

  1. Initialization code, that will be executed only once by using the Execute method.
  2. Сode of immediate call or evaluation, that is executed by the Evaluate method (also do not forget about the CallFunction method).
stuart-beattie commented 6 years ago

Thanks, it is a strange one. I am catching c# exceptions but the process falls over at a later point. If I don't call the JS engine, the error doesn't occur so it does seem to be related. I'll update when I have a resolution. Thanks for your advice on managing engine instances.

Taritsyn commented 6 years ago

I am catching c# exceptions but the process falls over at a later point.

It is possible, that this is due to a lack of resources. Therefore, I recommend that you follow my advice.

Taritsyn commented 6 years ago

Hello, Stuart!

Is this error still relevant?

stuart-beattie commented 6 years ago

It is yes but nothing to do with JavascriptEngineSwitcher. Seems to be a problem with ChakraCore. I have raised the issue with them.

Taritsyn commented 6 years ago

As far as I know, this error has been fixed in ChakraCore version 1.8.3. Try to upgrade the JavaScript Engine Switcher to version 2.4.15 or 3.0.0 Beta 2.