Reputation: 2458
I am running into an issue where FD_SET
is crashing my program. I am running the example code from a boost Boost ASIO sample HTTP Server. In the request handler, I put a third party api the eventually makes the following calls:
fd_set fds;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
My program seems to be crashing on the FD_SET
. Is there a situation where calling FD_SET
from different threads would cause a segfault? I am not sure exactly how FD_SET
works, my gut feeling would be that I am calling FD_SET
on a fd that is already in use. Would this cause a problem at all?
sockfd
is a class member, which is created each time a request comes in, so I should not be sharing a sockfd
variable between threads.
Upvotes: 1
Views: 4303
Reputation: 24174
At 1000 requests/second you are very likely eclipsing the FD_SETSIZE
limit (typically 1024 on Linux), which results in undefined behavior:
An fd_set is a fixed size buffer. Executing FD_CLR() or FD_SET() with a value of fd that is negative or is equal to or larger than FD_SETSIZE will result in undefined behavior. Moreover, POSIX requires fd to be a valid file descriptor.
You should look at using poll
, epoll
, or a higher level library like Boost Asio. Any of those choices will not have the limitation of select
.
Upvotes: 4