Skip to contentSkip to conversation

nbd/server: Suppress Broken pipe errors on abrupt disconnection

PATCH In review Richard W.M. Jones 3 participants 13 messages Sep 13, 2021 – Nov 17, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nbd/server: Suppress Broken pipe errors on abrupt disconnection

$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'
---
 nbd/server.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
nbd/server.c +6 -1
index 3927f7789d..e0a43802b2 100644
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
         ret = nbd_handle_request(client, &request, req->data, &local_err);
     }
     if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {
+            error_prepend(&local_err, "Failed to send reply: ");
+        } else {
+            error_free(local_err);
+            local_err = NULL;
+        }
         goto disconnect;
     }
 
Signature · 1 line
2.32.0
#1 lore raw
Quoted 7 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd
Quoted 8 lines

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'

My personal opinion, is that this warning doesn't hurt in general. I think in production tools should gracefully shutdown any connection, and abrupt shutdown is a sign of something wrong - i.e., worth warning.

Shouldn't nbdsh do graceful shutdown by default?

Quoted 11 lines
---
  nbd/server.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
nbd/server.c +0 -0
index 3927f7789d..e0a43802b2 100644

nbd/server.c static coroutine_fn void nbd_trip(void *opaque) · line 2672

Quoted 5 lines
nbd/server.c +1 -1
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
          ret = nbd_handle_request(client, &request, req->data, &local_err);
      }
      if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {

Both nbd_handle_request() and nbd_send_generic_reply() declares that they return -errno on failure in communication with client. I think, you should use ret here: if (ret != -EPIPE). It's safer: who knows, does errno really set on all error paths of called functions? If not, we may see here errno of some another previous operation.

Quoted 9 lines
nbd/server.c +5 -0
+            error_prepend(&local_err, "Failed to send reply: ");
+        } else {
+            error_free(local_err);
+            local_err = NULL;
+        }
          goto disconnect;
      }
 
Signature · 2 lines
Best regards,
Vladimir
#2 lore raw
Quoted 15 lines · Vladimir Sementsov-Ogievskiy
Quoted 14 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

Quoted 8 lines
Quoted 1 line
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'

My personal opinion, is that this warning doesn't hurt in general. I think in production tools should gracefully shutdown any connection, and abrupt shutdown is a sign of something wrong - i.e., worth warning.

Shouldn't nbdsh do graceful shutdown by default?

On the client side the only difference is that nbd_shutdown sends NBD_CMD_DISC to the server (versus simply closing the socket). On the server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Quoted 5 lines
Quoted 5 lines
---
 nbd/server.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 16 lines

nbd/server.c static coroutine_fn void nbd_trip(void *opaque) · line 2672

Quoted 10 lines
nbd/server.c +1 -1
index 3927f7789d..e0a43802b2 100644
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
         ret = nbd_handle_request(client, &request, req->data, &local_err);
     }
     if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {

Both nbd_handle_request() and nbd_send_generic_reply() declares that they return -errno on failure in communication with client. I think, you should use ret here: if (ret != -EPIPE). It's safer: who knows, does errno really set on all error paths of called functions? If not, we may see here errno of some another previous operation.

Should we set errno = 0 earlier in nbd_trip()? I don't really know how coroutines in qemu interact with thread-local variables though.

Rich.

Quoted 13 lines
Quoted 8 lines
nbd/server.c +5 -0
+            error_prepend(&local_err, "Failed to send reply: ");
+        } else {
+            error_free(local_err);
+            local_err = NULL;
+        }
         goto disconnect;
     }
 
Signature · 2 lines
Best regards,
Vladimir
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#3 lore raw
Quoted 23 lines · Richard W.M. Jones
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd
Quoted 8 lines

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'

My personal opinion, is that this warning doesn't hurt in general. I think in production tools should gracefully shutdown any connection, and abrupt shutdown is a sign of something wrong - i.e., worth warning.

Quoted 4 lines
Quoted 1 line

Shouldn't nbdsh do graceful shutdown by default?

