Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random 'ECONNABORTED' error when using sendFile in Express/Node

I have set a node server with Express middleware. I get the ECONNABORTED error randomly on some files when loading an HTML file which triggers about 10 other loads (js, css, etc.). The exact error is:

 { [Error: Request aborted] code: 'ECONNABORTED' }

Generated by this simplified code (after I tried to debug the issue):

 res.sendFile(res.locals.physicalUrl,function (err) {
   if (err)
      console.log(err);
   ...
  }

Many posts talk about this error resulting from not specifying the full path name. That is not the situation here. I do specify the full path and indeed the error is randomly generated. There are times when the page and all its subsequent links load perfectly and there are times when they do not. I tried to flush the cache and did not find any pattern to connect it with this.

This specific error appears to be a a generic term for socket connection getting aborted and is discussed in the context of other applications like FTP.

Having realized that the node worker threads can be increased, I tried to do so using:

process.env.UV_THREADPOOL_SIZE = 20;

However, my understanding is that even absent this, at most the file transfer may have to wait for a worker thread to be free and not get aborted. I am not talking about big files here, all files are less than 1 MB.

I have a gut feeling that this has nothing to do with node directly.

Please point to any other possibilities (node or otherwise) to handle this error. Also, any other indirect solutions? Retrying a few times could be one but that would be clumsy. EDIT: No, I cannot retry. Headers are already sent with the error!

A SIDE NOTE: Many examples on the use of sendFile skip using the callback thereby giving the impression that it is a synchronous call. It is not. Do use the callback at all times, check for success and only then move on to the "next" middleware or take appropriate steps if the send fails for whatever reason. Not doing so can make it difficult to debug the consequences in an asynchronous environment.

like image 746
Sunny Avatar asked Nov 16 '15 02:11

Sunny


1 Answers

See https://stackoverflow.com/a/36949631/2798152 Could it be possible that in some cases you terminate the connection by calling res.end before the asynchronous call to res.sendFile ends?

If that's not the case - can you pastebin more of your application code?

like image 120
Gabi Lee Avatar answered Nov 20 '22 14:11

Gabi Lee