Is it FD_SETSIZE hardcoded to 64?

liyafe1997
Posts: 3
Joined: Mon Oct 28, 2024 1:35 pm

Is it FD_SETSIZE hardcoded to 64?

Postby liyafe1997 » Mon May 19, 2025 1:29 pm

I want to have socket connection more than 64.
If I set CONFIG_LWIP_MAX_SOCKETS > 64 in the sdkconfig, I got

Code: Select all

v5.4.1/esp-idf/components/lwip/port/esp32xx/vfs_lwip.c:23:1: error: static assertion failed: "MAX_FDS < CONFIG_LWIP_MAX_SOCKETS"
   23 | _Static_assert(MAX_FDS >= CONFIG_LWIP_MAX_SOCKETS, "MAX_FDS < CONFIG_LWIP_MAX_SOCKETS");
      | ^~~~~~~~~~~~~~
I see in the [.espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/xtensa-esp-elf/include/sys/select.h]

Code: Select all

#ifndef FD_SETSIZE
# ifdef __CYGWIN__
#  define FD_SETSIZE	1024
# else
#  define FD_SETSIZE	64
# endif
#endif
Seems it is hardcoded to 64?

But in [v5.4.1/esp-idf/components/lwip/lwip/src/include/lwip/sockets.h]

Code: Select all

/* FD_SET used for lwip_select */
#ifndef FD_SET
#undef  FD_SETSIZE
/* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
#define FD_SETSIZE    MEMP_NUM_NETCONN
#define LWIP_SELECT_MAXNFDS (FD_SETSIZE + LWIP_SOCKET_OFFSET)
And in [v5.4.1/esp-idf/components/lwip/port/include/lwipopts.h]

Code: Select all

/**
 * MEMP_NUM_NETCONN: the number of struct netconns.
 * (only needed if you use the sequential API, like api_lib.c)
 */
#define MEMP_NUM_NETCONN                CONFIG_LWIP_MAX_SOCKETS
Seems the FD_SETSIZE should be set from CONFIG_LWIP_MAX_SOCKETS?

But anyway, how can I increase FD_SETSIZE / MAX_FDS so make it possible to have fd number or opened socket more than 64?

Crororo
Posts: 1
Joined: Sun Jul 20, 2025 7:19 pm

Re: Is it FD_SETSIZE hardcoded to 64?

Postby Crororo » Sun Jul 20, 2025 7:22 pm

I encountered the same error when increasing CONFIG_LWIP_MAX_SOCKETS:

Code: Select all

_Static_assert(MAX_FDS >= CONFIG_LWIP_MAX_SOCKETS, "MAX_FDS < CONFIG_LWIP_MAX_SOCKETS");
The issue is that in ESP-IDF (and PlatformIO), MAX_FDS is not defined directly via CONFIG_MAX_FDS, but instead through FD_SETSIZE:

Code: Select all

#define MAX_FDS FD_SETSIZE
By default, FD_SETSIZE is 64. So if you raise CONFIG_LWIP_MAX_SOCKETS to 100 or more, the static assertion fails because MAX_FDS is still 64.

Trying to define MAX_FDS directly via build_flags doesn't work, because it's already defined in esp_vfs.h, resulting in a redefinition error:

Code: Select all

warning: "MAX_FDS" redefined
The fix is to define FD_SETSIZE directly, so that MAX_FDS becomes larger:

In platformio.ini:

Code: Select all

build_flags =
  -DFD_SETSIZE=128
  -DCONFIG_LWIP_MAX_SOCKETS=100

Who is online

Users browsing this forum: Bytespider, Qwantbot and 6 guests