Skip to content
Snippets Groups Projects
Commit 7e9a872f authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy
Browse files

sio: use kern.ipc.somaxconn for listen() on Mac

listen() on Mac used to take SOMAXCONN as the backlog size. It is
just 128, which is too small when connections are incoming too
fast. They get rejected.

Increase of the queue size wasn't possible, because the limit was
hardcoded. But now sio takes the runtime limit from
kern.ipc.somaxconn sysctl setting.

One weird thing is that when set too high, it seems to have no
effect, like if nothing was changed. Specifically, values above
32767 are not doing anything, even though stay visible in
kern.ipc.somaxconn.

It seems listen() on Mac internally might be using 'short' or
int16_t to store the queue size and it gets broken when anything
above INT16_MAX is used. The code truncates the queue size to this
value if the given one is too high.

Closes #8130

NO_DOC=bugfix
NO_TEST=requires root privileges for testing
parent d2240bf7
No related branches found
No related tags found
No related merge requests found
## bugfix/core
* Fixed the bug that on Mac the system setting `kern.ipc.somaxconn` was ignored
for listening sockets. Now it is used, but capped at 32367 due to how
`listen()` works on Mac (gh-8130).
......@@ -43,6 +43,10 @@
#include "uri/uri.h"
#include "errinj.h"
#if TARGET_OS_DARWIN
#include <sys/sysctl.h>
#endif
static_assert(SMALL_STATIC_SIZE > NI_MAXHOST + NI_MAXSERV,
"static buffer should fit host name");
......@@ -137,7 +141,31 @@ sio_listen_backlog()
if (rc == 1)
return backlog;
}
#endif /* TARGET_OS_LINUX */
#elif TARGET_OS_DARWIN
int somaxconn = 0;
size_t size = sizeof(somaxconn);
const char *name = "kern.ipc.somaxconn";
int rc = sysctlbyname(name, &somaxconn, &size, NULL, 0);
if (rc == 0) {
/*
* From tests it appears that values > INT16_MAX
* work strangely. For example, 32768 behaves
* worse than 32767. Like if nothing was changed.
* The suspicion is that listen() on Mac
* internally uses int16_t or 'short' for storing
* the queue size and it simply gets reset to
* default on bigger values.
*/
if (somaxconn > INT16_MAX) {
say_warn("%s is too high (%d), "
"truncated to %d", name,
somaxconn, (int)INT16_MAX);
somaxconn = INT16_MAX;
}
return somaxconn;
}
say_syserror("couldn't get system's %s setting", name);
#endif
return SOMAXCONN;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment