Preventing server timeout in node.js

Update 2017-05-11. Updated node source code line link again, and links to documentation.

Update 2016-03-07. New link to node source code line.

Update 2015-10-09.  Not sure why people keep hitting this, but they do.  Apparently node.js docs don’t do a good job explaining that there is a two minute time limit hard coded in there?  I updated the link below to the latest master branch.

Update 2013-10-08. This is an old post but continues to get page views, so clearly it is still a problem. The feature is now documented (see link below) and this post is still correct.

Original post, 2011-03-30

This is something I spent an hour or so trying to track down today, so I thought I’d write it up in the hopes that someone else is spared the trouble.

First of all, I have both web client and server written in node.js. My server is designed such that it first checks for cached versions of URLs, and if the file doesn’t exist, then it hits the database and creates the file. This second step can take a long time, and so I wanted to write a utility script that I could trigger manually to update the cache of files whenever the database changes.

So I wrote the script using javascript and node, but was getting a strange error in which the client would die if the server took longer than two minutes to complete the request. No amount of abusing the code on the client would change this, even though the node.js source code seemed to indicate that no timeout was ever being set on the client socket, and most questions on the internet were about how to limit the timeout, not set it to forever.

Turns out the suspicious setTimeout( 2 * 60 * 1000 ) in http.js at line 986 was indeed the culprit. I originally ignored that line, as it was only setting the timeout for the server-side socket. But then, after editing that line in the code and recompiling (grasping at straws), re-running the client using the recompiled node and still getting exactly 2 minutes for the socket to die, it suddenly hit me that my server was timing out, not my client!

So with a single undocumented call inside of the handler in question, I had no more troubles:

res.writeHead(200, { 'Content-Type': 'application/json' });
res.connection.setTimeout(0); // this could take a while

Note the second line above. While the 0.4.4 API docs don’t state this fact, the http response object exposes the socket as the connection object (I found this on the mailing list in this thread So res.connection gives a hook to the Socket’s setTimeout function, and setting that to zero drops the default 2 minute limit.

November 2012 I’m still doing this in node.js 0.6.8+ 0.8.x, setTimeout is still part of net, and the http server is still by default using a 2 minute timeout. And github is still awesome.

August 2014 update. Yes, still there: 2 minute timeout. Really this isn’t a bug that needs fixing, because who wants a server to go away for two minutes in these days of attention deficit disorder web surfing. But I wish it was documented in the docs. Apparently this behavior will change soon:

And with the release of 0.10.x, but it is now documented. See server set timeout and response set timeout.

When I modify my own code to use 0.10.x, I will put up a new post.
No actually, apparently I never got around to putting up a new post on using the now documented timeout functions.

October 2015March 2016May 2017. Still there, but now you can find the offending hard-coded two minutes in the node source code here. The documentation links above now link to the version 7 API, but are still reasonably accurate for older (v6, etc) versions of node.

19 thoughts on “Preventing server timeout in node.js

  1. Was just trying to write a server to translate UDP packets sent from our application (nothing we can’t lose, and takes less time than a full TCP setup/teardown) to HTTP so we could push them out to a browser (server side push). This solved the issue I was having (connection timing out). Thanks so much!

  2. Thank you. You saved our of my times with this solution.
    Disappointed with the documentation of Node 0.8

  3. Thank you for this awesome post. I couldn’t figure out why the ajax call in the browser was failing.. at first I thought the browser had some sort of timeout, but after trying in both Chrome and IE it turned out they both timed out at exactly 2 minutes.
    I then used Wireshark to capture the traffic and was surprised to see that the connection termination actually came from the server, not the client! From there it was a relatively short (compared to the time spent on other directions) trip via google to this blog.

    So, thanks again :)

  4. Thanks! Large files uploaded through my node proxy server kept failing at around 800mb….turns out it was dying at the 2min mark. This is on node v4.3.0.

  5. Thank you so much for your post. Besides big file downloads, this issue can also typically happen for database connections (among other situations). I had an Ajax request to feed a large MongoDB database, which kept restarting every two minutes with (apparently) no reason… Problem fixed now!

  6. Same situation, I had a long call to cache records in a database. I kept running into the odd behavior of the client timing out, followed by (inexplicably) the server endpoint restarting itself (?!), which was giving me all sorts of duplicate key warnings in my cache tables. This fixed it.

  7. Thank you, thank you, thank you! I’ve been trying so long to find the solution to server timeouting after 2mins. You saved my day!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.