Too many open files problem

I have been using freebsd for few days. But everytime after few hours it is stuck with error
“too many open files” and I can’t even run sudo.
I tried to fix limits by known methods for FreeBSD but doesn’t help.
Here when it happened again:

nomad@NomadBSD ~> sysctl kern.maxfiles kern.maxfilesperproc kern.openfiles
kern.maxfiles: 10000000
kern.maxfilesperproc: 20000000
kern.openfiles: 34426

I have been using NomadBSD of course.
And how it looks like:

nomad@NomadBSD ~> sudo
Failed to execute process '/usr/local/bin/sudo'. Reason:
exec: Too many open files
nomad@NomadBSD ~> ulimit -a
Maximum size of core files created                           (kB, -c) unlimited
Maximum size of a process’s data segment                     (kB, -d) 33554432
Maximum size of files created by the shell                   (kB, -f) unlimited
Maximum size that may be locked into memory                  (kB, -l) 64
Maximum resident set size                                    (kB, -m) unlimited
Maximum number of open file descriptors                          (-n) 20000000
Maximum stack size                                           (kB, -s) 524288
Maximum amount of cpu time in seconds                   (seconds, -t) unlimited
Maximum number of processes available to a single user           (-u) 34218
Maximum amount of virtual memory available to the shell      (kB, -v) unlimited

I made this bigger too but no help… I open browser and 1 application and system dies with “too many open files”

That’s odd. I would say, increase kern.maxfiles, but it is already insanely high, and kern.openfiles is only at ~ .3%. Could you please run the following script for a while (until you get that error again), and post the result here?

#!/usr/local/bin/perl

foreach (`fstat`) {
	next if ($lc++ == 0);
	$app = $2 if (/(\S+)\s+(\S+).*/);
	$prev_counts{$app}++;
}

while (1) {
	$lc = 0;
	system("clear");
	foreach (`fstat`) {
		next if ($lc++ == 0);
		$app = $2 if (/(\S+)\s+(\S+).*/);
		$counts{$app}++;
	}
	printf("%-25s\tOPEN\tINCREASE\n", "PROCESS");
	foreach $k (sort (keys %counts)) {
		if ($prev_counts{$k} > 0) {
			$incr{$k} += $counts{$k} - $prev_counts{$k};
		}
		printf("%-25s\t%d\t%d\n", $k, $counts{$k}, $incr{$k});
	}
	%prev_counts = %counts;
	%counts = ();
	sleep(5);
}

% perl openmon.pl

1 Like

normally I just doing something in browser after time it happens.
With script running. I tried to open many applications. It didn’t happen then I watched youtube for a while and it happened. Here is the output at that time:

PROCESS OPEN INCREASE
at-spi-bus-launcher 18 0
at-spi2-registryd 15 0
audacious 18 0
bamfdaemon 29 2
blender 21 14
chrome 2261 1653
ck-launch-session 8 0
compton 8 0
dbus-daemon 72 14
dconf-service 14 0
dsbmc 26 0
dsbmixer 36 0
dunst 15 0
fish 20 10
fstat 8 0
gdbus 13 0
gimp-2.10 20 5
gvfs-gphoto2-volume 14 0
gvfs-hal-volume-mon 14 0
gvfsd 16 0
gvfsd-fuse 17 0
gvfsd-http 16 -3
gvfsd-metadata 18 4
gvfsd-trash 22 0
hexchat 21 12
octopkg 25 -6
openbox 16 0
pcmanfm 33 -3
perl 16 0
plank 43 0
redshift 8 0
remmina 21 -1
sakura 45 24
script-fu 15 0
sh 14 7
thunar 40 7
tint2 10 0
xbindkeys 7 0
xfconfd 16 0

What does sysctl kern.maxusers say?

nomad@NomadBSD ~> sysctl kern.maxusers
kern.maxusers: 2375

Wow. I guess you got a lot of RAM, right?

What about sysctl kern.maxprocperuid?

Yeah right now I have 32Gb, it is home PC. I don’t plan to use only here though.
I need it for my work I often have to change PCs, laptops… sometime very old PC.

nomad@NomadBSD ~> sysctl kern.maxprocperuid
kern.maxprocperuid: 340000

Try to change unionfsmax_files value from 32768 to 65536 in /etc/rc.d/mount_uzip. It seems, unionfs is responsible. The number of opened files does not decrease albeit programs are closed. I need to investigate further.

I did. Normally It was dead arounf 33000 open files. And I usually get to that point in 2 hours.
Now I have been working all day and I am here:

nomad@NomadBSD ~> sysctl kern.openfiles
kern.openfiles: 44984

But no errors so far. Lets see what happens when it reaches 65k open files.

So I worked all day and it happened only in the evening…
at least I can work now.

nomad@NomadBSD ~> sysctl kern.openfiles
kern.openfiles: 68641
nomad@NomadBSD ~> sudo
Failed to execute process '/usr/local/bin/sudo'. Reason:
exec: Too many open files

this should be enough.
Because to get to that limit I used 3 chrome with different users, with 20 tabs on each.
Modeling in blender. at the same time compiling stm32 in keil that run in wine.
and many more heavy apps I could possibly find.
and it worked from 6am to midnight.
in real work I hardly get to that kind of usage …