Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to troubleshoot nodejs problems

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Nodejs problem is how to troubleshoot, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

I believe all of you have encountered the mistake of Error: read ECONNRESET. Although it is easy to find out what this error means through the ECONNRESET error code, a thorough analysis through the source code and analysis tools will give you a better understanding of how this error occurs and how it works. It's even more refreshing.

The following is divided into two parts, first analyze the cause of the error through the nodejs source code, and then capture the error by grabbing the package by the network tool.

1 source code analysis

We start with the analysis (net.js) of the actions performed by nodejs after a successful tcp connection is established.

This is the nodejs callback performed after a successful connection. A new socket object representing communication with the client is executed in the callback. Let's see what new Socket did.

The main logic of new Socket is

1 save the handle (socket) that communicates with the client

2 register read callback

3 register read event

Let's look at the third point first.

Socket is a readable and writable stream, read (0) directly calls the function of the readable stream, the readable stream provides abstract logic, and the specific reading operation is implemented by the subclass (implement the _ read function, and the readable read will call the _ read function). Let's take a look at the implementation of the Socket class _ read function.

Call the readStart function of handle directly. Because we are using tcp services here. So the corresponding implementation of handle is in tcp_wrap.cc. But we found that tcp_wrap.cc does not have a readStart function. All the way to the parent class, and finally found the function in stream_wrap.cc.

This function directly calls the uv_read_start function of libuv. The three input parameters are

1 uv_tcp_t structure

2 allocate memory to save read data

3 callbacks executed after reading (including read failure)

Keep going.

At this point, nodejs registers a readable event at the bottom, and when there is data or an error occurs, the upper callback will be triggered (although only readable events are registered, epoll will return POLLIN and POLLERR events if an error occurs). At this point, the client sends a rst. At this time, the callback uv__stream_io of libuv will be executed (instead of the one passed in by nodejs, the read_cb,read_cb is called back by libuv)

Then we take a look at uv_read.

Focusing on the read function, we might as well take a look at the code and take a look at the implementation of rst and read under linux.

The above is what happens when the operating system receives a rst packet. Set the error message of the corresponding socket to ECONNRESET, and set the status to close. What if the user executes read at this time?

The read function returns the error message directly to the caller. We go back to libuv and return the error code ECONNRESET when libuv calls the read function. Libuv then executes the read_cb callback of nodejs. If we remember, the callback provided by nodejs is OnUvRead.

Nodejs covers a lot of layers, but we still find him, and the final MakeCallback (env- > onread_string (), arraysize (argv), argv) is the onread function that executes the js layer. We also mentioned this function at the beginning. Back to net.js.

The onread function of nodejs executes the destroy function. Instead of expanding here, what destroy does is call the _ destroy function. Then emit an error event and pass in an Error object (containing information such as error codes and system call functions). When the error event is triggered, we output read ECONNRESET. At this point, the whole source code analysis process is over.

2 grab packet analysis

Log in to the server and use the tcpdump tool to filter out the desired packets. Find the ip that has the problem here. The filter condition is set to

Tcpdump-I any-Q-A-nn src ip1 or dst ip1 or src ip2 or dst ip2-w tcp.cap

Save as a cap file, and then download it to wireshark analysis (analysis under linux will be more troublesome). Finally, it is found that at the same point in time, both the grab package and the log system output related errors.

Insert a picture description here

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report