Skip to content

Commit 21cdbe5

Browse files
Wieger Opmeercmouse
authored andcommitted
second take of busy waiting fix, now with blocking io
1 parent 654d9e0 commit 21cdbe5

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

modules/remotebackend/unixconnector.cc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ int UnixsocketConnector::recv_message(rapidjson::Document &output) {
5353
s_output = "";
5454

5555
while((t.tv_sec - t0.tv_sec)*1000 + (t.tv_usec - t0.tv_usec)/1000 < this->timeout) {
56+
int avail = waitForData(this->fd, 0, this->timeout * 500); // use half the timeout as poll timeout
57+
if (avail < 0) // poll error
58+
return -1;
59+
if (avail == 0) { // timeout
60+
gettimeofday(&t, NULL);
61+
continue;
62+
}
63+
5664
std::string temp;
5765
temp.clear();
5866

@@ -110,7 +118,7 @@ ssize_t UnixsocketConnector::write(const std::string &data) {
110118
nbuf = data.copy(buf, sizeof buf, pos); // copy data and write
111119
nwrite = ::write(fd, buf, nbuf);
112120
pos = pos + sizeof(buf);
113-
if (nwrite == -1) {
121+
if (nwrite < 1) {
114122
connected = false;
115123
close(fd);
116124
return -1;
@@ -141,17 +149,7 @@ void UnixsocketConnector::reconnect() {
141149
return;
142150
}
143151

144-
if (fcntl(fd, F_SETFL, O_NONBLOCK, &fd)) {
145-
connected = false;
146-
L<<Logger::Error<<"Cannot manipulate socket: " << strerror(errno) << std::endl;;
147-
close(fd);
148-
return;
149-
}
150-
151-
if((rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock))==-1 && (errno == EINPROGRESS)) {
152-
waitForData(fd, 0, -1);
153-
rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock);
154-
}
152+
rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock);
155153

156154
if (rv != 0 && errno != EISCONN && errno != 0) {
157155
L<<Logger::Error<<"Cannot connect to socket: " << strerror(errno) << std::endl;

0 commit comments

Comments
 (0)