Skip to content

Commit ada9e70

Browse files
authored
fix: use poll() for connect timeout on UNIX to avoid FD_SETSIZE limit (#800)
1 parent 99d5a6d commit ada9e70

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

base/hsocket.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include "hdef.h"
44

5+
#ifdef OS_UNIX
6+
#include <poll.h>
7+
#endif
8+
59
#ifdef OS_WIN
610
#include "hatomic.h"
711
static hatomic_flag_t s_wsa_initialized = HATOMIC_FLAG_INIT;
@@ -260,6 +264,20 @@ static int ListenFD(int sockfd) {
260264
static int ConnectFDTimeout(int connfd, int ms) {
261265
int err = 0;
262266
socklen_t optlen = sizeof(err);
267+
268+
#ifdef OS_UNIX
269+
// Use poll() to avoid FD_SETSIZE limit (fd >= 1024 crashes with select)
270+
struct pollfd pfd;
271+
pfd.fd = connfd;
272+
pfd.events = POLLOUT;
273+
pfd.revents = 0;
274+
int ret = poll(&pfd, 1, ms);
275+
if (ret < 0) {
276+
perror("poll");
277+
goto error;
278+
}
279+
#else
280+
// Windows: select() is safe (fd_set uses different implementation)
263281
struct timeval tv = { ms / 1000, (ms % 1000) * 1000 };
264282
fd_set writefds;
265283
FD_ZERO(&writefds);
@@ -269,6 +287,7 @@ static int ConnectFDTimeout(int connfd, int ms) {
269287
perror("select");
270288
goto error;
271289
}
290+
#endif
272291
if (ret == 0) {
273292
errno = ETIMEDOUT;
274293
goto error;

0 commit comments

Comments
 (0)