D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21784 - joining a detached thread results in segfault.
Summary: joining a detached thread results in segfault.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-03-30 10:04 UTC by omerfirmak
Modified: 2021-04-18 14:24 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description omerfirmak 2021-03-30 10:04:14 UTC
import core.thread;
import core.sync.semaphore;

__gshared Semaphore sem;

void thread_main ()
{
    sem.notify();
}

void main()
{
    auto th = new Thread(&thread_main);
    sem = new Semaphore();
    th.start();
    sem.wait();
    while (th.isRunning()) {}
    destroy(th); // force detach
    th.join();
}

Above piece of code will segfault since the dtor of the Thread will reset m_addr and Thread.join() will pass that null value to phread_join() without checking if it is valid.
Comment 1 Dlang Bot 2021-03-30 10:05:14 UTC
@omerfirmak updated dlang/druntime pull request #3418 "Allow detached threads to be joined" fixing this issue:

- Fix Issue 21784: Allow detached threads to be joined
  
  This is useful when a thread terminates before the
  caller is able to join().

https://github.com/dlang/druntime/pull/3418
Comment 2 Dlang Bot 2021-03-30 10:53:51 UTC
dlang/druntime pull request #3418 "Fix Issue 21784: Allow detached threads to be joined" was merged into stable:

- b301faf42df1bfab60ada38473dd46000c1fdf66 by Ömer Faruk Irmak:
  Fix Issue 21784: Allow detached threads to be joined
  
  This is useful when a thread terminates before the
  caller is able to join().

https://github.com/dlang/druntime/pull/3418
Comment 3 Dlang Bot 2021-04-18 14:24:33 UTC
dlang/druntime pull request #3434 "merge stable" was merged into master:

- c5eb299128d7614e18ee2e2366005843dbeb0c6b by Ömer Faruk Irmak:
  Fix Issue 21784: Allow detached threads to be joined
  
  This is useful when a thread terminates before the
  caller is able to join().

https://github.com/dlang/druntime/pull/3434