On the client side the only difference is that nbd_shutdown sends NBD_CMD_DISC to the server (versus simply closing the socket).

Qemu as NBD client always send NBD_CMD_DISC on close() (if io channel is alive).

Quoted 7 lines

On the
server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully

Hmm. Me go to NBD spec :)

I think, there is a reason:

"The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances."

And the only possibility for client to initiate a hard disconnect listed above is "if it detects a violation by the other party of a mandatory condition within this document".

So at least, nbdsh violates NBD protocol. May be spec should be updated to satisfy your needs.

Quoted 3 lines

shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Which ABI will it break?

Quoted 42 lines
Quoted 6 lines
Quoted 6 lines
---
  nbd/server.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 16 lines

nbd/server.c static coroutine_fn void nbd_trip(void *opaque) · line 2672

Quoted 10 lines
nbd/server.c +1 -1
index 3927f7789d..e0a43802b2 100644
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
          ret = nbd_handle_request(client, &request, req->data, &local_err);
      }
      if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {

Both nbd_handle_request() and nbd_send_generic_reply() declares that they return -errno on failure in communication with client. I think, you should use ret here: if (ret != -EPIPE). It's safer: who knows, does errno really set on all error paths of called functions? If not, we may see here errno of some another previous operation.

Should we set errno = 0 earlier in nbd_trip()? I don't really know how coroutines in qemu interact with thread-local variables though.

Rich.

Quoted 13 lines
Quoted 8 lines
nbd/server.c +5 -0
+            error_prepend(&local_err, "Failed to send reply: ");
+        } else {
+            error_free(local_err);
+            local_err = NULL;
+        }
          goto disconnect;
      }
 
Signature · 2 lines
Best regards,
Vladimir
Signature · 2 lines
Best regards,
Vladimir
#4 lore raw
Quoted 11 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones

On the
server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully

Hmm. Me go to NBD spec :)

Quoted 7 lines

I think, there is a reason:

"The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances."

And the only possibility for client to initiate a hard disconnect listed above is "if it detects a violation by the other party of a mandatory condition within this document".

So at least, nbdsh violates NBD protocol. May be spec should be updated to satisfy your needs.

I would say the spec is at best contradictory, but if you read other parts of the spec, then it's pretty clear that we're allowed to drop the connection whenever we like. This section says as much:

https://github.com/NetworkBlockDevice/nbd/blob/5805b25ad3da96e7c0b3160cda51ea19eb518d5b/doc/proto.md#terminating-the-transmission-phase

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Anyway we're dropping the TCP connection because sometimes we are just interrogating an NBD server eg to find out what it supports, and doing a graceful shutdown is a waste of time and internet.

Quoted 5 lines
Quoted 3 lines

shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Which ABI will it break?

Our contract with callers using nbd_close(3), if nbd_shutdown(3) is not called beforehand.
https://libguestfs.org/nbd_shutdown.3.html
https://libguestfs.org/nbd_create.3.html (really nbd_close ...)

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
#5 lore raw
Quoted 18 lines · Richard W.M. Jones
Quoted 17 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones

On the
server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully

Hmm. Me go to NBD spec :)

I think, there is a reason:

"The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances."

And the only possibility for client to initiate a hard disconnect listed above is "if it detects a violation by the other party of a mandatory condition within this document".

Quoted 7 lines
Quoted 1 line

So at least, nbdsh violates NBD protocol. May be spec should be updated to satisfy your needs.

I would say the spec is at best contradictory, but if you read other parts of the spec, then it's pretty clear that we're allowed to drop the connection whenever we like. This section says as much:

https://github.com/NetworkBlockDevice/nbd/blob/5805b25ad3da96e7c0b3160cda51ea19eb518d5b/doc/proto.md#terminating-the-transmission-phase

Hmm, that was exactly the section I read and quoted :)

Quoted 6 lines

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Right. Here the method is defined, no word that client can do it at any time.

Next, in same section:

Either side MAY initiate a hard disconnect if it detects a violation by the other party of a mandatory condition within this document.

Next

The client MAY issue a soft disconnect at any time

