Skip to content

Commit 8fd03cc

Browse files
committed
Fix error handling when connection closed by peer
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]>
1 parent fa0dbdd commit 8fd03cc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ static void on_accept(struct state *state, int accept_fd, interface_ref iface) {
486486
perror("read[header]");
487487
goto done;
488488
}
489+
if (header_received == 0) {
490+
// EOF according to man page of read.
491+
fprintf(stderr, "Connection closed by peer (fd %d)\n", accept_fd);
492+
goto done;
493+
}
489494
uint32_t header = ntohl(header_be);
490495
assert(header <= buf_len);
491496
ssize_t received = read(accept_fd, buf, header);
@@ -495,6 +500,7 @@ static void on_accept(struct state *state, int accept_fd, interface_ref iface) {
495500
}
496501
if (received == 0) {
497502
// EOF according to man page of read.
503+
fprintf(stderr, "Connection closed by peer (fd %d)\n", accept_fd);
498504
goto done;
499505
}
500506
assert(received == header);

0 commit comments

Comments
 (0)