http server -- too many open files (even with ulimit increase)

1,023 views
Skip to first unread message

John Lee

unread,
May 10, 2014, 10:39:24 PM5/10/14
to golan...@googlegroups.com
I have a node.js server that makes around 100-500 requests/second to my Go server. I set my ulimit to over 300,000. 

Sometimes, when traffic is high I get the "too many open files" error. 

From what I observed, the file descriptors increase with each request. They seem to decrease significantly once in a while (I suspect it decreases when the GC kicks in).

This seems to be caused by the http server. I'm not sure why the file descriptors keep increasing and are not being cleared. The Go server responds to the node.js server fairly quickly. 

Brad Fitzpatrick

unread,
May 10, 2014, 11:01:19 PM5/10/14
to John Lee, golang-nuts
Run lsof -p <pid> on your server and see what's leaking. It's likely you're forgetting to call Close() on something and that's why you see a decrease at GC time, when finalizers clean stuff up for you.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Lee

unread,
May 10, 2014, 11:06:14 PM5/10/14
to golan...@googlegroups.com, John Lee
It's all tcp connections from the nodejs server. 
I checked, and everything seems closed. 

John Lee

unread,
May 10, 2014, 11:27:02 PM5/10/14
to golan...@googlegroups.com, John Lee
a bit of code:

func handleRequest(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")

body, err := ioutil.ReadAll(req.Body)

        // ... bunch of code

        res.Write(nil)  // <-- I'm using this to send an empty response. Would it cause a problem?
}

my other theory is that I need to use Keep-Alive to re-use the same connection, but it doesn't explain why the other connections aren't closing. 

On Saturday, May 10, 2014 11:01:19 PM UTC-4, bradfitz wrote:

mikespook

unread,
May 11, 2014, 12:37:55 AM5/11/14
to John Lee, golang-nuts

Have you tried: defer body.Close()?

Jesse McNelis

unread,
May 11, 2014, 12:52:34 AM5/11/14
to golang-nuts, John Lee


On 11/05/2014 2:38 pm, "mikespook" <mike...@gmail.com> wrote:
>
> Have you tried: defer body.Close()?
>

The body is already closed when the request handler returns.

Perhaps the node.js server is keeping the connections open?
What makes you think they are closed?

egon

unread,
May 11, 2014, 1:16:12 AM5/11/14
to golan...@googlegroups.com, John Lee, jes...@jessta.id.au
Post code that reproduces the problem, it's hard to guess the exact problem without knowing the code. Alternatively, if you want to figure out on your own then try to make a minimal example that reproduces the problem... see what can be closed and start adding explicit Close-s to things; or try removing/adding stuff that could cause the problem.

+ egon

John Lee

unread,
May 11, 2014, 1:47:07 AM5/11/14
to golan...@googlegroups.com, John Lee, jes...@jessta.id.au
Thanks. I found the issue.  The node server wasn't closing the request because the response object was missing the 'data' handler.
Reply all
Reply to author
Forward
0 new messages