[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL for-2.0 3/6] nbd: close socket if connection breaks
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL for-2.0 3/6] nbd: close socket if connection breaks |
Date: |
Fri, 14 Mar 2014 17:14:50 +0100 |
nbd_receive_reply() is called by the event loop whenever data is
available or the socket has been closed by the remote side.
This patch closes the socket when an error occurs to prevent the
nbd_receive_reply() handler from being called indefinitely after the
connection has failed.
Note that we were already correctly returning EIO for pending requests
but leaving the nbd_receive_reply() handler registered resulted in high
CPU consumption and a flood of error messages.
Reuse nbd_teardown_connection() to close the socket.
Reported-by: Zhifeng Cai <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block/nbd-client.c | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/block/nbd-client.c b/block/nbd-client.c
index 0922b78..7d698cb 100644
--- a/block/nbd-client.c
+++ b/block/nbd-client.c
@@ -43,6 +43,17 @@ static void nbd_recv_coroutines_enter_all(NbdClientSession
*s)
}
}
+static void nbd_teardown_connection(NbdClientSession *client)
+{
+ /* finish any pending coroutines */
+ shutdown(client->sock, 2);
+ nbd_recv_coroutines_enter_all(client);
+
+ qemu_aio_set_fd_handler(client->sock, NULL, NULL, NULL);
+ closesocket(client->sock);
+ client->sock = -1;
+}
+
static void nbd_reply_ready(void *opaque)
{
NbdClientSession *s = opaque;
@@ -78,7 +89,7 @@ static void nbd_reply_ready(void *opaque)
}
fail:
- nbd_recv_coroutines_enter_all(s);
+ nbd_teardown_connection(s);
}
static void nbd_restart_write(void *opaque)
@@ -324,7 +335,7 @@ int nbd_client_session_co_discard(NbdClientSession *client,
int64_t sector_num,
}
-static void nbd_teardown_connection(NbdClientSession *client)
+void nbd_client_session_close(NbdClientSession *client)
{
struct nbd_request request = {
.type = NBD_CMD_DISC,
@@ -332,22 +343,14 @@ static void nbd_teardown_connection(NbdClientSession
*client)
.len = 0
};
- nbd_send_request(client->sock, &request);
-
- /* finish any pending coroutines */
- shutdown(client->sock, 2);
- nbd_recv_coroutines_enter_all(client);
-
- qemu_aio_set_fd_handler(client->sock, NULL, NULL, NULL);
- closesocket(client->sock);
- client->sock = -1;
-}
-
-void nbd_client_session_close(NbdClientSession *client)
-{
if (!client->bs) {
return;
}
+ if (client->sock == -1) {
+ return;
+ }
+
+ nbd_send_request(client->sock, &request);
nbd_teardown_connection(client);
client->bs = NULL;
--
1.8.5.3
- [Qemu-devel] [PULL for-2.0 0/6] Block patches, Stefan Hajnoczi, 2014/03/14
- [Qemu-devel] [PULL for-2.0 3/6] nbd: close socket if connection breaks,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL for-2.0 1/6] blockdev: Refuse to open encrypted image unless paused, Stefan Hajnoczi, 2014/03/14
- [Qemu-devel] [PULL for-2.0 5/6] qemu-iotests: add 083 NBD client disconnect tests, Stefan Hajnoczi, 2014/03/14
- [Qemu-devel] [PULL for-2.0 6/6] qemu-iotests: remove 085 and 087 from 'quick' group, Stefan Hajnoczi, 2014/03/14
- [Qemu-devel] [PULL for-2.0 2/6] block: Explicitly specify 'unsigned long long' for VHDX 64-bit constants, Stefan Hajnoczi, 2014/03/14
- [Qemu-devel] [PULL for-2.0 4/6] tests: add nbd-fault-injector.py utility, Stefan Hajnoczi, 2014/03/14
- Re: [Qemu-devel] [PULL for-2.0 0/6] Block patches, Peter Maydell, 2014/03/15