Skip to content

Commit

Permalink
Fix error handling when connection closed by peer
Browse files Browse the repository at this point in the history
We checked for zero read when reading the packet but not when reading
the header, so we tried to read zero bytes from the closed socket (which
succeeds), and then tried to write zero length packet to vmnet, which
fails with VMNET_INVALID_ARGUMENT:

Example logs (from my logging branch):

    ERROR| accept: Interrupted system call
    ERROR| vmnet_write: [1003] VMNET_INVALID_ARGUMENT
    INFO | Closing a connection (fd 5)
    ERROR| writev: Bad file descriptor
    ERROR| writev: Bad file descriptor
    ERROR| vmnet_write: [1003] VMNET_INVALID_ARGUMENT
    INFO | Closing a connection (fd 6)

Now we check for zero read and log a clear message:

   Connection closed by peer (fd 5)

Signed-off-by: Nir Soffer <[email protected]>
  • Loading branch information
nirs committed Sep 30, 2024
1 parent fa0dbdd commit da54648
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ static void on_accept(struct state *state, int accept_fd, interface_ref iface) {
accept_fd);
uint32_t header_be = 0;
ssize_t header_received = read(accept_fd, &header_be, 4);
if (header_received == 0) {
// EOF according to man page of read.
fprintf(stderr, "Connection closed by peer (fd %d)\n", accept_fd);
goto done;
}
if (header_received < 0) {
perror("read[header]");
goto done;
Expand All @@ -495,6 +500,7 @@ static void on_accept(struct state *state, int accept_fd, interface_ref iface) {
}
if (received == 0) {
// EOF according to man page of read.
fprintf(stderr, "Connection closed by peer (fd %d)\n", accept_fd);
goto done;
}
assert(received == header);
Expand Down

0 comments on commit da54648

Please sign in to comment.