From 9554ca7cc8b26c328d870760f3aaac4e701ba7ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi
Date: Tue, 2 Nov 2021 18:53:30 +0200
Subject: [PATCH 1/1] Socket: Handling error while sending
If the peer closes the socket while we were still sending, it was possible that anything that the peer received was not read at all.
Now we keep reading (and failing to send) until the peer's response has been fully read.
src/platform/posix/socket.c | 55 +++++++++++++++++++------------------
1 file changed, 29 insertions(+), 26 deletions(-)
diff --git a/src/platform/posix/socket.c b/src/platform/posix/socket.c
index 5dcaed0..047ce16 100644
--- a/src/platform/posix/socket.c
+++ b/src/platform/posix/socket.c
@@ -128,10 +128,32 @@ static iThreadResult run_SocketThread_(iThread *thread) {
if (FD_ISSET(output_Pipe(&d->wakeup), &reads)) {
readByte_Pipe(&d->wakeup);
}
/* Check for incoming data. */
if (FD_ISSET(d->socket->fd, &reads)) {
ssize_t readSize = recv(d->socket->fd, data_Block(inbuf), size_Block(inbuf), 0);
if (readSize == 0) {
iWarning("[Socket] peer closed the connection while we were receiving\n");
shutdown_Socket_(d->socket);
return 0;
}
if (readSize == -1) {
if (status_Socket(d->socket) == connected_SocketStatus) {
iWarning("[Socket] error when receiving: %s\n", strerror(errno));
shutdown_Socket_(d->socket);
return errno;
}
/* This was expected. */
return 0;
}
iGuardMutex(smx, {
writeData_Buffer(d->socket->input, constData_Block(inbuf), readSize);
});
iNotifyAudience(d->socket, readyRead, SocketReadyRead);
}
/* Problem with the socket? */
if (FD_ISSET(d->socket->fd, &errors)) {
if (status_Socket(d->socket) == connected_SocketStatus) {
iWarning("[Socket] error when receiving: %s\n", strerror(errno));
iWarning("[Socket] error while receiving: %s\n", strerror(errno));
shutdown_Socket_(d->socket);
return errno;
}
@@ -154,9 +176,12 @@ static iThreadResult run_SocketThread_(iThread *thread) {
ssize_t sent = send(d->socket->fd, ptr, remaining, 0);
if (sent == -1) {
/* Error! */
delete_Block(data);
shutdown_Socket_(d->socket);
return errno;
iWarning("[Socket] peer closed the connection while we were sending "
"(errno:%d)\n", errno);
/* Don't quit immediately because we need to see if something was received. */
/* TODO: Need to set the Socket in a fail state, though?
Now we're assuming that the error will be noticed later. */
break;
}
remaining -= sent;
ptr += sent;
@@ -175,28 +200,6 @@ static iThreadResult run_SocketThread_(iThread *thread) {
}
});
}
/* Check for incoming data. */
if (FD_ISSET(d->socket->fd, &reads)) {
ssize_t readSize = recv(d->socket->fd, data_Block(inbuf), size_Block(inbuf), 0);
if (readSize == 0) {
iWarning("[Socket] peer closed the connection\n");
shutdown_Socket_(d->socket);
return 0;
}
if (readSize == -1) {
if (status_Socket(d->socket) == connected_SocketStatus) {
iWarning("[Socket] error when receiving: %s\n", strerror(errno));
shutdown_Socket_(d->socket);
return errno;
}
/* This was expected. */
return 0;
}
iGuardMutex(smx, {
writeData_Buffer(d->socket->input, constData_Block(inbuf), readSize);
});
iNotifyAudience(d->socket, readyRead, SocketReadyRead);
}
}
return 0;
}
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).