Skip to content

Commit

Permalink
fix: WaitOnSocket select error when sockfd above FD_SETSIZE (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
ztao authored May 21, 2022
1 parent 63803d1 commit 0ef9446
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# include <io.h>
# include <winsock2.h>
#else
# include <poll.h>
# include <unistd.h>
#endif
#include <curl/curl.h>
Expand Down Expand Up @@ -443,13 +444,19 @@ class HttpOperation
* @param sockfd
* @param for_recv
* @param timeout_ms
* @return
* @return true if expected events occur, false if timeout or error happen
*/
static int WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms)
static bool WaitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
bool res = false;

#if defined(_WIN32)

if (sockfd > FD_SETSIZE)
return false;

struct timeval tv;
fd_set infd, outfd, errfd;
int res;

tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
Expand All @@ -470,7 +477,47 @@ class HttpOperation
}

/* select() returns the number of signalled sockets or -1 */
res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
if (select((int)sockfd + 1, &infd, &outfd, &errfd, &tv) > 0)
{
if (for_recv)
{
res = (0 != FD_ISSET(sockfd, &infd));
}
else
{
res = (0 != FD_ISSET(sockfd, &outfd));
}
}

#else

struct pollfd fds[1];
::memset(fds, 0, sizeof(fds));

fds[0].fd = sockfd;
if (for_recv)
{
fds[0].events = POLLIN;
}
else
{
fds[0].events = POLLOUT;
}

if (poll(fds, 1, timeout_ms) > 0)
{
if (for_recv)
{
res = (0 != (fds[0].revents & POLLIN));
}
else
{
res = (0 != (fds[0].revents & POLLOUT));
}
}

#endif

return res;
}

Expand Down

2 comments on commit 0ef9446

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 0ef9446 Previous: 63803d1 Ratio
BM_BaselineBuffer/1 5012726.783752441 ns/iter 481516.6417187726 ns/iter 10.41

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 0ef9446 Previous: 63803d1 Ratio
BM_SpanIdConstructor 3.8515487214291055 ns/iter 1.7351641591149924 ns/iter 2.22

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.