From: "Daniel P. Berrangé" <berrange@redhat.com>
To: guillaume vincke <guillaume.vincke@ovhcloud.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org,
Eric Blake <eblake@redhat.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
Subject: Re: [PATCH v4 1/3] nbd/client-connection: Fix client returning a closed channel
Date: Thu, 3 Dec 2026 17:29:04 +0000 [thread overview]
Message-ID: <axGm4IBOQWFYJrt0@redhat.com> (raw)
In-Reply-To: <44fba603-aeec-42ed-9ade-632dd52fc63f@ovhcloud.com>
On Thu, Dec 03, 2026 at 06:17:26PM +0100, guillaume vincke wrote:
> On 12/3/26 17:02, Daniel P. Berrangé wrote:
> > On Thu, Dec 03, 2026 at 01:57:44PM +0100, guillaume vincke wrote:
> > > On 12/1/26 18:14, Daniel P. Berrangé wrote:
> > > > On Mon, Nov 30, 2026 at 01:44:42PM +0100, Guillaume Vincke wrote:
> > > > > When a server closes the connection after a successful handshake but
> > > > > before running any operations, the channel returned by
> > > > > nbd_co_establish_connection() is the closed one and the connect thread
> > > > > is not started.
> > > > >
> > > > > When configured with reconnect-delay=0 (the default), this situation
> > > > > generates at least two IO errors. The first one happens when trying to
> > > > > issue an operation to the closed socket. The connect thread will be
> > > > > started on the next request, which may happen long after, and will
> > > > > create the second error as nbd_co_establish_connection() returns a NULL
> > > > > channel.
> > > > >
> > > > > Peeking the channel to detect FIN or RST avoids the first IO error and
> > > > > does not delay the starting of the connect thread.
> > > > >
> > > > > Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
> > > > > Reviewed-by: Eric Blake <eblake@redhat.com>
> > > > > ---
> > > > > nbd/client-connection.c | 32 ++++++++++++++++++++++++++++++++
> > > > > 1 file changed, 32 insertions(+)
> > > > >
> > > > > diff --git a/nbd/client-connection.c b/nbd/client-connection.c
> > > > > index 580ebe160b..78caea3c37 100644
> > > > > --- a/nbd/client-connection.c
> > > > > +++ b/nbd/client-connection.c
> > > > > @@ -296,6 +296,23 @@ void nbd_client_connection_release(NBDClientConnection *conn)
> > > > > }
> > > > > }
> > > > > +/* Only true when the peer closed the channel. Does not detect if dead. */
> > > > > +static bool nbd_channel_is_closed(QIOChannel *ioc)
> > > > > +{
> > > > > + char data;
> > > > > + struct iovec iov = { .iov_base = &data, .iov_len = 1 };
> > > > > + ssize_t len = -1;
> > > > > +
> > > > > + if (!qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) {
> > > > > + return false;
> > > > > + }
> > > > > +
> > > > > + len = qio_channel_readv_full(
> > > > > + ioc, &iov, 1, NULL, NULL, QIO_CHANNEL_READ_FLAG_MSG_PEEK, NULL);
> > > > > +
> > > > > + return len == 0 || (len < 0 && len != QIO_CHANNEL_ERR_BLOCK);
> > > > > +}
> > > > > +
> > > > > /*
> > > > > * Get a new connection in context of @conn:
> > > > > * if the thread is running, wait for completion
> > > > > @@ -317,6 +334,7 @@ nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
> > > > > bool blocking, Error **errp)
> > > > > {
> > > > > QemuThread thread;
> > > > > + QIOChannel *ioc;
> > > > > if (conn->do_negotiation) {
> > > > > assert(info);
> > > > > @@ -330,6 +348,20 @@ nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
> > > > > assert(!conn->wait_co);
> > > > > if (!conn->running) {
> > > > > + if (conn->sioc) {
> > > > > + /* discard if the socket got closed by its peer */
> > > > > + ioc = QIO_CHANNEL(conn->sioc);
> > > > > + if (!qio_channel_set_blocking(ioc, false, NULL) ||
> > > > > + nbd_channel_is_closed(ioc)) {
> > > > > +
> > > > > + qio_channel_close(ioc, NULL);
> > > > > + object_unref(OBJECT(conn->sioc));
> > > > > + conn->sioc = NULL;
> > > > > + object_unref(OBJECT(conn->ioc));
> > > > > + conn->ioc = NULL;
> > > > > + }
> > > > > + }
> > > >
> > > > nbd_channel_is_closed() could return "FALSE", and then the
> > > > client could go away, before we get to this next part.
> > > >
> > > > IOW, peeking at the channel doesn't look like it is inherently
> > > > fixing anything, instead it is playing with a race condition
> > > > for when the peer closed the connection.
> > > >
> > >
> > > Thanks for the review.
> > > You are right on this. It does not prevent using a channel closed after
> > > the check but it considerably reduces the window. Without that patch, we
> > > always get a closed channel if the server closes the connection while it
> > > is parked. The channel stays parked as long as the device is idle. The
> > > race condition you describe represents the short window between the
> > > check and the first request. So, the patch addresses the certain
> > > failure, not the residual race.
> >
> > I'm still struggling to understand why we need to create all these
> > hacks for detecting EOF by peeking at incoming bytes at only one
> > specific place int he code, when the QIOChannel code has a well
> > defined mechanism to detect it - create a I/O watch and look for
> > G_IO_HUP to be signalled.
> >
>
> I did not know this was the expected mechanism. Thanks for pointing
> that out.
> I understand that you consider the peeking to be a hack. The problem
> with TLS is that a read stops after each non-data record so a one-shot
> check cannot tell whether a close_notify is still queued without
> looking at the underlying socket. Your solution would avoid all that
> but it requires attaching the parked channel to the AioContext which
> looks like a bigger change. Would you prefer that solution ?
I can just say that's the recommended approach from the I/O channel
API design. I don't know enough about the block/nbd code to give advice
on the specific challenges/tradeoffs of integration. So I'll defer to
Eric to give an opinion on this alternative implementation idea.
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
next prev parent reply other threads:[~2026-12-03 17:29 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 12:44 [PATCH v4 0/3] nbd/client-connection: Fix client returning a closed channel Guillaume Vincke
2026-11-30 12:44 ` [PATCH v4 1/3] " Guillaume Vincke
2026-12-01 17:14 ` Daniel P. Berrangé
2026-12-03 12:57 ` guillaume vincke
2026-12-03 16:02 ` Daniel P. Berrangé
2026-12-03 17:17 ` guillaume vincke
2026-12-03 17:29 ` Daniel P. Berrangé [this message]
2026-12-03 17:31 ` Eric Blake
2026-11-30 12:44 ` [PATCH v4 2/3] nbd/client-connection: Fix client returning a closed TLS channel Guillaume Vincke
2026-12-01 16:23 ` Eric Blake
2026-11-30 12:44 ` [PATCH v4 3/3] iotests: Add coverage for NBD connect thread start delay Guillaume Vincke
2026-12-01 17:05 ` Eric Blake
2026-12-03 15:31 ` guillaume vincke
2026-12-03 15:53 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axGm4IBOQWFYJrt0@redhat.com \
--to=berrange@redhat.com \
--cc=eblake@redhat.com \
--cc=guillaume.vincke@ovhcloud.com \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.