简体   繁体   English

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

[英]How to debug a socket hang up error in NodeJS?

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. 在节点v0.6.6中,我的代码有多个http.request和.get调用。 Please suggest ways to track what causes the socket hang up, and on which request/call it is. 请建议如何跟踪导致套接字挂断的原因以及请求/呼叫的方式。 Thank you 谢谢

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 . 清晰且正确的解决方案 :从技术上讲,在节点中,每当您发出'error'事件并且没有人收听它时,它就会抛出 To make it not throw, put a listener on it and handle it yourself. 为了使它不被抛出,请在其上放置一个监听器并自己处理它。 That way you can log the error with more information. 这样,您可以使用更多信息记录错误。

To have one listener for a group of calls you can use domains and also catch other errors on runtime. 要为一组调用创建一个侦听器,您可以使用 并在运行时捕获其他错误。 Make sure each async operation related to http(Server/Client) is in different domain context comparing to the other parts of the code, the domain will automatically listen to the error events and will propagate it to it's own handler. 确保与http(服务器/客户端)相关的每个异步操作与代码的其他部分相比处于不同的 上下文中,域将自动侦听 error事件并将其传播到它自己的处理程序。 So you only listen to that handler and get the error data. 所以你只听那个处理程序并获取错误数据。 You also get more information for free. 您还可以免费获得更多信息。 (Domains are depreceated). (域名被剥夺了)。

As Mike suggested you can also set NODE_DEBUG=net or use strace . 正如Mike建议你也可以设置NODE_DEBUG=net或使用strace They both provide you what is node doing internally. 它们都为您提供内部节点的功能。

Additionally, you can set the NODE_DEBUG environment variable to net to get information about what all the sockets are doing. 此外,您可以将NODE_DEBUG环境变量设置为net以获取有关所有套接字正在执行的操作的信息。 This way you can isolate which remote resource is resetting the connection. 这样,您可以隔离正在重置连接的远程资源。

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. 当我有这个“套接字挂断”错误时,这是​​因为我没有捕获请求错误。

使用

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

Most probably your server socket connection was somehow closed before all http.ServerResponse objects have ended. 在所有http.ServerResponse对象结束之前,很可能您的服务器套接字连接以某种方式关闭。 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请求不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM