Crash with engine Interrupt() instruction from another thread

Jul 9, 2013 at 10:03 AM
The documentation states that This method can be called safely from any thread

So I made this unit test. Unfortunately it crashes most of time with an access violation in function InternalFieldOK (v8\src\api.cc). The test conditions are ClearScript 5.3.2 or 5.3.3 with default V8 version. Release mode 32 bits.
        [Test]
        public virtual void TestScriptInterruptedCrash()
        {
            var ctx = new PropertyBag();
            using (var engine = new V8ScriptEngine("CrashScript"))
            {
                engine.AddHostObject("context", ctx);

                var startEvent = new ManualResetEventSlim(false);
                ThreadStart main = () =>
                {
                    ctx["startEvent"] = startEvent;
                    ctx["counter"] = 0;
                    try
                    {
                        System.DateTime dtRun = System.DateTime.UtcNow;
                        engine.Execute("dummyDocument.js", @"
for (var i = 0; i < 3000000; i++ )
{
    context.counter++;
    context.startEvent.Set();
}

");
                        Assert.Inconclusive();
                    }
                    catch (Microsoft.ClearScript.ScriptInterruptedException si)
                    {
                        Assert.That(si.Message.Contains("interrupted"));
                    }
                };

                var t = new Thread(main);
                t.Start();
                Assert.That(startEvent.Wait(10000));
                engine.Interrupt();

                t.Join();

                // The thread did some work but was interrupted in the middle
                Assert.Greater(3000000, (Int32)ctx["counter"]);
                Assert.Less(0, (Int32)ctx["counter"]);
            }
        }
I can guess from call stack that it crashes when tring to access to the host object "startEvent"... but the v8 handles and objects are a bit cryptic to debug.

Am-I doing something wrong ? or does the script interruption disposes some ressources at the wrong time ?
Coordinator
Jul 9, 2013 at 2:18 PM
Hello julien_b! We have reproduced this issue and do not believe that you are doing anything wrong. We are investigating.
Coordinator
Jul 9, 2013 at 5:58 PM
Hi julien_b. Could you please download the latest source code and try your test again? We've posted an update that includes a potential fix and a modified version of your test. If the fix works for you, we'll release it as a new version of ClearScript. Thanks for reporting this issue!
Jul 10, 2013 at 1:20 PM
Edited Jul 10, 2013 at 1:20 PM
Hello, I applied the fix and recompiled : it works perfectly ! Thanks.