monitor/qmp: resume monitor when clearing its queue
PATCH v3 1/1 monitor/qmp: resume monitor when clearing its queue
When a monitor's queue is filled up in handle_qmp_command() it gets suspended. It's the dispatcher bh's job currently to resume the monitor, which it does after processing an event from the queue. However, it is possible for a CHR_EVENT_CLOSED event to be processed before before the bh is scheduled, which will clear the queue without resuming the monitor, thereby preventing the dispatcher from reaching the resume() call.
Any new connections to the qmp socket will be accept()ed and show the greeting, but will not respond to any messages sent afterwards (as they will not be read from the still-suspended socket).
Fix this by resuming the monitor when clearing a queue which was filled up.
---
Changes since v2:
* Rename monitor_qmp_cleanup_queues_and_resume to use singular
`queue`.
* Make the need_resume condition to actually be readable the same way the comment above it describes it.
* Update comments as suggested by Markus and linewrap at column 70.
* and comment ypo fixups
Changes from v1 to v2:
* Update commit message to include the resulting symptoms.
* Moved the resume code from monitor_qmp_cleanup_req_queue_locked to monitor_qmp_cleanup_queues to avoid an unnecessary resume when destroying the monitor (as the _locked version is also used by monitor_data_destroy().
* Renamed monitor_qmp_cleanup_queues to monitor_qmp_cleanup_queues_and_resume to reflect the change and be verbose about it for potential future users of the function. Currently the only user is monitor_qmp_event() in the CHR_EVENT_CLOSED case, which is exactly the problematic case currently.
monitor/qmp.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
monitor/qmp.c +31 -5
@@ -75,10 +75,35 @@ static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon) } } -static void monitor_qmp_cleanup_queues (MonitorQMP *mon) +static void monitor_qmp_cleanup_queue_and_resume (MonitorQMP *mon) {qemu_mutex_lock (&mon->qmp_queue_lock); + +/* +* Same condition as in monitor_qmp_bh_dispatcher(), but before +* removing an element from the queue (hence no `- 1`). +* Also, the queue should not be empty either, otherwise the +* monitor hasn't been suspended yet (or was already resumed). +*/ +bool need_resume = (!qmp_oob_enabled (mon) || + mon->qmp_requests->length ==QMP_REQ_QUEUE_LEN_MAX ) + && !g_queue_is_empty (mon->qmp_requests); +monitor_qmp_cleanup_req_queue_locked (mon); + +if (need_resume) { +/* +* handle_qmp_command() suspended the monitor because the +* request queue filled up, to be resumed when the queue has +* space again. We just emptied it; resume the monitor. +* +* Without this, the monitor would remain suspended forever +* when we get here while the monitor is suspended. An +* unfortunately timed CHR_EVENT_CLOSED can do the trick. +*/ +monitor_resume (&mon->common); + } +qemu_mutex_unlock (&mon->qmp_queue_lock); }
@@ -263,9 +288,10 @@ static void handle_qmp_command(void *opaque, QObject *req, Error *err)/* * Suspend the monitor when we can't queue more requests after -* this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume -* it. Note that when OOB is disabled, we queue at most one -* command, for backward compatibility. +* this one. Dequeuing in monitor_qmp_bh_dispatcher() or +* monitor_qmp_cleanup_queue_and_resume() will resume it. +* Note that when OOB is disabled, we queue at most one command, +* for backward compatibility. */ if (!qmp_oob_enabled (mon) || mon->qmp_requests->length ==QMP_REQ_QUEUE_LEN_MAX -1 ) {
@@ -332,7 +358,7 @@ static void monitor_qmp_event(void *opaque, int event)* stdio, it's possible that stdout is still open when stdin * is closed. */ -monitor_qmp_cleanup_queues (mon); +monitor_qmp_cleanup_queue_and_resume (mon);json_message_parser_destroy (&mon->parser);json_message_parser_init (&mon->parser, handle_qmp_command, mon,NULL );
Signature · 1 line
2.20.1
Quoted 15 lines · Wolfgang Bumiller
When a monitor's queue is filled up in
handle_qmp_command()it gets suspended. It's the dispatcher bh's job currently to resume the monitor, which it does after processing an event from the queue. However, it is possible for aCHR_EVENT_CLOSEDevent to be processed before before the bh is scheduled, which will clear the queue without resuming the monitor, thereby preventing the dispatcher from reaching theresume()call.
Any new connections to the qmp socket will be accept()ed and show the greeting, but will not respond to any messages sent afterwards (as they will not be read from the still-suspended socket).
Fix this by resuming the monitor when clearing a queue which was filled up.
Quoted 1 line
Signed-off-by Wolfgang Bumiller <[email protected]>
I'm going to prepare a pull request for today's -rc2.