Skip to content

Commit a807cb0

Browse files
committed
avoid runaway memory consumtion on unlimited open file limits
Usually ulimit -n is has sane values somewhere between 1000 and 10000. Some container managers like docker set higher values, so naemon might end up with ulimit of millions which just wastes memory. If a single worker hits the open file limit, simply start more worker.
1 parent 048dfa6 commit a807cb0

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/iobroker.c

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ struct iobroker_set *iobroker_create(void)
140140
}
141141

142142
iobs->max_fds = iobroker_max_usable_fds();
143+
/* add sane max limit, if ulimit is set to unlimited or a very high value we
144+
* don't want to waste memory for nothing */
145+
if (iobs->max_fds > MAX_FD_LIMIT)
146+
iobs->max_fds = MAX_FD_LIMIT;
143147
iobs->iobroker_fds = calloc(iobs->max_fds, sizeof(iobroker_fd *));
144148
if (!iobs->iobroker_fds) {
145149
goto error_out;

lib/lnae-utils.h

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
# define veclen ARRAY_SIZE
8989
#endif
9090

91+
/* sets a limit for max open files if ulimit is set to unlimited or a unusual high value */
92+
#ifndef MAX_FD_LIMIT
93+
#define MAX_FD_LIMIT 100000
94+
#endif
95+
9196
#ifndef offsetof
9297
/** standard offsetof macro */
9398
# define offsetof(t, f) ((unsigned long)&((t *)0)->f)

lib/runcmd.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static int maxfd = 0;
6969
# endif /* _SC_OPEN_MAX */
7070
#endif /* OPEN_MAX */
7171

72-
7372
const char *runcmd_strerror(int code)
7473
{
7574
switch (code) {
@@ -424,6 +423,11 @@ void runcmd_init(void)
424423
}
425424
#endif
426425

426+
/* add sane max limit, if ulimit is set to unlimited or a very high value we
427+
* don't want to waste memory for nothing */
428+
if (maxfd > MAX_FD_LIMIT)
429+
maxfd = MAX_FD_LIMIT;
430+
427431
/* reset pipe handling so child processes can use shell pipes */
428432
signal(SIGPIPE, SIG_DFL);
429433

0 commit comments

Comments
 (0)