Anyway to stop server restarts, if plugin throws an error?

Technical Support
  • Nodebb 1.12.1
    MongoDB 3.4

    There is something in our database that makes https://github.com/NicolasSiver/nodebb-plugin-ns-points and https://github.com/NicolasSiver/nodebb-plugin-ns-awards crash when certain calculation is made. Which leads to crashing whole nodebb server.

    Here is the log of area where it crashes

    2019-04-16T22:09:48.190Z [4567/24494] - error: uncaughtException: Cannot read property 'uid' of null
    TypeError: Cannot read property 'uid' of null
        at /var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:67:37
        at /var/www/community/node_modules/async/dist/async.js:1135:9
        at eachOfArrayLike (/var/www/community/node_modules/async/dist/async.js:1069:9)
        at eachOf (/var/www/community/node_modules/async/dist/async.js:1117:5)
        at _asyncMap (/var/www/community/node_modules/async/dist/async.js:1133:5)
        at Object.map (/var/www/community/node_modules/async/dist/async.js:1122:16)
        at Object.Filter.postGetPosts [as method] (/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:66:15)
        at /var/www/community/src/plugins/hooks.js:136:12
        at /var/www/community/node_modules/async/dist/async.js:2516:9
        at replenish (/var/www/community/node_modules/async/dist/async.js:1011:17) {"error":{},"stack":"TypeError: Cannot read property 'uid' of null\n    at /var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:67:37\n    at /var/www/community/node_modules/async/dist/async.js:1135:9\n    at eachOfArrayLike (/var/www/community/node_modules/async/dist/async.js:1069:9)\n    at eachOf (/var/www/community/node_modules/async/dist/async.js:1117:5)\n    at _asyncMap (/var/www/community/node_modules/async/dist/async.js:1133:5)\n    at Object.map (/var/www/community/node_modules/async/dist/async.js:1122:16)\n    at Object.Filter.postGetPosts [as method] (/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:66:15)\n    at /var/www/community/src/plugins/hooks.js:136:12\n    at /var/www/community/node_modules/async/dist/async.js:2516:9\n    at replenish (/var/www/community/node_modules/async/dist/async.js:1011:17)","exception":true,"date":"Tue Apr 16 2019 22:09:48 GMT+0000 (UTC)","process":{"pid":24494,"uid":0,"gid":0,"cwd":"/var/www/community","execPath":"/root/.nvm/versions/node/v8.11.4/bin/node","version":"v8.11.4","argv":["/root/.nvm/versions/node/v8.11.4/bin/node","/var/www/community/app.js"],"memoryUsage":{"rss":438337536,"heapTotal":304680960,"heapUsed":255611952,"external":54099262}},"os":{"loadavg":[1.5625,1.14306640625,1.07568359375],"uptime":7181824},"trace":[{"column":37,"file":"/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js","function":null,"line":67,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":null,"line":1135,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":"eachOfArrayLike","line":1069,"method":null,"native":false},{"column":5,"file":"/var/www/community/node_modules/async/dist/async.js","function":"eachOf","line":1117,"method":null,"native":false},{"column":5,"file":"/var/www/community/node_modules/async/dist/async.js","function":"_asyncMap","line":1133,"method":null,"native":false},{"column":16,"file":"/var/www/community/node_modules/async/dist/async.js","function":"Object.map","line":1122,"method":"map","native":false},{"column":15,"file":"/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js","function":"Object.Filter.postGetPosts [as method]","line":66,"method":"Filter.postGetPosts [as method]","native":false},{"column":12,"file":"/var/www/community/src/plugins/hooks.js","function":null,"line":136,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":null,"line":2516,"method":null,"native":false},{"column":17,"file":"/var/www/community/node_modules/async/dist/async.js","function":"replenish","line":1011,"method":null,"native":false}]}
    2019-04-16T22:09:48.193Z [4567/24494] - error: Cannot read property 'uid' of null
    2019-04-16T22:09:48.193Z [4567/24494] - info: [app] Shutdown (SIGTERM/SIGINT) Initialised.
    

    Within /nodebb-plugin-ns-points/plugin/filters.js:67

    Filter.postGetPosts = function (payload, callback) {
            async.map(payload.posts, function (post, next) {
                database.getPoints(post.uid, function (error, points) {
                    if (error) {
                        return next(error);
                    }
                    post.points = points || 0;
                    next(null, post);
                });
            }, function (error, results) {
                if (error) {
                    return callback(error);
                }
                payload.posts = results;
                callback(null, payload);
            });
        };
    

    So some how the post object is null. Is there any way we can catch this error and fail gracefully and not crash the whole server? Or alternatively any way we can run a query to find this dataset to fix the issue which causes the crash?

    Thanks

  • I don't think it's possible to recover from an unhandledException, the plugin needs to check the post object to make sure it's not undefined. Or needs to wrap that part of the code in a try/catch.


Suggested Topics