从Node.JS中的未捕获异常中恢复

时间:2022-09-12 20:28:29

OK, so I have a problem. If an uncaught exception occurs while I am handling an HTTP request, I have no opportunity to call the end() method on the http.ServerResponse object. Therefore, the server hangs forever and never fulfills the request.

好的,所以我有一个问题。如果在处理HTTP请求时发生未捕获的异常,我就没有机会在http.ServerResponse对象上调用end()方法。因此,服务器永远挂起,永远不会满足请求。

Here's an example:

这是一个例子:

var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
    console.log("Request #", ++reqNum, ":", req.url);
    next();
});
app.get('/error', function(req, res, next) {
    throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
    console.log("In /hang route");
    setTimeout(function() {
        console.log("In /hang callback");
        if(reqNum >= 3)
            throw new Error("Problem occurred");
        res.send("It worked!");
    }, 2000);
});
process.on('uncaughtException', function(err) {
    console.log("Uncaught exception!", err);
});
app.listen(8080);

If you visit /error, an exception occurs, but it is caught. The user receives an error message - no problem. If I visit /hang, though, the server will eventually throw an uncaught exception and hang forever. Any subsequent requests for /hang will hang.

如果您访问/错误,则会发生异常,但会被捕获。用户收到错误消息 - 没问题。但是,如果我访问/挂起,服务器最终将抛出未捕获的异常并永久挂起。任何后续的/ hang请求都将挂起。

This sucks. Any advice for how to fix this issue?

这很糟糕。有关如何解决此问题的任何建议?

2 个解决方案

#1


16  

When an uncaught exception occurs, you're in an unclean state. Let the process die and restart it, there's nothing else you can do to safely bring it back to a known-good state. Use forever, it'll restart your process as soon as it dies.

当未捕获的异常发生时,您处于不洁净状态。让进程死掉并重新启动它,没有别的办法可以让它安全地恢复到已知状态。永远使用,它会在它死后立即重启你的过程。

#2


1  

Use try/catch/finally.

使用try / catch / finally。

app.get('/hang', function(req, res, next) {
    console.log("In /hang route");
    setTimeout(function() {
        console.log("In /hang callback");
        try {
            if(reqNum >= 3)
                throw new Error("Problem occurred");
        } catch (err) {
            console.log("There was an error", err);
        } finally {
            res.send("It worked!");
        }
    }, 2000);
});

#1


16  

When an uncaught exception occurs, you're in an unclean state. Let the process die and restart it, there's nothing else you can do to safely bring it back to a known-good state. Use forever, it'll restart your process as soon as it dies.

当未捕获的异常发生时,您处于不洁净状态。让进程死掉并重新启动它,没有别的办法可以让它安全地恢复到已知状态。永远使用,它会在它死后立即重启你的过程。

#2


1  

Use try/catch/finally.

使用try / catch / finally。

app.get('/hang', function(req, res, next) {
    console.log("In /hang route");
    setTimeout(function() {
        console.log("In /hang callback");
        try {
            if(reqNum >= 3)
                throw new Error("Problem occurred");
        } catch (err) {
            console.log("There was an error", err);
        } finally {
            res.send("It worked!");
        }
    }, 2000);
});