1
votes

I am facing a strange issue with our node service.

Lots of socket file descriptors have remained open against node process and limit for open files (10240 files) against process has reached. So, I am getting EMFILE error.

Service has stalled and stopped accepting new requests as well as sending outbound requests to other services.

Nowhere in code I am EXPLICITLY dealing with socket connections.

Node process still listening on port. We are using PM2.

Similar question for Java: https://serverfault.com/questions/153983/sockets-found-by-lsof-but-not-by-netstat

Below is details of versions:

  • Node version: 8.16.0
  • Hapi: 14.2.0
  • request: 2.88.2 (used to send outbound requests)

Commands output in console:

[CONSOLE ~]$ lsof -p [PID] | wc -l
10253
[CONSOLE ~]$ ulimit -a
.
.
file size               (blocks, -f) unlimited
.
.
max memory size         (kbytes, -m) unlimited
open files                      (-n) 10240
.
.
.
[CONSOLE ~]$ netstat -np | grep [PORT]
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
[CONSOLE ~]$ netstat -a -n | grep  [PORT]
tcp        0      0 0.0.0.0:[PORT]           0.0.0.0:*               LISTEN     
[CONSOLE ~]$ lsof -i :[PORT]
COMMAND   PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
node    [PID] glpd   15u  IPv4 2270823542      0t0  TCP *:[PORT] (LISTEN)
[CONSOLE ~]$ lsof -p [PID]
COMMAND   PID USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
.
.
.
node    [PID] glpd   15u     IPv4         2270823542      0t0        TCP *:[PORT] (LISTEN)
node    [PID] glpd   16u     sock                0,7      0t0 2271652082 protocol: TCP
node    [PID] glpd   17u     sock                0,7      0t0 2271551621 protocol: TCP
node    [PID] glpd   18u     sock                0,7      0t0 2271663118 protocol: TCP
node    [PID] glpd   19u     sock                0,7      0t0 2271662963 protocol: TCP
node    [PID] glpd   20u     sock                0,7      0t0 2271660595 protocol: TCP
node    [PID] glpd   21u     sock                0,7      0t0 2271652144 protocol: TCP
node    [PID] glpd   22u     sock                0,7      0t0 2271660631 protocol: TCP
node    [PID] glpd   23u     sock                0,7      0t0 2271662997 protocol: TCP
node    [PID] glpd   24u     sock                0,7      0t0 2271660660 protocol: TCP
node    [PID] glpd   25u     sock                0,7      0t0 2271663083 protocol: TCP
.
.
.

Has anyone come across this in Node?

EDIT:
Socket timeout for all incoming requests to this service are disabled(set to false) as it is our main processing service and we can not predict the amount of time a request can take to get processed.

1
What does your process do? Are you 100% sure you don't have some incoming http requests that you never send a response to causing them to stay open for awhile? There's a >90% probability that this is something in your own code, perhaps a 9% probability that it's caused by some module you are using and <1% probability that this is a generic nodejs bug. FYI, if you're using the request() library, every time you call it, you're making a socket that should get cleaned up if everything works successfully, but could be left hanging around a while in some error conditions. - jfriend00
@jfriend00: I checked the same with "lsof" as well as "netstat" to identify if something is hanging in TCP states... nothing seems hanging... Output for those commands is added in question for reference. - Chaitanya
Are you leaking a file handle somewhere? - jfriend00
@jfriend00: all file descriptor types are "sock" which I am not dealing explicitly through my code. - Chaitanya
Re your edit, your stated rationale makes no sense whatsoever. How long you take to service the request once you have it all has exactly nothing to do with socket timeouts at your server, which are wholly and solely about reading the request. You should always have a read timeout in a server application, to protect yourself against rogue clients and clients who don't send entire requests in a timely manner. Otherwise you are going to accumuate socket leaks, and this is exactly what is happening. - user207421

1 Answers

0
votes

I had the same problem. In my case nodemailer did not close the connection after transport.close(). Updating to the latest nodemailer version solved the problem.