From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P . Berrange" <berrange@redhat.com>,
"Christian Borntraeger" <borntraeger@de.ibm.com>,
"Fam Zheng" <famz@redhat.com>, "Kevin Wolf" <kwolf@redhat.com>,
"Max Reitz" <mreitz@redhat.com>,
peterx@redhat.com, "Eric Auger" <eric.auger@redhat.com>,
"Eric Blake" <eblake@redhat.com>, "John Snow" <jsnow@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Dr . David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [PATCH v3 2/6] monitor: flush qmp responses when CLOSED
Date: Fri, 15 Jun 2018 09:42:45 +0800 [thread overview]
Message-ID: <20180615014249.22730-3-peterx@redhat.com> (raw)
In-Reply-To: <20180615014249.22730-1-peterx@redhat.com>
Previously we clean up the queues when we got CLOSED event. It was used
to make sure we won't send leftover replies/events of a old client to a
new client. Now this patch does an extra operation to flush the
response queue before cleaning up.
In most cases, a QMP session will be based on a bidirectional channel (a
TCP port, for example, we read/write to the same socket handle), so in
port and out port of the backend chardev are fundamentally the same
port. In these cases, it does not really matter much on whether we'll
flush the response queue since flushing should possibly fail
anyways (just imagine to write to a socket that has already closed).
However there can be cases where in & out ports of the QMP monitor's
backend chardev are separated. One true example:
cat $QMP_COMMANDS | qemu -qmp stdio ... | filter_commands
In this case, the backend is fd-typed, and it is connected to stdio
where in port is stdin and out port is stdout. Now if we drop all the
events on the response queue then filter_command process might miss some
events that it might expect. The thing is that, when stdin closes,
stdout might still be there alive!
In practice, I encountered a very strange SHUTDOWN event missing when
running test with iotest 087 with Out-Of-Band enabled. One condition
could be this (after "quit" command is executed and QEMU quits the main
loop):
1. [main thread] QEMU queues one SHUTDOWN event into response queue
2. "cat" terminates (to distinguish it from the animal, I quote it).
Logically it can terminate even earlier, but let's just assume it's
here.
3. [monitor iothread] QEMU reads EOF from stdin, which connects to the
"cat" process
4. [monitor iothread] QEMU calls the CLOSED event hook for the monitor,
which will clean up the response queue of the monitor, then the
SHUTDOWN event is dropped
5. [main thread] clean up the monitors in monitor_cleanup(), when
trying to flush pending responses, it sees nothing. SHUTDOWN is
lost forever
Note that before the monitor iothread was introduced, step [4]/[5] could
never happen since the main loop was the only place to detect the EOF
event of stdin and run the CLOSED event hooks. Now things can happen in
parallel in the iothread.
Without this patch, iotest 087 will have ~10% chance to miss the
SHUTDOWN event and fail when with Out-Of-Band enabled:
087 8s ... - output mismatch (see 087.out.bad)
--- /home/peterx/git/qemu/tests/qemu-iotests/087.out 2018-06-01 18:44:22.378982462 +0800
+++ /home/peterx/git/qemu/bin/tests/qemu-iotests/087.out.bad 2018-06-01 18:53:44.267840928 +0800
@@ -8,7 +8,6 @@
{"return": {}}
{"error": {"class": "GenericError", "desc": "'node-name' must be specified for the root node"}}
{"return": {}}
-{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}}
=== Duplicate ID ===
@@ -53,7 +52,6 @@
{"return": {}}
{"return": {}}
{"return": {}}
-{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN", "data": {"guest": false}}
This patch fixes the problem.
Fixes: 6d2d563f8c ("qmp: cleanup qmp queues properly", 2018-03-27)
Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
monitor.c | 30 +++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/monitor.c b/monitor.c
index 6d0cec552e..e59d4f09ac 100644
--- a/monitor.c
+++ b/monitor.c
@@ -512,20 +512,39 @@ struct QMPResponse {
};
typedef struct QMPResponse QMPResponse;
+static QObject *monitor_qmp_response_pop_one(Monitor *mon)
+{
+ QObject *data;
+
+ qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
+ data = g_queue_pop_head(mon->qmp.qmp_responses);
+ qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
+
+ return data;
+}
+
+static void monitor_qmp_response_flush(Monitor *mon)
+{
+ QObject *data;
+
+ while ((data = monitor_qmp_response_pop_one(mon))) {
+ monitor_json_emitter_raw(mon, data);
+ qobject_unref(data);
+ }
+}
+
/*
* Return one QMPResponse. The response is only valid if
* response.data is not NULL.
*/
-static QMPResponse monitor_qmp_response_pop_one(void)
+static QMPResponse monitor_qmp_response_pop(void)
{
Monitor *mon;
QObject *data = NULL;
qemu_mutex_lock(&monitor_lock);
QTAILQ_FOREACH(mon, &mon_list, entry) {
- qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
- data = g_queue_pop_head(mon->qmp.qmp_responses);
- qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
+ data = monitor_qmp_response_pop_one(mon);
if (data) {
break;
}
@@ -539,7 +558,7 @@ static void monitor_qmp_bh_responder(void *opaque)
QMPResponse response;
while (true) {
- response = monitor_qmp_response_pop_one();
+ response = monitor_qmp_response_pop();
if (!response.data) {
break;
}
@@ -4366,6 +4385,7 @@ static void monitor_qmp_event(void *opaque, int event)
mon_refcount++;
break;
case CHR_EVENT_CLOSED:
+ monitor_qmp_response_flush(mon);
monitor_qmp_cleanup_queues(mon);
json_message_parser_destroy(&mon->qmp.parser);
json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
--
2.17.1
next prev parent reply other threads:[~2018-06-15 1:43 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-15 1:42 [Qemu-devel] [PATCH v3 0/6] monitor: enable OOB by default Peter Xu
2018-06-15 1:42 ` [Qemu-devel] [PATCH v3 1/6] chardev: comment details for CLOSED event Peter Xu
2018-06-15 12:49 ` Markus Armbruster
2018-06-18 15:55 ` Stefan Hajnoczi
2018-06-15 1:42 ` Peter Xu [this message]
2018-06-15 8:11 ` [Qemu-devel] [PATCH v3 2/6] monitor: flush qmp responses when CLOSED Markus Armbruster
2018-06-19 4:35 ` Peter Xu
2018-06-19 13:40 ` Markus Armbruster
2018-06-15 1:42 ` [Qemu-devel] [PATCH v3 3/6] tests: iotests: drop some stderr line Peter Xu
2018-06-15 8:13 ` Markus Armbruster
2018-06-19 4:41 ` Peter Xu
2018-06-15 1:42 ` [Qemu-devel] [PATCH v3 4/6] docs: mention shared state protect for OOB Peter Xu
2018-06-15 12:37 ` Markus Armbruster
2018-06-19 4:49 ` Peter Xu
2018-06-15 1:42 ` [Qemu-devel] [PATCH v3 5/6] monitor: remove "x-oob", turn oob on by default Peter Xu
2018-06-15 1:42 ` [Qemu-devel] [PATCH v3 6/6] Revert "tests: Add parameter to qtest_init_without_qmp_handshake" Peter Xu
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=20180615014249.22730-3-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=borntraeger@de.ibm.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=eric.auger@redhat.com \
--cc=famz@redhat.com \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mreitz@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.