NodeJS crashes CEF (Chrome engine) when a Node Error is encountered
jingtaotan Jul 17, 2014 7:17 AMI have a HTML5 panel which uses NodeJS and an NPM module called "restify" to create a REST service that runs from inside Premiere Pro.
My code looks something like this:
var SERVICE_PORT = 8080;
var restify = require('restify');
var server = restify.createServer();
function attemptListening() {
server.started = false;
console.log("%s is trying to start...", server.name);
server.listen(SERVICE_PORT);
}
server.on('error', function(err) {
console.log("%s couldn't start, retrying in 5 seconds...", server.name);
setTimeout(attemptListening, 5000);
});
server.once('listening', function() {
console.log('%s listening at %s', server.name, server.url);
server.started = true;
server.removeAllListeners('error');
});
$(window).on('beforeunload', function () {
if (server.started) {
console.log('%s is now closing', server.name);
server.close(function () {
console.log('%s closed', server.name);
});
}
});
There are two problems with how I am currently approaching this:
1. When the user navigates to a different page, I need to close the server so that it stops listening on SERVICE_PORT. The problem is that server.close() is an asynchronous operation, and there is no way to tell Chrome to wait for the server to be closed before navigating away to the next page. Making this a single-page app with no browser navigation would get around this issue, however it would be ideal if I could stop the server (or by way of some NodeJS force close) before the page is navigated away and the JavaScript execution context is lost.
2. To counteract the problem in 1, I have some logic to retry listening on the port every 5 seconds should an error occur. When testing my JavaScript with the NodeJS command prompt, it works as expected if the port is already in use, i.e. it keeps printing "is trying to start", "couldn't start, retrying in 5 seconds", "is trying to start"... but when running this in Premiere, it just outright crashes Chrome and causes the extension to reload the initial page, meaning it only shows "is trying to start" before it reloads the initial page. This can happen several times as the port may still be in use from the previous run.
My main concern is number 2, as it seems to me that the NodeJS integrated into CEF is not letting the developer handle errors correctly.
Any thoughts or help would be much appreciated.

