All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rjones@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	pkrempa@redhat.com, Alberto Garcia <berto@igalia.com>,
	slp@redhat.com, qemu-block@nongnu.org,
	Markus Armbruster <armbru@redhat.com>,
	qemu-devel@nongnu.org, mreitz@redhat.com,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Klaus Jensen <its@irrelevant.dk>,
	philmd@redhat.com, sgarzare@redhat.com
Subject: libnbd thread-local errors and dlclose (was: Re: [ANNOUNCE] libblkio v0.1.0 preview release)
Date: Thu, 29 Apr 2021 18:17:32 +0100	[thread overview]
Message-ID: <20210429171732.GX26415@redhat.com> (raw)
In-Reply-To: <YIrL5kE+0ULVN2lK@redhat.com>

On Thu, Apr 29, 2021 at 04:08:22PM +0100, Daniel P. Berrangé wrote:
> On Thu, Apr 29, 2021 at 04:00:38PM +0100, Richard W.M. Jones wrote:
> > On Thu, Apr 29, 2021 at 03:41:29PM +0100, Stefan Hajnoczi wrote:
> > > On Thu, Apr 29, 2021 at 03:22:59PM +0100, Richard W.M. Jones wrote:
> > > > libvirt originally, and now libnbd, keep a per-thread error message
> > > > (stored in thread-local storage).  It's a lot nicer than having to
> > > > pass &errmsg to every function.  You can just write:
> > > > 
> > > >  if (nbd_connect_tcp (nbd, "remote", "nbd") == -1) {
> > > >    fprintf (stderr,
> > > >             "failed to connect to remote server: %s (errno = %d)\n",
> > > >             nbd_get_error (), nbd_get_errno ());
> > > >    exit (EXIT_FAILURE);
> > > >  }
> > > > 
> > > > (https://libguestfs.org/libnbd.3.html#ERROR-HANDLING)
> > > > 
> > > > It means you can extend the range of error information available in
> > > > future.  Also you can return a 'const char *' and the application
> > > > doesn't have to worry about lifetimes, at least in the common case.
> > > 
> > > Thanks for sharing the idea, I think it would work well for libblkio
> > > too.
> > > 
> > > Do you ignore the dlclose(3) memory leak?
> > 
> > I believe this mechanism in libnbd ensures there is no leak in the
> > ordinary shared library (not dlopen/dlclose) case:
> > 
> > https://gitlab.com/nbdkit/libnbd/-/blob/f9257a9fdc68706a4079deb4ced61e1d468f28d6/lib/errors.c#L35
> > 
> > However I'm not sure what happens in the dlopen case, so I'm going to
> > test that out now ...
> 
> pthread_key destructors are a disaster waiting to happen with
> dlclose, if the dlclose happens while non-main threads are
> still running. When those threads exit, they'll run the
> destructor which points to a function that is no longer
> resident in memory.

While libnbd had a memory leak there, now fixed by:

https://gitlab.com/nbdkit/libnbd/-/commit/026d281c57dd95485cc9bf829918b5efd9e32ddb

I don't actually think we have the bug you're describing.  We have a
destructor (errors_free()) which runs on dlclose, which deletes the
thread-local storage key, so the destructor will not run after the
library has been unloaded.

I added a test for this which works fine for me:

https://gitlab.com/nbdkit/libnbd/-/commit/831d142787aba4f5b638418e02cf7e0f2a051565

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org



  parent reply	other threads:[~2021-04-29 17:27 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-29 14:05 [ANNOUNCE] libblkio v0.1.0 preview release Stefan Hajnoczi
2021-04-29 14:22 ` Richard W.M. Jones
2021-04-29 14:41   ` Stefan Hajnoczi
2021-04-29 15:00     ` Richard W.M. Jones
2021-04-29 15:08       ` Daniel P. Berrangé
2021-04-29 15:31         ` Richard W.M. Jones
2021-04-29 15:35           ` Daniel P. Berrangé
2021-04-29 17:17         ` Richard W.M. Jones [this message]
2021-04-29 17:27           ` libnbd thread-local errors and dlclose (was: Re: [ANNOUNCE] libblkio v0.1.0 preview release) Daniel P. Berrangé
2021-04-30 16:20       ` [ANNOUNCE] libblkio v0.1.0 preview release Stefan Hajnoczi
2021-04-29 15:51 ` Kevin Wolf
2021-04-30 15:49   ` Stefan Hajnoczi
2021-05-04 13:44     ` Kevin Wolf
2021-05-05 16:19       ` Stefan Hajnoczi
2021-05-05 16:46         ` Kevin Wolf
2021-05-06  8:46           ` Stefan Hajnoczi
2021-05-06 10:33             ` Kevin Wolf
2021-05-06 13:53               ` Stefan Hajnoczi
2021-05-13  9:47               ` Stefan Hajnoczi
2021-05-14 15:55                 ` Kevin Wolf
2021-05-17 14:09                   ` Stefan Hajnoczi
2021-05-18  8:02                     ` Kevin Wolf

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=20210429171732.GX26415@redhat.com \
    --to=rjones@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=berto@igalia.com \
    --cc=its@irrelevant.dk \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=philmd@redhat.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=slp@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /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.