Bug 1304106

Summary: dlopen deadlocks during thread cancellation
Product: Red Hat Enterprise Linux 6 Reporter: Anit Lulla <alulla>
Component: glibcAssignee: glibc team <glibc-bugzilla>
Status: CLOSED WONTFIX QA Contact: qe-baseos-tools-bugs
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.9CC: ashankar, codonell, fweimer, mnewsome, pfrankli
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-08-29 20:27:21 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
C++ source code that reproduces the bug none

Description Anit Lulla 2016-02-02 22:10:19 UTC
Created attachment 1120575 [details]
C++ source code that reproduces the bug

Description of problem:
dlopen() deadlocks when a prior dlopen() call is cancelled via pthread_cancel()

Version-Release number of selected component (if applicable):
All versions

How reproducible:
Always (see reproducer, which is attached)

#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <dlfcn.h>

using namespace std;

// to reproduce (copy contents to dl_deadlock.cpp)
//
// $ g++ -D FOO_SHARE_LIB -shared -o libfoo.so -fPIC dl_deadlock.cpp
// $ g++ -g dl_deadlock.cpp -ldl -lpthread
// $ ./a.out

#ifdef FOO_SHARE_LIB
class Foo
{
  public:
  Foo() {
    cout << "construct foo" << endl;
    usleep(500000);
    cout << "done construct foo" << endl;
  }
};
Foo g_foo;

#else

extern "C" void *thrfxn_init(void *arg)
{ usleep(500000); return NULL; }

extern "C" void *thrfxn(void *arg)
{
  cout << "before dlopen" << endl;
  void *h = dlopen("./libfoo.so",  RTLD_GLOBAL | RTLD_NOW);
  cout << "after dlopen" << endl;
  if (h);
    dlclose(h);
}

int main()
{
  pthread_t thr;

  // need to call cancel so pthread_cancel_init is called,
  // which calls into libdl (needed since first pthread_cancel waits on dlopen)
  pthread_create(&thr, NULL, thrfxn_init, NULL);
  pthread_cancel(thr);
  pthread_join(thr, NULL);
  
  cout << "before thread create" << endl;
  pthread_create(&thr, NULL, thrfxn, NULL);
  cout << "after thread create" << endl;

  // sleep 5ms
  usleep(5000);

  // cancel thread that is waiting in dlopen
  cout << "before cancel" << endl;
  pthread_cancel(thr);
  cout << "after cancel" << endl;

  // this dlopen will now deadlock
  void *h = dlopen("/lib64/libc.so.6", RTLD_GLOBAL | RTLD_NOW);
  if (h)
    dlclose(h);

  //find_symbol("PA_REPORT_CALC_DISPATCH", PA_REPORT_CALC_DISPATCH);
  //FDSA_FQL_SERVICE, CREATE_FDSA_FQL_SERVICE;

  cout << "before join" << endl;
  pthread_join(thr, NULL);
  cout << "after join" << endl;

  return 0;
}

#endif

Steps to Reproduce:
1. g++ -D FOO_SHARE_LIB -shared -o libfoo.so -fPIC dl_deadlock.cpp
2. g++ -g dl_deadlock.cpp -ldl -lpthread
3. ./a.out

Actual results:
before thread create
after thread create
before dlopen
construct foo
before cancel
after cancel
-- deadlock --

Expected results:
before thread create
after thread create
before dlopen
construct foo
before cancel
after cancel
done construct foo
after dlopen
before join
-- program completion --

Additional info:

Comment 3 Carlos O'Donell 2016-11-22 17:11:56 UTC
I'm moving this to rhel-6.10 for review. We aren't going to look at this for rhel-6.9. There are several cases which we have fixed where the error paths out of dlopen failed to successfully rollback the changes for intermediate loads, but it's likely we haven't done this for all the cases possible. It would be interesting if this reproduces in upstream master.

Comment 5 Carlos O'Donell 2017-08-29 20:27:21 UTC
Constructors are foreign function calls which happen while the dynamic loader is loading the shared library, as such, there are some things which don't work during this particular stage of loading. In this case the foreign function has triggered deferred cancellation and has left the internal dynamic loader locks taken. One fix would be to push a cleanup function in dlopen which releases the locks if the foreign functions call a cancellation point. Unfortunately this kind of fix is invasive for RHEL6, and upstream still doesn't pass this test.

I've opened upstream bug 22034 for this to be tracked upstream.