Skip to content

Commit 2fd4c2b

Browse files
author
Gurov Aleksei
committed
Fix freezing on select, if there is no active sockets
1 parent bc08d87 commit 2fd4c2b

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/http.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <sstream>
3535
#include <stdexcept>
3636
#include <string>
37+
#include <thread>
38+
#include <chrono>
3739
#include <type_traits>
3840

3941
#include "miniocpp/error.h"
@@ -431,18 +433,28 @@ Response Request::execute() {
431433
fd_set fdread{};
432434
fd_set fdwrite{};
433435
fd_set fdexcep{};
434-
int maxfd = 0;
436+
int maxfd = -1;
435437

436438
FD_ZERO(&fdread);
437439
FD_ZERO(&fdwrite);
438440
FD_ZERO(&fdexcep);
439441

440442
requests.fdset(&fdread, &fdwrite, &fdexcep, &maxfd);
441443

442-
if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, nullptr) < 0) {
443-
std::cerr << "select() failed; this should not happen" << std::endl;
444-
std::terminate();
444+
timeval timeout{};
445+
timeout.tv_sec = 0;
446+
timeout.tv_usec = 10000;
447+
448+
if (maxfd == -1) {
449+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
450+
} else {
451+
int ret = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
452+
if (ret < 0) {
453+
std::cerr << "select() failed; this should not happen" << std::endl;
454+
std::terminate();
455+
}
445456
}
457+
446458
while (!requests.perform(&left)) {
447459
}
448460
}

0 commit comments

Comments
 (0)