tjanczuk / tripwire

Break out from scripts blocking the node.js event loop
Other
99 stars 18 forks source link

recover from tripwire firing #8

Open jacksonloper opened 9 years ago

jacksonloper commented 9 years ago

After the tripwire fires, my script exits, whether I call process.exit(1) or not.

For example, this code...

var tripwire = require("tripwire");

process.on('uncaughtException', function (e) {
   console.log('Doom');
});

tripwire.resetTripwire(2000);

console.log("Hello")
try { 
    while(true) {}
} catch(err) {
    console.log("Goodbye");
}

... prints "Hello" and "Doom" but not "Goodbye"

I suppose there's no way around this. It's presumably sort of the same problem as #1.

rpaterson commented 8 years ago

This is the nature of uncaughtException - tripwire can't cause the while statement to throw an error so you never get to your catch block.

kristianoye commented 6 years ago

So I would not expect the original code block to continue, but what exactly happens internally? It appears that the code/callback that caused the block has stopped, but is there no way to continue flush the event loop and continue with the process?

I added a callback in my context so that uncaughtException executes the callback in my game:

who Kriton the newbie

eval while(true);

*boom uncaughtException fires, I try and clear/reset tripWire and exec the callback which prints:

Command exceeded max execution time. <-- This was printed after the uncaught handler.

But now I am stuck in a loop where tripwire is firing an exception every 2000ms and I can't seem to clear or reset it. Here is what I am trying:


            process.on('uncaughtException', err => {
                let ctx = tripwire.getContext();
                if (ctx) {
                    ctx.callback(ctx.input);
                    setImmediate(() => {
                        tripwire.clearTripwire();
                    });
                }
                logger.log(err);
                logger.log(err.stack);
                this.errorHandler(err, false);
            });
kristianoye commented 6 years ago

I see V8 has a "CancelTerminateExecution()" that seems like it should be able to resume the original isolate/script once the event loop is all cleared out. My C++ is super rusty, though. I have created a fork of this project to try and get a working proof-of-concept.