diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 5991766795..6c107c53fa 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -2390,6 +2390,13 @@ int varlink_collect_full( JsonVariant *e = json_variant_by_key(v->current, "error"), *p = json_variant_by_key(v->current, "parameters"); + /* Unless there is more to collect we reset state to idle */ + if (!FLAGS_SET(v->current_reply_flags, VARLINK_REPLY_CONTINUES)) { + varlink_set_state(v, VARLINK_IDLE_CLIENT); + assert(v->n_pending == 1); + v->n_pending--; + } + if (e) { if (!ret_error_id) return varlink_error_to_errno(json_variant_string(e), p); @@ -2418,10 +2425,6 @@ int varlink_collect_full( continue; } - varlink_set_state(v, VARLINK_IDLE_CLIENT); - assert(v->n_pending == 1); - v->n_pending--; - if (ret_parameters) /* Install the collection array in the connection object, so that we can hand * out a pointer to it without passing over ownership, to make it work more diff --git a/src/test/test-varlink.c b/src/test/test-varlink.c index 67ad213002..65e7dfafe9 100644 --- a/src/test/test-varlink.c +++ b/src/test/test-varlink.c @@ -239,6 +239,7 @@ static void flood_test(const char *address) { static void *thread(void *arg) { _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; _cleanup_(json_variant_unrefp) JsonVariant *i = NULL; + _cleanup_(json_variant_unrefp) JsonVariant *wrong = NULL; JsonVariant *o = NULL, *k = NULL, *j = NULL; const char *error_id; const char *e; @@ -252,6 +253,13 @@ static void *thread(void *arg) { assert_se(varlink_set_allow_fd_passing_input(c, true) >= 0); assert_se(varlink_set_allow_fd_passing_output(c, true) >= 0); + /* Test that client is able to perform two sequential varlink_collect calls if first resulted in an error */ + assert_se(json_build(&wrong, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)), + JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(99)))) >= 0); + assert_se(varlink_collect(c, "io.test.DoSomethingMore", wrong, &j, &error_id) >= 0); + assert_se(strcmp_ptr(error_id, "org.varlink.service.InvalidParameter") == 0); + + assert_se(varlink_collect(c, "io.test.DoSomethingMore", i, &j, &error_id) >= 0); assert_se(!error_id);