7

I know there are a lot of these questions on SO but I cannot see anything that matches my specific situation.

I am running a .NET console application via Mono on Ubuntu. The app runs as a server and accepts connections via a TcpListener (TcpListener.AcceptTcpClient()). The problem I have is that after a while the program starts throwing 'Too many open files' exceptions.

I have increased the max file limit in Ubuntu in the two places that I am aware of:

  • /etc/security/limits.conf

root soft nofile 240000

root hard nofile 320000

(The process in question runs as root)

  • /etc/sysctl.conf

fs.file-max = 2000000

Both are set to ~200000.

If check the number of open file descriptions on the system it is only 996 even though it is throwing the errors.

I have the same program running on several Windows servers with many more connections and they never have this problem.

Any idea what could be causing this error?

2
  • Could you paste the relevant parts of the configuration files you changed? Looks like the limit is still at the default 1024 for the user that is running your application. Also note that the new limits are only effective after a new login as far as I am aware. Commented Jan 24, 2012 at 14:50
  • erm.. looks like something got lost. also, please provide all information by editing the question, not in the comments. thanks :) Commented Jan 24, 2012 at 15:39

4 Answers 4

7

The reason the error does not occur on Windows could be related to when garbage collection is done. If you do not explicitly close your socket connections, the sockets will be closed when they are collected.

This is somewhat speculative but if Mono does not start the garbage collection until it sees you running out of memory and it never runs out of memory sockets are never closed. Windows .NET framework might run garbage collecting regularly or when it sees you running out of file descriptors, which could account for the difference.

As a test you could force a manual garbage collection (GB.Collect) at regular intervals and if this fixes your errors, the proper solution would be to not rely on garbage collection for closing sockets, making sure to close them manually in your code. Forcing manual garbage collection brings its own set of problems.

3
  • What would garbage collection have to do with sockets? GC is about managed memory. Commented Oct 26, 2012 at 4:56
  • 1
    @AndrewBarber The GC calls the Finalize() method of each class that is garbarge collected. Classes with unmanaged resources usually implement the IDisposable pattern, adding a Dispose() method that cleans up the unmanaged handles. The finalizer in those classes usually call the Dipose() method. Thus, when the user never calls Dispose himself (which is very bad practice) and the GC never collects, then Dispose is never called and the unmanaged resources are never collected.
    – Drakarah
    Commented Jul 7, 2013 at 9:17
  • @drake7707 that's actually an excellent point, and makes me a little embarrassed to have posted that comment, now! Commented Jul 7, 2013 at 21:19
3

I first ran into a similar problem, but this is what I did to get it working:

sysctl -w net.inet.ip.portrange.first=3000  
sysctl -w net.inet.ip.portrange.hifirst=3000  
sysctl -w kern.maxfiles=900000  
sysctl -w kern.maxfilesperproc=900000  
ulimit -n 900000  

I ran the process under root instead of a normal user (different soft limit) and managed to create up to 100,000 connections. Note that ulimit is bound to a shell, so need to enter the mono (yourapp.exe) in the same shell as you do the ulimit, not from MonoDevelop.

For development running under OSX 10.7.5.

0
0

In linux TCP connections are assigned a file handle, which is probably why it is showing too many open file handles. Are you closing your TCP connections after you are done with them? A code sample exhibiting the problem would help.

1
  • As far as I understand the question, the application deliberately uses more than 1024 simultaneous conncections. Commented Jan 26, 2012 at 8:25
0

Have you tried setting ulimit as root?

On some distros the following may work:

Check ulimit:

Command: sudo ulimit -n
Response: 1024

Set ulimit:

Command: sudo ulimit -n 65500

Reboot

Check ulimit:

Command: sudo ulimit -n
Response: 65500

My own testing on "Ubuntu Precise" returned an error stating Command not found. The following however worked for me http://posidev.com/blog/2009/06/04/set-ulimit-parameters-on-ubuntu/ (Substitute "user" with the actual username when wildcard doesn't work for normal users)

1
  • Very important - add "session required pam_limits.so" to /etc/pam.d/common-session
    – Andre
    Commented Aug 3, 2012 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.