Skip to content

Commit 4bb5348

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

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/http.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,20 +431,30 @@ Response Request::execute() {
431431
fd_set fdread{};
432432
fd_set fdwrite{};
433433
fd_set fdexcep{};
434-
int maxfd = 0;
434+
int maxfd = -1;
435435

436436
FD_ZERO(&fdread);
437437
FD_ZERO(&fdwrite);
438438
FD_ZERO(&fdexcep);
439439

440440
requests.fdset(&fdread, &fdwrite, &fdexcep, &maxfd);
441441

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

450460
if (progressfunc != nullptr) {

0 commit comments

Comments
 (0)