Thread overview
[Issue 17957] D shared library throws asserts when called from C detached pthread but not terminated with dlclose
Nov 02, 2017
ponce
Nov 02, 2017
Nicolas F.
Nov 12, 2017
Nicolas F.
Dec 08, 2021
Stanislav Blinov
Dec 17, 2022
Iain Buclaw
November 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17957

ponce <aliloko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aliloko@gmail.com

--- Comment #1 from ponce <aliloko@gmail.com> ---
A major problem I see is that you use pthread_detach which you are not supposed to.

People are talking about "attachment" in the context of the D runtime which maintains a list of "attached" threads (thread_attachThis/thread_detachThis), nothing to do with pthreads.

In your case, you don't want the runtime enabled so don't have to worry about this attachment to the D runtime anyway.

--
November 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17957

--- Comment #2 from Nicolas F. <ajidala@gmail.com> ---
(In reply to ponce from comment #1)
> A major problem I see is that you use pthread_detach which you are not supposed to.
> 
> People are talking about "attachment" in the context of the D runtime which maintains a list of "attached" threads (thread_attachThis/thread_detachThis), nothing to do with pthreads.
> 
> In your case, you don't want the runtime enabled so don't have to worry about this attachment to the D runtime anyway.

This is just a minimal test application which shows exactly the behaviour that is causing an issue. The place I ran onto this in the real world is when trying to write a SO plugin for mpv. mpv spawns SO plugins in a detached thread, and I have no control over this. Ideally I'd use the runtime within my thread in that case so I can get things like the GC, but this issue happens both with the runtime initialised and without.

There really is no reason for D to segfault or throw asserts at all if the parent application doesn't call dlclose() before shutting down.

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

--- Comment #3 from Nicolas F. <ajidala@gmail.com> ---
fwiw, you can get rid of the naughty pthread_join call which would result in undefined behaviour for detached threads by replacing it with a simple sleep(3), which will result in the same segfault inside the D runtime, effectively proving that a completely valid C program can call into completely valid D code and cause a crash because of the D runtime.

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=17957

Stanislav Blinov <stanislav.blinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |stanislav.blinov@gmail.com

--- Comment #4 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
With 2.098, I observer no mentioned asserts. However, this does lead to an abort IFF the shared library isn't dlclosed.

main()
run_lib()
can confirm this is fun
main() done
Aborting from src/rt/sections_elf_shared.d(523) _handleToDSO not in sync with
_loadedDSOs.[1]    75008 abort (core dumped)  ./main

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
December 07
https://issues.dlang.org/show_bug.cgi?id=17957

--- Comment #5 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17161

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--