如何在 NodeJS 中调试套接字挂断错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10814481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:47:13  来源:igfitidea点击:

How to debug a socket hang up error in NodeJS?

node.jssocketshttpdebugging

提问by user971956

I am getting the following error:

我收到以下错误:

events.js:48
        throw arguments[1]; // Unhandled 'error' event
        ^
Error: socket hang up
    at createHangUpError (http.js:1091:15)
    at Socket.onend (http.js:1154:27)
    at TCP.onread (net.js:363:26)

In node v0.6.6, my code has multiple http.request and .get calls. Please suggest ways to track what causes the socket hang up, and on which request/call it is. Thank you

在节点 v0.6.6 中,我的代码有多个 http.request 和 .get 调用。请建议跟踪导致套接字挂断的原因以及它是哪个请求/调用的方法。谢谢

回答by Farid Nouri Neshat

Quick and dirty solution for development:

快速而肮脏的开发解决方案

Use longjohn, you get long stack traces that will contain the async operations.

使用 longjohn,您将获得包含异步操作的长堆栈跟踪。

Clean and correct solution: Technically, in node, whenever you emit an 'error'event and no one listens to it, it will throw. To make it not throw, put a listener on it and handle it yourself. That way you can log the error with more information.

干净且正确的解决方案:从技术上讲,在节点中,每当您发出一个'error'事件而没有人收听它时,它就会抛出. 为了让它不抛出,在它上面放一个监听器并自己处理。这样你就可以用更多的信息记录错误。

To have one listener for a group of calls you can use domainsand also catch other errors on runtime. Make sure each async operation related to http(Server/Client) is in different domaincontext comparing to the other parts of the code, the domain will automatically listen to the errorevents and will propagate it to it's own handler. So you only listen to that handler and get the error data. You also get more information for free.(Domains are depreceated).

要为一组调用设置一个侦听器,您可以使用并在运行时捕获其他错误。确保与 http(Server/Client) 相关的每个异步操作与代码的其他部分相比处于不同的上下文中,域将自动侦听error事件并将其传播到它自己的处理程序。所以你只听那个处理程序并获取错误数据。您还可以免费获得更多信息。(域已弃用)。

As Mikesuggested you can also set NODE_DEBUG=netor use strace. They both provide you what is node doing internally.

根据Mike建议,您还可以设置NODE_DEBUG=net或使用strace。它们都为您提供了节点在内部做什么。

回答by Mike

Additionally, you can set the NODE_DEBUGenvironment variable to netto get information about what all the sockets are doing. This way you can isolate which remote resource is resetting the connection.

此外,您可以将NODE_DEBUG环境变量设置为net以获取有关所有套接字正在执行的操作的信息。通过这种方式,您可以隔离哪个远程资源正在重置连接。

回答by fwoelffel

In addition to ftft1885's answer

除了ftft1885的回答

http.get(url, function(res)
{
    var bodyChunks = [];

    res.on('data', function(chunk)
    {
        // Store data chunks in an array
        bodyChunks.push(chunk);
    }).on('error', function(e)
    {
        // Call callback function with the error object which comes from the response
        callback(e, null);
    }).on('end', function()
    {
        // Call callback function with the concatenated chunks parsed as a JSON object (for example)
        callback(null, JSON.parse(Buffer.concat(bodyChunks)));
    });
}).on('error', function(e) {
    // Call callback function with the error object which comes from the request
    callback(e, null);
});

When I had this "socket hang up" error, it was because I wasn't catching the requests errors.

当我遇到这个“套接字挂断”错误时,是因为我没有捕捉到请求错误。

回答by ftft1885

use

req.on('error',function(err){})

回答by Mateusz Charytoniuk

Most probably your server socket connection was somehow closed before all http.ServerResponse objects have ended. Make sure that you have stopped all incoming requests before doing something with incoming connections (incomming connection is something different than incoming HTTP request).

很可能您的服务器套接字连接在所有 http.ServerResponse 对象结束之前以某种方式关闭。确保在对传入连接执行某些操作之前已停止所有传入请求(传入连接与传入 HTTP 请求不同)。