qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Yongji Xie" <elohimes@gmail.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH 12/12] chardev: fix race with client connections in tcp_chr_wait_connected
Date: Tue, 15 Jan 2019 14:52:56 +0000	[thread overview]
Message-ID: <20190115145256.9593-13-berrange@redhat.com> (raw)
In-Reply-To: <20190115145256.9593-1-berrange@redhat.com>

When the 'reconnect' option is given for a client connection, the
qmp_chardev_open_socket_client method will run an asynchronous
connection attempt. The QIOChannel socket executes this is a single use
background thread, so the connection will succeed immediately (assuming
the server is listening). The chardev, however, won't get the result
from this background thread until the main loop starts running and
processes idle callbacks.

Thus when tcp_chr_wait_connected is run s->ioc will be NULL, and the
state will still be TCP_CHARDEV_STATE_DISCONNECTED, but there will
already be an established connection that will be associated with the
chardev by the pending idle callback.  tcp_chr_wait_connected doesn't
see this and so attempts to establish another connection synchronously.

If the server allows multiple connections this is unhelpful but not a
fatal problem as the duplicate connection will get ignored by the
tcp_chr_new_client method when it sees the state is already connected.

If the server only supports a single connection, however, the
tcp_chr_wait_connected method will hang forever because the server will
not accept its synchronous connection attempt until the first connection
is closed.

To deal with this we must ensure that qmp_chardev_open_socket_client
does not actually start the asynchronous connection attempt. Instead it
should schedule a timer with 0ms expiry time, which will only be
processed once the main loop starts running. The tcp_chr_wait_connected
method can now safely do a synchronous connection attempt without
creating a race condition. When the timer expires it will see that a
connection has already been established and take no further action.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 chardev/char-socket.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 7e98a95bbd..07942d7a1b 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -965,7 +965,25 @@ static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
         }
     }
 
-    while (!s->ioc) {
+    /*
+     * We expect state to be as follows:
+     *
+     *  - server
+     *    - wait   -> CONNECTED
+     *    - nowait -> DISCONNECTED
+     *  - client
+     *    - reconnect == 0 -> CONNECTED
+     *    - reconnect != 0 -> DISCONNECTED
+     *
+     */
+    if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
+        error_setg(errp,
+                   "Unexpected 'connecting' state when waiting for "
+                   "connection during early startup");
+        return -1;
+    }
+
+    while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
         if (s->is_listen) {
             tcp_chr_accept_server_sync(chr);
         } else {
@@ -1106,7 +1124,15 @@ static int qmp_chardev_open_socket_client(Chardev *chr,
 
     if (reconnect > 0) {
         s->reconnect_time = reconnect;
-        tcp_chr_connect_client_async(chr);
+        /*
+         * We must not start the socket connect attempt until the main
+         * loop is running, otherwise qemu_chr_wait_connect will not be
+         * able to take over connection establishment during startup
+         */
+        s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
+                                                     0,
+                                                     socket_reconnect_timeout,
+                                                     chr);
         return 0;
     } else {
         return tcp_chr_connect_client_sync(chr, errp);
-- 
2.20.1

  parent reply	other threads:[~2019-01-15 14:54 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-15 14:52 [Qemu-devel] [PATCH 00/12] chardev: refactoring & many bugfixes related tcp_chr_wait_connected Daniel P. Berrangé
2019-01-15 14:52 ` [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs Daniel P. Berrangé
2019-01-15 19:13   ` Marc-André Lureau
2019-01-16  5:07   ` Thomas Huth
2019-01-16  9:27     ` Daniel P. Berrangé
2019-01-17  9:21       ` Markus Armbruster
2019-01-17 14:13         ` Eric Blake
2019-01-15 14:52 ` [Qemu-devel] [PATCH 02/12] chardev: forbid 'reconnect' option with server sockets Daniel P. Berrangé
2019-01-15 19:13   ` Marc-André Lureau
2019-01-16  5:11   ` Thomas Huth
2019-01-15 14:52 ` [Qemu-devel] [PATCH 03/12] chardev: forbid 'wait' option with client sockets Daniel P. Berrangé
2019-01-15 19:14   ` Marc-André Lureau
2019-01-16  5:17   ` Thomas Huth
2019-01-15 14:52 ` [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket Daniel P. Berrangé
2019-01-15 19:18   ` Marc-André Lureau
2019-01-16  9:33     ` Daniel P. Berrangé
2019-01-15 19:33   ` Eric Blake
2019-01-16  9:31     ` Daniel P. Berrangé
2019-01-15 14:52 ` [Qemu-devel] [PATCH 05/12] chardev: ensure qemu_chr_parse_compat reports missing driver error Daniel P. Berrangé
2019-01-15 19:20   ` Marc-André Lureau
2019-01-15 14:52 ` [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths Daniel P. Berrangé
2019-01-15 19:39   ` Marc-André Lureau
2019-01-16  5:24   ` Thomas Huth
2019-01-16  5:47     ` Peter Xu
2019-01-16  6:01       ` Thomas Huth
2019-01-16  9:34     ` Daniel P. Berrangé
2019-01-15 14:52 ` [Qemu-devel] [PATCH 07/12] chardev: split tcp_chr_wait_connected into two methods Daniel P. Berrangé
2019-01-15 19:44   ` Marc-André Lureau
2019-01-16  9:36     ` Daniel P. Berrangé
2019-01-15 14:52 ` [Qemu-devel] [PATCH 08/12] chardev: split up qmp_chardev_open_socket connection code Daniel P. Berrangé
2019-01-15 21:02   ` Marc-André Lureau
2019-01-15 14:52 ` [Qemu-devel] [PATCH 09/12] chardev: use a state machine for socket connection state Daniel P. Berrangé
2019-01-15 21:05   ` Marc-André Lureau
2019-01-15 14:52 ` [Qemu-devel] [PATCH 10/12] chardev: honour the reconnect setting in tcp_chr_wait_connected Daniel P. Berrangé
2019-01-15 21:22   ` Marc-André Lureau
2019-01-15 14:52 ` [Qemu-devel] [PATCH 11/12] chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected Daniel P. Berrangé
2019-01-15 21:54   ` Marc-André Lureau
2019-01-16  9:37     ` Daniel P. Berrangé
2019-01-15 14:52 ` Daniel P. Berrangé [this message]
2019-01-21  9:51 ` [Qemu-devel] [PATCH 00/12] chardev: refactoring & many bugfixes related tcp_chr_wait_connected no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190115145256.9593-13-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=elohimes@gmail.com \
    --cc=lvivier@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).