Bug 661676

Summary: accessing thread local storage blocks forever when using dlopen
Product: [Fedora] Fedora Reporter: Maxim Egorushkin <maxim.yegorushkin>
Component: glibcAssignee: Jeff Law <law>
Status: CLOSED WONTFIX QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: medium Docs Contact:
Priority: low    
Version: 14CC: fweimer, jakub, schwab
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-01-26 17:48:40 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Maxim Egorushkin 2010-12-09 11:22:21 UTC
Description of problem:
A new thread created by a shared library initialization routine blocks forever accessing thread local storage when the shared library is loaded during run-time using dlopen().

Version-Release number of selected component (if applicable):
$ rpm -q glibc
glibc-2.12.1-4.x86_64

How reproducible:
Always.

Steps to Reproduce:
$ cat shared.cc
#include <cassert>
#include <pthread.h>
#include <stdio.h>

namespace {

__thread void* some_value;

void* thread(void*)
{
    printf("thread enter\n");
    void* value = some_value; // hangs here forever
    printf("thread leave\n");
    return value;
}

struct X
{
    X()
    {
        pthread_t thread_id;
        int r = pthread_create(&thread_id, NULL, thread, NULL);
        assert(!r);
        void* value;
        r = pthread_join(thread_id, &value);
        assert(!r);
    }
} x;

}

$ g++ -Wall -shared -fpic -pthread -g -o shared.so shared.cc

$ cat loader.cc
#include <stdio.h>
#include <dlfcn.h>

char const shared_path[] = "./shared.so";

int main()
{
    printf("loading %s\n", shared_path);
    void* h = dlopen(shared_path, RTLD_NOW | RTLD_GLOBAL);
    printf("%s loaded at %p\n", shared_path, h);
    dlclose(h);
    printf("%s unloaded\n", shared_path);
}

$ g++ -Wall -pthread -ldl -g -o loader loader.cc

  
Actual results:
$ ./loader
loading ./shared.so
thread enter
(the above hangs forever)
  C-c C-c

Expected results:
$ ./loader
loading ./shared.so
thread enter
thread leave
./shared.so loaded at 0x7ff97355eb18
./shared.so unloaded

Additional info:
When the executable links the shared library explicitly at link time, i.e.:

$ g++ -Wall -pthread -ldl -g -o loader -l:./shared.so loader.cc 

it works as expected.

Comment 1 Jakub Jelinek 2010-12-09 11:33:07 UTC
Don't do something so silly then.
The dynamic linker has to hold locks guarding some of its internal state during dlopen until the dlopening finishes, so if you spawn another thread and try to call stuff that needs that lock, it will obviously block until dlopen finishes and the lock is released.  But the initial thread waits on this thread, so it is an obvious deadlock.

Comment 2 Maxim Egorushkin 2010-12-09 11:45:08 UTC
Thanks for sharing your useful opinion.

By the time glibc calls shared library constructors it must have already set up thread local storage. It looks like one big mutex is used for both dlopen() and tls setup. The mutex protecting tls must be released before calling shared library constructors.

Comment 3 Bug Zapper 2011-05-30 12:58:06 UTC
This message is a reminder that Fedora 13 is nearing its end of life.
Approximately 30 (thirty) days from now Fedora will stop maintaining
and issuing updates for Fedora 13.  It is Fedora's policy to close all
bug reports from releases that are no longer maintained.  At that time
this bug will be closed as WONTFIX if it remains open with a Fedora 
'version' of '13'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version prior to Fedora 13's end of life.

Bug Reporter: Thank you for reporting this issue and we are sorry that 
we may not be able to fix it before Fedora 13 is end of life.  If you 
would still like to see this bug fixed and are able to reproduce it 
against a later version of Fedora please change the 'version' of this 
bug to the applicable version.  If you are unable to change the version, 
please add a comment here and someone will do it for you.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events.  Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.

The process we are following is described here: 
http://fedoraproject.org/wiki/BugZappers/HouseKeeping

Comment 4 Maxim Egorushkin 2011-05-30 13:10:17 UTC
Still broken with Fedora 14 and glibc-2.13-1.x86_64

Comment 5 Fedora Admin XMLRPC Client 2011-11-14 19:44:09 UTC
This package has changed ownership in the Fedora Package Database.  Reassigning to the new owner of this component.

Comment 6 Maxim Egorushkin 2011-11-15 09:05:43 UTC
I know it is a tough one. Are there any plans to fix it?

Comment 7 Jeff Law 2012-01-26 17:48:40 UTC
No plans to fix this.

Comment 8 Maxim Egorushkin 2012-01-26 19:15:08 UTC
Why not? Any comment?

Comment 9 Jeff Law 2012-01-26 19:46:39 UTC
Nothing beyond what Jakub has already stated in c#1.