Bug 22622 - [Bug nptl] pthread_once hangs when init routine throws an exception
Summary: [Bug nptl] pthread_once hangs when init routine throws an exception
Status: RESOLVED DUPLICATE of bug 18435
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: unspecified
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-17 08:10 UTC by dzhwinter
Modified: 2018-03-31 12:39 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dzhwinter 2017-12-17 08:10:59 UTC
This is the same bug with 
`Bug 16816 - pthread_once: ABA issue related to the fork generation counter`. In fact, I find some related case in bugzilla and a fix patch. But it failed in tst-once5. 


A minimum reproduce test http://en.cppreference.com/w/cpp/thread/call_once
```
#include <iostream>
#include <thread>
#include <mutex>
 
std::once_flag flag1, flag2;
 
void simple_do_once()
{
    std::call_once(flag1, [](){ std::cout << "Simple example: called once\n"; });
}
 
void may_throw_function(bool do_throw)
{
  if (do_throw) {
    std::cout << "throw: call_once will retry\n"; // 这会出现多于一次
    throw std::exception();
  }
  std::cout << "Didn't throw, call_once will not attempt again\n"; // 保证一次
}
 
void do_once(bool do_throw)
{
  try {
    std::call_once(flag2, may_throw_function, do_throw);
  }
  catch (...) {
  }
}
 
int main()
{
    std::thread st1(simple_do_once);
    std::thread st2(simple_do_once);
    std::thread st3(simple_do_once);
    std::thread st4(simple_do_once);
    st1.join();
    st2.join();
    st3.join();
    st4.join();
 
    std::thread t1(do_once, true);
    std::thread t2(do_once, true);
    std::thread t3(do_once, false);
    std::thread t4(do_once, true);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
}
```

There is test description in the latest develop branch. 
```makefile
# Test expected to fail on most targets (except x86_64) due to bug
# 18435 - pthread_once hangs when init routine throws an exception.
test-xfail-tst-once5 = yes
```
But it failed in x86_64

system: Linux idgsim-gpu-001 4.10.0-40-generic #44~16.04.1-Ubuntu SMP Thu Nov 9 15:37:44 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
glibc: latest branch
commit id 7d38eb38977980efe703eac93645b1af5a5f8a0c
Comment 1 Florian Weimer 2018-02-02 12:01:33 UTC
Bug 18435 is actually architecture-independent, so tracking the issue there is sufficient.

*** This bug has been marked as a duplicate of bug 18435 ***