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

NAKAMURA Takumi via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 7 05:51:44 PST 2017


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171107/7f5b5237/attachment.html>


More information about the llvm-commits mailing list