Skip to content

In version 6.X, state is lost after an exception is thrown  #123

Closed
@Taritsyn

Description

@Taritsyn

Hello, Jeremy!

After upgrading from version 5.4.4 to 6.1.0, some my tests began to fail. Upon a detailed research, it turned out that after any errors occur in JavaScript code, state is lost.

To reproduce this error, I wrote a console application:

using System;
using System.Threading.Tasks;

using Jering.Javascript.NodeJS;

namespace TestJeringNodeJS
{
    public class Program
    {
        const string MODULE_FILE_NAME = "counter.js";
        const string MODULE_CODE = @"
let counter = 0;

module.exports.increaseCounter = (callback, num) => {
    if (num < 0) {
        throw new Error('The value cannot be negative!');
    }

    let result = counter += num;
    callback(null, result);
}";


        public static void Main(string[] args)
        {
            IncreaseCounter(3);
            IncreaseCounter(4);
            IncreaseCounter(-1);
            IncreaseCounter(5);
        }

        private static void IncreaseCounter(int num)
        {
            try
            {
                int result = InvokeEngineHelper<int>("increaseCounter", new object[] { num });

                Console.WriteLine("Result: {0}", result);
            }
            catch (InvocationException e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }

        private static T InvokeEngineHelper<T>(string exportName = null, object[] args = null)
        {
            Task<(bool, T)> cachedTask = StaticNodeJSService.TryInvokeFromCacheAsync<T>(MODULE_FILE_NAME,
                exportName, args);
            (bool success, T result) = cachedTask.ConfigureAwait(false).GetAwaiter().GetResult();

            if (success)
            {
                return result;
            }
            else
            {
                Task<T> task = StaticNodeJSService.InvokeFromStringAsync<T>(MODULE_CODE, MODULE_FILE_NAME,
                    exportName, args);

                return task.ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
    }
}

When using version 5.4.4, state after the error occurs is preserved:

Result: 3
Result: 7
The value cannot be negative!
Error: The value cannot be negative!
    at module.exports.increaseCounter (anonymous:6:15)
    at Server.<anonymous> ([eval]:1:4360)
    at Generator.next (<anonymous>)
    at [eval]:1:1271
    at new Promise (<anonymous>)
    at r ([eval]:1:1016)
    at IncomingMessage.<anonymous> ([eval]:1:2634)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Result: 12

When using version 6.1.0, state after the error occurs is lost:

Result: 3
Result: 7
The value cannot be negative!
Error: The value cannot be negative!
    at module.exports.increaseCounter (anonymous:6:15)
    at Server.<anonymous> ([eval]:1:2443)
    at Generator.next (<anonymous>)
    at [eval]:1:355
    at new Promise (<anonymous>)
    at o ([eval]:1:100)
    at IncomingMessage.<anonymous> ([eval]:1:557)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Result: 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions