#786397 glibc: deadlock in dlopen with TLS and threads

#786397#5
Date:
2015-05-21 10:12:37 UTC
From:
To:
When using dlopen on a library with TLS and using threads in its constructor,
there can be a deadlock.

The bug is discuted here:
https://bugzilla.redhat.com/show_bug.cgi?id=1223055

Code to reproduce (taken from the above bug report):
main.c:

#include <stdio.h>
#include <dlfcn.h>
#include <assert.h>

int
main (void)
{
  void *h = dlopen ("./mod1.so", RTLD_NOW | RTLD_GLOBAL);
  assert (h != NULL);
  return 0;
}


mod1.cc:

#include <pthread.h>

class A
{
  private:
    int i;
  public:
    A () {i = 0;}
    ~A () {i = 42;}
    void hello (void) {}
};

thread_local A a;

static void *
thr (void *u)
{
  a.hello ();
  return NULL;
}

void
__attribute__((constructor))
init (void)
{
  pthread_t t;

  pthread_create (&t, NULL, thr, NULL);
  pthread_join (t, NULL);
}


Build mod1.cc with:

g++ -std=c++11 -shared -fPIC -g -o mod1.so mod1.cc -pthread


Note that, on Debian, I can observe the bug in the stable glibc version
(2.19-18) and also in the experimental one (2.21-0experimental0)

  Regards,
    Vincent

#786397#10
Date:
2015-05-21 11:24:13 UTC
From:
To:
Hello,

Vincent Danjean, le Thu 21 May 2015 12:12:37 +0200, a écrit :

Could you discuss directly with upstream about this issue?  I'm afraid
it'll be difficult to solve :)

Samuel

#786397#15
Date:
2015-05-21 11:43:30 UTC
From:
To:
Le 21/05/2015 13:24, Samuel Thibault a écrit :

It seems to be handled by competent people on the redhat bugzilla
(Siddhesh Poyarekar in particular). So unless someone wants to
help them or to dig into the problem himself, I think we should wait
for the outcome they will find. They will probably push the fix
upstream anyway.
  I initially opened the bug because I was thinking that the bug
would be a regression in the Debian experimental version. But, I was
surprised to find the bug also in the Debian stable version.
I still report it so that we (in Debian) know the bug exists and is
being working on (in case some people look at the BTS when they
see a deadlock).
  If the fix is not too intrusive, perhaps it will be possible to
patch the Debian stable version.

  Regards,
    Vincent