And finally:

The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances.

Or do you mean some other spec section I missed?

Quoted 18 lines

Anyway we're dropping the TCP connection because sometimes we are just interrogating an NBD server eg to find out what it supports, and doing a graceful shutdown is a waste of time and internet.

Quoted 5 lines
Quoted 3 lines

shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Which ABI will it break?

Our contract with callers using nbd_close(3), if nbd_shutdown(3) is not called beforehand.
https://libguestfs.org/nbd_shutdown.3.html
https://libguestfs.org/nbd_create.3.html (really nbd_close ...)

Rich.

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw
Quoted 30 lines · Vladimir Sementsov-Ogievskiy
Quoted 18 lines · Richard W.M. Jones
Quoted 17 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones

On the
server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully

Hmm. Me go to NBD spec :)

I think, there is a reason:

"The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances."

And the only possibility for client to initiate a hard disconnect listed above is "if it detects a violation by the other party of a mandatory condition within this document".

Quoted 7 lines
Quoted 1 line

So at least, nbdsh violates NBD protocol. May be spec should be updated to satisfy your needs.

I would say the spec is at best contradictory, but if you read other parts of the spec, then it's pretty clear that we're allowed to drop the connection whenever we like. This section says as much:

https://github.com/NetworkBlockDevice/nbd/blob/5805b25ad3da96e7c0b3160cda51ea19eb518d5b/doc/proto.md#terminating-the-transmission-phase

Hmm, that was exactly the section I read and quoted :)

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Right. Here the method is defined, no word that client can do it at any time.

I don't read this as a definition.

But we should probably clarify the spec to note that dropping the connection is fine, because it is - no one has made any argument so far that there's an actual reason to waste time on NBD_CMD_DISC.

Rich.

Quoted 37 lines

Next, in same section:

Either side MAY initiate a hard disconnect if it detects a violation by the other party of a mandatory condition within this document.

Next
The client MAY issue a soft disconnect at any time

And finally:

The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances.

Or do you mean some other spec section I missed?

Quoted 18 lines

Anyway we're dropping the TCP connection because sometimes we are just interrogating an NBD server eg to find out what it supports, and doing a graceful shutdown is a waste of time and internet.

Quoted 5 lines
Quoted 3 lines

shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Which ABI will it break?

Our contract with callers using nbd_close(3), if nbd_shutdown(3) is not called beforehand.
https://libguestfs.org/nbd_shutdown.3.html
https://libguestfs.org/nbd_create.3.html (really nbd_close ...)

Rich.

Signature · 2 lines
Best regards,
Vladimir
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#7 lore raw
Quoted 37 lines · Richard W.M. Jones
Quoted 36 lines · Vladimir Sementsov-Ogievskiy
Quoted 18 lines · Richard W.M. Jones
Quoted 17 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones

On the
server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

So I don't think there's any reason for libnbd to always gracefully

Hmm. Me go to NBD spec :)

I think, there is a reason:

"The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances."

And the only possibility for client to initiate a hard disconnect listed above is "if it detects a violation by the other party of a mandatory condition within this document".

Quoted 7 lines
Quoted 1 line

So at least, nbdsh violates NBD protocol. May be spec should be updated to satisfy your needs.

I would say the spec is at best contradictory, but if you read other parts of the spec, then it's pretty clear that we're allowed to drop the connection whenever we like. This section says as much:

https://github.com/NetworkBlockDevice/nbd/blob/5805b25ad3da96e7c0b3160cda51ea19eb518d5b/doc/proto.md#terminating-the-transmission-phase

Hmm, that was exactly the section I read and quoted :)

Quoted 6 lines

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Quoted 3 lines
Quoted 1 line

Right. Here the method is defined, no word that client can do it at any time.

I don't read this as a definition.

If so, next paragraphs that specify client behavior doesn't make sense.

Quoted 5 lines

But we should probably clarify the spec to note that dropping the connection is fine, because it is - no one has made any argument so far that there's an actual reason to waste time on NBD_CMD_DISC.

I agree that it's safe enough..

