Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Possible Solution] Crash when a Chromium Environment Error occurs on Node Environment #1468

Closed
danyg opened this issue Jan 12, 2014 · 7 comments

Comments

@danyg
Copy link

danyg commented Jan 12, 2014

Hi, I'm creating a proyect that uses NeDB, when I do something like that

    db.find({}, function(){
        // some code in webkit/chromium environment that is executed from node environment
    });

And the code on webkit explode because some type error o whatever, the application crash.

I found this solution for that

    // clean exceptions from a previous load (refresh case)
    if(process._events.uncaughtException.length > 0){
        process._events.uncaughtException.splice(0,1);
    }
    process.on('uncaughtException', function(e){
        console.group('Node uncaughtException');
        if(!!e.message){
            console.log(e.message);
        }
        if(!!e.stack){
            console.log(e.stack);
        }
        console.log(e);
        console.groupEnd();
    });
    // Clean Buggy thing
    if(process._events.uncaughtException.length > 1 
        && !!process._events.uncaughtException[0].toString().match(/native code/)
    ){
        process._events.uncaughtException.splice(0,1);
    }

Apparently the default uncaughtException listener ([native code] says) is not able to handle this type of errors.
To solve this, I remove it doing process._events.uncaughtException.splice(0,1);

Tested on Windows 7 x64 NodeWebKit 0.8.3

Something interesting is, in this case when the app crash, the app crash report (.dmp file) is not generated.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@olecom
Copy link

olecom commented Jan 15, 2014

Something interesting is, in this case when the app crash, the app crash report (.dmp file) is not generated.

AFAIK *.dmp is generated after node-webkit process nw.exe itself crashes, not HTML/JS app inside it.

For all possible errors i'm using both node's process.on('uncaughtException') and HTML's JS window.onerror handlers.

Sometimes one catches something (syntax, page bugs, simple JS bugs), sometimes other (errors in nodejs objects, if 'error' event is not handled by callbacks).

HTML:

        window.addEventListener('error' ,function(errEvent){
            var m
            console && console.error(errEvent)
            m = 'uncaughtException: '  +
                errEvent.message + '\nfilename:"' +
                (errEvent.filename ? errEvent.filename : 'app_front.js') +
                '", line:' + errEvent.lineno
            // show any errors early
            document.write('<pre><h2>' +
                m + '</h2><div style="color:white;background-color:red">' +
                errEvent.error.stack + '</div></pre>'
            )
            alert(m)
        })

nodeJS part:

    process.on('uncaughtException' ,function(err){
        console.error('uncaughtException:', err)
        console.error(err.stack)
        alert(l10n.uncaughtException  + err)
    })

have a look here
olecom/enjsms@1a9efc6#commitcomment-5018613

@danyg
Copy link
Author

danyg commented Feb 1, 2014

@olecom The app crash, is closed by it self, and there is not any clue why. (of course the reason is something wrong in my JS code, and for that I want to see what happen there). This problem occurs when you use nodejs modules, and this modules uses callbacks, you write a callback code in the webkit context, and that callback is executed in node context, when an error occurs there the only one that can catch that error is node, and is nothing is handling the errors on node the app crash without message or dump for that.

Apparently the bug appear when you add a Listener to uncaughtException. Normally nothing is listening that, until the developer add a custom listener. I redactor the code to eliminate this NATIVE CODE that is automatically added.

    // clean exceptions from a previous load (refresh case)
    if(process._events.uncaughtException.length > 0){
        process._events.uncaughtException.splice(0,1);
    }
    process.on('uncaughtException', function(e){
        console.group('Node uncaughtException');
        if(!!e.message){
            console.log(e.message);
        }
        if(!!e.stack){
            console.log(e.stack);
        }
        console.log(e);
        console.groupEnd();
    });
    // Clean Buggy thing
    if(process._events.uncaughtException.length > 1 
        && !!process._events.uncaughtException[0].toString().match(/native code/)
    ){
        process._events.uncaughtException.splice(0,1);
    }

This new code is tested on Win7 x64 with nodewebkit 0.8.4

@katanacrimson
Copy link

Do not log and continue when using process.on 'uncaughtException'. You're leaving the stack in an unclean state and must terminate.

http://nodejs.org/api/process.html#process_event_uncaughtexception

Note that uncaughtException is a very crude mechanism for exception handling and may be removed in the future.

Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!

Do not use it as the node.js equivalent of On Error Resume Next. An unhandled exception means your application - and by extension node.js itself - is in an undefined state. Blindly resuming means anything could happen.

Think of resuming as pulling the power cord when you are upgrading your system. Nine out of ten times nothing happens - but the 10th time, your system is bust.

You have been warned.

@katanacrimson
Copy link

Further, nedb has an err parameter in most callbacks to indicate if an error was thrown. Consult its documentation.

@danyg
Copy link
Author

danyg commented Feb 10, 2014

@damianb, domains are in a Unstable status. I don't think that can be a solution, a least for the moment.

I don't be able to read the error of the nedb callback because the application explode before.
I think that nobody understood what I'm trying to explain.

// this is executed on webkit environment, directly for a tag script.
var someNODEJSmodule = require('someNODEJSmodule');

someNODEJSmodule.method(function(){
    window.inexistentFunction();
});

file in node_modules/someNODEJSmodule.js

module.exports = {
    method: function(cbk){
        cbk();
    }
}

this is the scenario, that can throw a typeError exception, because the inexistentFunction do not exists. BUT the problem is, that error is throwed on NODE JS ENVIRONMENT, and there is not a handler/catcher/logger/whatever by default in nodewebkit to get that error and show in console or stdout o something. and because of that, the application crash, and close by itself.

Is nothing with nedb itself. In order to prevent the crash, you need to define a handler for the uncaughtException in the NODE ENVIRONMENT. The problem with this, is when you do that, something in the nodewebkit code add another handler to that event (uncaughtException) that is BROKEN! and do that the app crash in the same way.

@katanacrimson
Copy link

@danyg "unstable" regarding domains refers to the API's structure (method naming, method availability, etc.). read the documentation on nodejs.org itself again.

@nwjs-bot
Copy link

This should be working with latest version now.

In 0.13 we changed to an optimized architecture so more features can be supported, see http://nwjs.io/blog/whats-new-in-0.13/ and it's good for keeping up with Chromium upstream -- we released with Node.js v6.0 and new Chromium versions within 1 day after upstream release.

The new version would fixed many issues reported here and we're scrubbing them. This issue is closed as we believe it should be fixed. Please leave a message if it isn't and we'll reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants