When the client doesn't support 'async' capability, let's suspend the monitor until the on-going async command completes. That way, the client will be able to use -async commands, but will get only in order reply: (t won't be able to do concurrent commands. In this case, the 'id' field isn't mandatory.
Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- monitor.c | 32 +++++++++++++++++++++++++++++++- qapi/qmp-dispatch.c | 3 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/monitor.c b/monitor.c index c5c70d7e17..3ff32c000c 100644 --- a/monitor.c +++ b/monitor.c @@ -165,6 +165,7 @@ typedef struct { * mode. */ bool in_command_mode; /* are we in command mode? */ + QObject *suspended; QmpClient client; } MonitorQMP; @@ -3675,9 +3676,10 @@ static int monitor_can_read(void *opaque) { Monitor *mon = opaque; - return (mon->suspend_cnt == 0) ? 1 : 0; + return (mon->suspend_cnt == 0 && !mon->qmp.suspended) ? 1 : 0; } + static bool invalid_qmp_mode(const Monitor *mon, const char *cmd, Error **errp) { @@ -3754,11 +3756,33 @@ static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp) return input_dict; } +static void monitor_qmp_suspend(Monitor *mon, QObject *req) +{ + assert(monitor_is_qmp(mon)); + assert(!mon->qmp.suspended); + + qobject_incref(req); + mon->qmp.suspended = req; +} + +static void monitor_qmp_resume(Monitor *mon) +{ + assert(monitor_is_qmp(mon)); + assert(mon->qmp.suspended); + + qobject_decref(mon->qmp.suspended); + mon->qmp.suspended = NULL; +} + static void qmp_dispatch_return(QmpClient *client, QObject *rsp) { Monitor *mon = container_of(client, Monitor, qmp.client); monitor_json_emitter(mon, rsp); + + if (mon->qmp.suspended) { + monitor_qmp_resume(mon); + } } static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens) @@ -3797,6 +3821,12 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens) } qmp_dispatch(&mon->qmp.client, req, rqdict); + + /* suspend if the command is on-going and client doesn't support async */ + if (!QLIST_EMPTY(&mon->qmp.client.pending) && !mon->qmp.client.has_async) { + monitor_qmp_suspend(mon, req); + } + qobject_decref(req); return; diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 76c7402ac8..5bf4b1b520 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -109,7 +109,8 @@ static QObject *do_qmp_dispatch(QObject *request, QmpReturn *qret, Error **errp) } break; case QCT_ASYNC: - if (!qdict_haskey(qret->rsp, "id")) { + if (qret->client->has_async && + !qdict_haskey(qret->rsp, "id")) { error_setg(errp, "An async command requires an 'id'"); break; } -- 2.11.0.295.gd7dffce1c