Hmm. If we update the spec to clarify that sending DISC is not necessary, is there any reason to send it at all? We can stop sending it in Qemu as well.

Quoted 39 lines
Quoted 38 lines

Next, in same section:

Either side MAY initiate a hard disconnect if it detects a violation by the other party of a mandatory condition within this document.

Next
The client MAY issue a soft disconnect at any time

And finally:

The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances.

Or do you mean some other spec section I missed?

Quoted 18 lines

Anyway we're dropping the TCP connection because sometimes we are just interrogating an NBD server eg to find out what it supports, and doing a graceful shutdown is a waste of time and internet.

Quoted 5 lines
Quoted 3 lines

shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

Which ABI will it break?

Our contract with callers using nbd_close(3), if nbd_shutdown(3) is not called beforehand.
https://libguestfs.org/nbd_shutdown.3.html
https://libguestfs.org/nbd_create.3.html (really nbd_close ...)

Rich.

Signature · 2 lines
Best regards,
Vladimir
Signature · 2 lines
Best regards,
Vladimir
#8 lore raw
Quoted 7 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Right. Here the method is defined, no word that client can do it at any time.

Note that right now, most TLS clients are NOT gracefully shutting down the TLS session, on either hard or soft disconnect. And this is even observable in non-deterministic behavior of what message the server reports when it diagnoses that a TLS client has gone away. As argued elsewhere, a hard disconnect does not necessarily lose data, but it does add non-determinism into the equation, which makes regression testing a bit harder.

Quoted 6 lines
Quoted 2 lines

I don't read this as a definition.

If so, next paragraphs that specify client behavior doesn't make sense.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

But we should probably clarify the spec to note that dropping the connection is fine, because it is - no one has made any argument so far that there's an actual reason to waste time on NBD_CMD_DISC.

I agree that it's safe enough..

Hmm. If we update the spec to clarify that sending DISC is not necessary, is there any reason to send it at all? We can stop sending it in Qemu as well.

No, I'd still prefer that qemu send NBD_CMD_DISC where possible. Although existing servers tolerate hard disconnects, the way I read the NBD spec is that clients SHOULD prefer soft disconnect, in case it deals with a server where the difference matters.

When implementing 'qemu-nbd --list', I remember having to rejigger code to add in a graceful NBD_OPT_ABORT into more places, for the same reason as sending NBD_CMD_DISC at the end of transmission phase reduces the chance for non-determinism on how the server may react.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#9 lore raw
In reply to Richard W.M. Jones
Quoted 17 lines · Richard W.M. Jones
Quoted 9 lines
Quoted 5 lines

I would say the spec is at best contradictory, but if you read other parts of the spec, then it's pretty clear that we're allowed to drop the connection whenever we like. This section says as much:

https://github.com/NetworkBlockDevice/nbd/blob/5805b25ad3da96e7c0b3160cda51ea19eb518d5b/doc/proto.md#terminating-the-transmission-phase

Hmm, that was exactly the section I read and quoted :)

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

There are two methods of terminating the transmission phase:
...
"The client or the server drops the TCP session (in which case it SHOULD shut down the TLS session first). This is referred to as 'initiating a hard disconnect'."

Right. Here the method is defined, no word that client can do it at any time.

Quoted 18 lines

I don't read this as a definition.

But we should probably clarify the spec to note that dropping the connection is fine, because it is - no one has made any argument so far that there's an actual reason to waste time on NBD_CMD_DISC.

Rich.

Quoted 10 lines

Next, in same section:

Either side MAY initiate a hard disconnect if it detects a violation by the other party of a mandatory condition within this document.

Next
The client MAY issue a soft disconnect at any time

And finally:

The client and the server MUST NOT initiate any form of disconnect other than in one of the above circumstances.

I went and re-read the upstream NBD discussion when this part of the doc was added...

https://lists.debian.org/nbd/2016/04/msg00420.html
https://lists.debian.org/nbd/2016/04/msg00425.html

