std::thread::detach() rarely crashes in Parallel.cpp

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 23 16:53:40 PST 2017


Another idea to try to figure out what is going on is to run a test that
normally fails with

rr record --chaos

If that fails you should be able to debug what is failing in a
reproducible way.

Cheers,
Rafael

NAKAMURA Takumi <geek4civic at gmail.com> writes:

> Rafael, I tried a few stories you suggested.
>
> 1) Launch noop(return nullptr) and detach it in main context
> I saw crash in pthread_detach.
>
> 2) Avoid grandchildren. Just launch threads in main context.
> I couldn't happen crash in a million run.
> Note, it's a devil's proof.
>
> I haven't tried cond var, since I think it doesn't make sense.
>
> (2) might be ideal but I am worried it would increase latency to set up
> threads.
> I think we may use naked pthread_create(PTHREAD_CREATE_DETACHED) there,
> instead of std::thread.
>
> I wish we didn't meet the processor's errata. It ought to be impossible.
>
> On Sat, Nov 4, 2017 at 6:50 AM Rafael Avila de Espindola <
> rafael.espindola at gmail.com> wrote:
>
>> And an even simpler thing to try: start all the threads from the main
>> thread.
>>
>> I think the existing code was added because creating threads was slow on
>> windows, but it is not used on windows anymore.
>>
>> Cheers,
>> Rafael
>>
>> Rafael Avila de Espindola <rafael.espindola at gmail.com> writes:
>>
>> > NAKAMURA Takumi <geek4civic at gmail.com> writes:
>> >
>> >> It was discussed in https://reviews.llvm.org/D39038
>> >>
>> >> I suffered when check-lld behaves flaky. For example;
>> >>
>> http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/19673
>> >>
>> >> I have been investigating and I guess it triggered bug or something in
>> >> libpthread or Linux kernel.
>> >> pthread_detach() in std::thread::detach() is crashing with segv.
>> >> Seems pthread (internal struct in Linux nptl) is inaccessible.
>> >> The crash is reproducible w/o std::thread, but with libpthread.
>> >> In my environment, crash can be seen several times per million attempt.
>> >>
>> >> This is my testcase. (in Parallel.cpp)
>> >>
>> >>         pthread_t th;
>> >>         auto r = pthread_create(&th, nullptr, worker_stub, (void*)this);
>> >>         (void)r;
>> >>         assert(r == 0);
>> >>         auto p = reinterpret_cast<volatile int*>(th);
>> >>         for (int j = 0; j < 1000 * 1000; ++j)
>> >>           *p;
>> >>         pthread_detach(th);
>> >
>> >
>> > Can you try if you get a crash with just
>> >
>> > ------------------------------
>> > #include <pthread.h>
>> > #include <assert.h>
>> > static void *worker_stub(void *) {
>> >   return nullptr;
>> > }
>> > int main() {
>> >   pthread_t th;
>> >  auto r = pthread_create(&th, nullptr, worker_stub, (void*)42);
>> >  (void)r;
>> >  assert(r == 0);
>> >  auto p = reinterpret_cast<volatile int*>(th);
>> >  for (int j = 0; j < 1000 * 1000; ++j)
>> >    *p;
>> >  pthread_detach(th);
>> > }
>> > ------------------------------
>> >
>> > Cheers,
>> > Rafael
>>


More information about the llvm-commits mailing list