[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [libnbd PATCH 1/2] lib: Decrement in_flight at response, not retirement



We had a mismatch between documentation/code comments
(nbd_aio_in_flight counts commands not yet ready to retire) and actual
code (we didn't decrement the count until nbd_aio_command_completed).
As the documented behavior is useful (I shouldn't delay sending a
command to the server merely because I haven't retired all the
available commands), it is worth fixing the code to match the intent.
This also means decrementing in_flight in bulk when commands are
stranded due to a transition to CLOSED/DEAD.

Update the testsuite to cover this.

Fixes: a05b2237
---
 generator/states-reply.c |  2 ++
 generator/states.c       |  2 ++
 lib/aio.c                |  2 --
 tests/server-death.c     | 10 +++++-----
 4 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/generator/states-reply.c b/generator/states-reply.c
index 05b2e58..4b22c39 100644
--- a/generator/states-reply.c
+++ b/generator/states-reply.c
@@ -179,6 +179,8 @@ save_reply_state (struct nbd_handle *h)
   }
   else
     h->cmds_done = cmd;
+  h->in_flight--;
+  assert (h->in_flight >= 0);

   /* Notify the user */
   if (cmd->cb.callback) {
diff --git a/generator/states.c b/generator/states.c
index fa6df9f..bc2b4a6 100644
--- a/generator/states.c
+++ b/generator/states.c
@@ -156,6 +156,7 @@ void abort_commands (struct nbd_handle *h,
   assert (nbd_get_error ());
   abort_commands (h, &h->cmds_to_issue);
   abort_commands (h, &h->cmds_in_flight);
+  h->in_flight = 0;
   if (h->sock) {
     h->sock->ops->close (h->sock);
     h->sock = NULL;
@@ -165,6 +166,7 @@ void abort_commands (struct nbd_handle *h,
  CLOSED:
   abort_commands (h, &h->cmds_to_issue);
   abort_commands (h, &h->cmds_in_flight);
+  h->in_flight = 0;
   if (h->sock) {
     h->sock->ops->close (h->sock);
     h->sock = NULL;
diff --git a/lib/aio.c b/lib/aio.c
index 5df20da..2fae10d 100644
--- a/lib/aio.c
+++ b/lib/aio.c
@@ -85,8 +85,6 @@ nbd_unlocked_aio_command_completed (struct nbd_handle *h,
     prev_cmd->next = cmd->next;
   else
     h->cmds_done = cmd->next;
-  h->in_flight--;
-  assert (h->in_flight >= 0);

   free (cmd);

diff --git a/tests/server-death.c b/tests/server-death.c
index c9195c5..4093c20 100644
--- a/tests/server-death.c
+++ b/tests/server-death.c
@@ -128,6 +128,11 @@ main (int argc, char *argv[])
   }

   /* Detection of the dead server completes all remaining in-flight commands */
+  if (nbd_aio_in_flight (nbd) != 0) {
+    fprintf (stderr, "%s: test failed: nbd_aio_in_flight\n",
+             argv[0]);
+    goto fail;
+  }
   if (nbd_aio_peek_command_completed (nbd) != cookie) {
     fprintf (stderr, "%s: test failed: nbd_aio_peek_command_completed\n",
              argv[0]);
@@ -148,11 +153,6 @@ main (int argc, char *argv[])
   }

   /* With all commands retired, no further command should be pending */
-  if (nbd_aio_in_flight (nbd) != 0) {
-    fprintf (stderr, "%s: test failed: nbd_aio_in_flight\n",
-             argv[0]);
-    goto fail;
-  }
   if (nbd_aio_peek_command_completed (nbd) != -1) {
     fprintf (stderr, "%s: test failed: nbd_aio_peek_command_completed\n",
              argv[0]);
-- 
2.20.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]