Skip to content

Don't apply the FD_SETSIZE check to Windows sockets#866

Open
StalderT wants to merge 1 commit into
stephane:masterfrom
StalderT:fix-fdsetsize-windows
Open

Don't apply the FD_SETSIZE check to Windows sockets#866
StalderT wants to merge 1 commit into
stephane:masterfrom
StalderT:fix-fdsetsize-windows

Conversation

@StalderT

Copy link
Copy Markdown
Contributor

On Windows a SOCKET is an opaque handle, not a small index, and an fd_set is a counted array of sockets rather than a bitmask. libmodbus only ever puts a single socket in a set, so it can't overflow it.

The fd >= FD_SETSIZE checks added in 7b13341 therefore don't apply on Windows, where they instead reject any socket whose handle value reaches FD_SETSIZE (64 by default) - which happens after a few dozen open handles and breaks Modbus TCP with EINVAL / "exceeds FD_SETSIZE".

Wrap the check in a macro that keeps it on POSIX and disables it on Windows. POSIX behaviour is unchanged.

Fixes #842

@cla-bot

cla-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: TStal.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

On Windows a SOCKET is an opaque handle, not a small index, and an fd_set
is a counted array of sockets rather than a bitmask. libmodbus only ever
puts a single socket in a set, so it can't overflow it.

The fd >= FD_SETSIZE checks added in 7b13341 therefore don't apply on
Windows, where they instead reject any socket whose handle value reaches
FD_SETSIZE (64 by default) - which happens after a few dozen open handles
and breaks Modbus TCP with EINVAL / "exceeds FD_SETSIZE".

Wrap the check in a macro that keeps it on POSIX and disables it on
Windows. POSIX behaviour is unchanged.

Fixes stephane#842
@StalderT
StalderT force-pushed the fix-fdsetsize-windows branch from fde8276 to 24f6be6 Compare July 23, 2026 16:31
@cla-bot

cla-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libmodbus 3.1.12 does not connect in windows

1 participant