The argument back then was that sending CMD_DISC is desirable (clients want to be nice to the server) but not mandatory (server must be prepared for clients that aren't nice). Sending CMD_DISC is what counts as initiating soft disconnect; terminating abruptly without CMD_DISC is hard disconnect (servers must not do it without detecting some other first, but clients doing it when they have nothing further to send is something we have to live with).

At the time the text was added, there was a question of whether to add a command NBD_CMD_CLOSE that guaranteed clean server shutdown (the client had to wait for the server's response):
https://lists.debian.org/nbd/2016/04/msg00236.html

but in the end, it was decided that the semantics of NBD_CMD_DISC were already good enough for that purpose. Which in turn means that clients really do expect servers to gracefully flush things to disk on NBD_CMD_DISC, and merely disconnecting (a hard shutdown) after NBD_CMD_FLUSH is a bit riskier than sending a soft shutdown request.

Quoted 8 lines
Quoted 8 lines

Or do you mean some other spec section I missed?

Quoted 4 lines

Anyway we're dropping the TCP connection because sometimes we are just interrogating an NBD server eg to find out what it supports, and doing a graceful shutdown is a waste of time and internet.

You're right that it costs another couple of packets, particularly for things like 'nbdinfo --list', but is it really dominating the time spent in the normal use of NBD? Micro-optimizing the shutdown doesn't have as much payoff as optimizing the speed of NBD_CMD_READ/WRITE.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#10 lore raw
In reply to Richard W.M. Jones
Quoted 23 lines · Richard W.M. Jones
Quoted 22 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd
Quoted 8 lines

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'

My personal opinion, is that this warning doesn't hurt in general. I think in production tools should gracefully shutdown any connection, and abrupt shutdown is a sign of something wrong - i.e., worth warning.

Quoted 8 lines
Quoted 1 line

Shouldn't nbdsh do graceful shutdown by default?

On the client side the only difference is that nbd_shutdown sends NBD_CMD_DISC to the server (versus simply closing the socket). On the server side when the server receives NBD_CMD_DISC it must complete any in-flight requests, but there's no requirement for the server to commit anything to disk. IOW you can still lose data even though you took the time to disconnect.

If you use NBD_CMD_FLUSH as the last command before NBD_CMD_DISC, then you shouldn't have data loss (but it requires the server to support flush). And in general, while a server that does not flush data on CMD_DISC is compliant, it is poor quality of implementation if it strands data that easily, for a client that tried hard to exit gracefully.

Quoted 5 lines

So I don't think there's any reason for libnbd to always gracefully shut down (especially in this case where there are no in-flight requests), and anyway it would break ABI to make that change and slow down the client in cases when there's nothing to clean up.

I agree that we don't want libnbd to always gracefully shut down by default; end users can already choose a graceful shutdown when they want.

At the same time, I would not be opposed to improving the libnbd and nbdkit testsuite usage of libnbd to request graceful shutdown in places where it is currently getting an abrupt disconnect merely because we were lazy when writing the test.

Quoted 23 lines
Quoted 6 lines
Quoted 6 lines
---
 nbd/server.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 16 lines

nbd/server.c static coroutine_fn void nbd_trip(void *opaque) · line 2672

Quoted 10 lines
nbd/server.c +1 -1
index 3927f7789d..e0a43802b2 100644
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
         ret = nbd_handle_request(client, &request, req->data, &local_err);
     }
     if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {

Both nbd_handle_request() and nbd_send_generic_reply() declares that they return -errno on failure in communication with client. I think, you should use ret here: if (ret != -EPIPE). It's safer: who knows, does errno really set on all error paths of called functions? If not, we may see here errno of some another previous operation.

Quoted 2 lines

Should we set errno = 0 earlier in nbd_trip()? I don't really know how coroutines in qemu interact with thread-local variables though.

No, we don't need to set errno to 0 prior to a call except at points where we expect errno to be reliable after the call; but nbd_trip() does not have any guarantees of reliable errno in the first place (instead, it captured errno into the return value prior to any point where errno loses its reliability).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#11 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 12 lines · Vladimir Sementsov-Ogievskiy
Quoted 11 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

Quoted 8 lines
Quoted 4 lines

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'

My personal opinion, is that this warning doesn't hurt in general. I think in production tools should gracefully shutdown any connection, and abrupt shutdown is a sign of something wrong - i.e., worth warning.

Shouldn't nbdsh do graceful shutdown by default?

nbdsh exposes the ability to do graceful shutdown, but does not force it (it is up to the client software using nbdsh whether it calls the right APIs for a graceful shutdown).

We might consider a new API (which we'd then expose via a new command-line option to nbdsh) that requests that libnbd try harder to send NBD_OPT_ABORT or NBD_CMD_DISC prior to closing, but it would still be something that end users would have to opt into using, and not something we can turn on by default.

Quoted 9 lines

nbd/server.c static coroutine_fn void nbd_trip(void *opaque) · line 2672

Quoted 7 lines

+++ b/nbd/server.c

nbd/server.c +1 -1
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
          ret = nbd_handle_request(client, &request, req->data, &local_err);
      }
      if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {

Both nbd_handle_request() and nbd_send_generic_reply() declares that they return -errno on failure in communication with client. I think, you should use ret here: if (ret != -EPIPE). It's safer: who knows, does errno really set on all error paths of called functions? If not, we may see here errno of some another previous operation.

Correct - 'errno' is indeterminate at this point; the correct check is if (-ret != EPIPE). I can make that tweak while taking this patch, if we decide it is worthwhile.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#12 lore raw
In reply to Richard W.M. Jones

Revisiting an older thread

Quoted 10 lines · Richard W.M. Jones
$ rm -f /tmp/sock /tmp/pid
$ qemu-img create -f qcow2 /tmp/disk.qcow2 1M
$ qemu-nbd -t --format=qcow2 --socket=/tmp/sock --pid-file=/tmp/pid /tmp/disk.qcow2 &
$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()'
qemu-nbd: Disconnect client, due to: Failed to send reply: Unable to write to socket: Broken pipe
$ killall qemu-nbd

nbdsh is abruptly dropping the NBD connection here which is a valid way to close the connection. It seems unnecessary to print an error in this case so this commit suppresses it.

I've investigated this a bit further, and found that we have a regression in 6.0 when the abrupt disconnect occurs over TCP instead of Unix sockets. Prior to that point, if a client does a hard disconnect with no pending message, a 5.2 server reports:

$ qemu-nbd -f raw file &
[1] 1059670
$ nbdsh -u nbd://localhost:10809 -c 'h.trim(1,0)'
qemu-nbd: Disconnect client, due to: Failed to read request: Unexpected end-of-file before all bytes were read
[1]+  Done                    qemu-nbd -f raw file

but in 6.0, we started reporting:

qemu-nbd: Disconnect client, due to: Request handling failed in intermediate state

and looking further, I discovered that if you also use --trace=nbd_\*, qemu-nbd ends up tracing uninitialized memory; it is now failing because it sees an unexpected magic number from the uninitialized memory, rather than its earlier detection of early EOF.

It's interesting that a Unix socket sees EPIPE when a TCP socket does not, but it also means that your patch in isolation won't solve the TCP issue, so I'm trying to come up with something today.

Quoted 32 lines

Note that if you call the nbdsh h.shutdown() method then the message was not printed:

$ nbdsh -u 'nbd+unix:///?socket=/tmp/sock' -c 'h.get_size()' -c 'h.shutdown()'
---
 nbd/server.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
nbd/server.c +6 -1
index 3927f7789d..e0a43802b2 100644
@@ -2669,7 +2669,12 @@ static coroutine_fn void nbd_trip(void *opaque)
         ret = nbd_handle_request(client, &request, req->data, &local_err);
     }
     if (ret < 0) {
-        error_prepend(&local_err, "Failed to send reply: ");
+        if (errno != EPIPE) {
+            error_prepend(&local_err, "Failed to send reply: ");
+        } else {
+            error_free(local_err);
+            local_err = NULL;
+        }
         goto disconnect;
     }
 
Signature · 1 line
2.32.0
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org