Thread overview
[Issue 18063] thread_attachThis returns dangling pointer
Dec 11, 2017
Ali Cehreli
Dec 12, 2017
Ali Cehreli
Mar 01, 2020
Dlang Bot
Oct 05, 2021
Ali Cehreli
Dec 17, 2022
Iain Buclaw
December 11, 2017
https://issues.dlang.org/show_bug.cgi?id=18063

Ali Cehreli <acehreli@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #1 from Ali Cehreli <acehreli@yahoo.com> ---
https://github.com/dlang/druntime/pull/1986

--
December 12, 2017
https://issues.dlang.org/show_bug.cgi?id=18063

--- Comment #2 from Ali Cehreli <acehreli@yahoo.com> ---
New PR:

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

--
March 01, 2020
https://issues.dlang.org/show_bug.cgi?id=18063

--- Comment #3 from Dlang Bot <dlang-bot@dlang.rocks> ---
@MoonlightSentinel created dlang/druntime pull request #2966 "Revive 1989: Thread detach stability" fixing this issue:

- Issue 18063 - Do not pthread_detach non-D threads

- Fix issue 18063 - thread_attachThis returns dangling pointer

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

--
October 05, 2021
https://issues.dlang.org/show_bug.cgi?id=18063

Ali Cehreli <acehreli@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=22358

--
October 05, 2021
https://issues.dlang.org/show_bug.cgi?id=18063

thomas.bockman@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thomas.bockman@gmail.com

--- Comment #4 from thomas.bockman@gmail.com ---
It appears to be a race condition between `thread_attachThis` and `GC.collect`. Serializing them prevents the crash:

//////////////////////////////////////////////////
import core.sys.posix.pthread;
import core.memory;
import core.sync.mutex;
import core.thread;
import std.stdio;

enum allocCount = 1000;
enum allocSize = 1000;
enum threadCount = 100;
enum collectCount = 1000;

shared Mutex serialize;
shared static this() {
    serialize = new shared Mutex;
}

extern(C) void* foo(void*)
{
    serialize.lock_nothrow();
    thread_attachThis();
    serialize.unlock_nothrow();

    scope(exit)
        thread_detachThis();

    foreach(_; 0..allocCount) {
        new int;
        new int[allocSize];
    }

    writeln(`foo done`);
    return null;
}

void main()
{
    pthread_t thread;

    foreach(_; 0..threadCount) {
        auto status = pthread_create(&thread, null, &foo, null);
        assert(status == 0);

        foreach(i; 0..collectCount) {
            serialize.lock_nothrow();
            GC.collect();
            serialize.unlock_nothrow();
        }

        pthread_join(thread, null);
    }

    writeln(`main done`);
}
//////////////////////////////////////////////////

--
October 05, 2021
https://issues.dlang.org/show_bug.cgi?id=18063

--- Comment #5 from thomas.bockman@gmail.com ---
(In reply to thomas.bockman from comment #4)
> It appears to be a race condition between `thread_attachThis` and `GC.collect`. Serializing them prevents the crash:

Oops, I meant to submit that to bug 22358: https://issues.dlang.org/show_bug.cgi?id=22358

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=18063

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2

--