Thread overview
[Issue 20680] core.thread.Thread leaks OS handle when not joined
May 25, 2020
Sebastiaan Koppe
Nov 17, 2021
Tomáš Chaloupka
Dec 17, 2022
Iain Buclaw
May 25, 2020
https://issues.dlang.org/show_bug.cgi?id=20680

Sebastiaan Koppe <mail@skoppe.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mail@skoppe.eu
                 OS|Windows                     |All

--- Comment #1 from Sebastiaan Koppe <mail@skoppe.eu> ---
This is the case as well under Linux.

With GDB attached I have put a breakpoint at the core.thread.osthread.Thread's destructor and confirmed it doesn't get run (I spawned 100k threads and let them run till finish).

As a workaround I am calling `pthread_detach(pthread_self);` right before the thread terminates. This is not a good solution though since the Thread's destructor (if it did ran), would also call pthread_detach, which results in undefined behaviour.

Better would be to do a thread_detachThis as well, since that will cause a different branch in the Thread's destructor and avoid the call to pthread_detach.

Sadly, using thread_detachThis and the pthread_detach I get spurious crashes in std.concurrency's tls module teardown (sometimes).

--
November 17, 2021
https://issues.dlang.org/show_bug.cgi?id=20680

Steven Schveighoffer <schveiguy@gmail.com> changed:

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

--- Comment #2 from Steven Schveighoffer <schveiguy@gmail.com> ---
When linking to github, use the `y` command to change the link to a hash-based link, otherwise your links get stale.

For reference, here are the two links:

https://github.com/dlang/druntime/blob/19ef6336d1940e770d696dc71f7605eb3f618a85/src/core/thread/osthread.d#L304 https://github.com/dlang/druntime/blob/19ef6336d1940e770d696dc71f7605eb3f618a85/src/core/thread/osthread.d#L697

Can you test again? the code has changed quite a bit since then, the destructor looks rewritten.

--
November 17, 2021
https://issues.dlang.org/show_bug.cgi?id=20680

--- Comment #3 from Tomáš Chaloupka <chalucha@gmail.com> ---
(In reply to Steven Schveighoffer from comment #2)
> When linking to github, use the `y` command to change the link to a hash-based link, otherwise your links get stale.
> 
> For reference, here are the two links:
> 
> https://github.com/dlang/druntime/blob/ 19ef6336d1940e770d696dc71f7605eb3f618a85/src/core/thread/osthread.d#L304 https://github.com/dlang/druntime/blob/ 19ef6336d1940e770d696dc71f7605eb3f618a85/src/core/thread/osthread.d#L697
> 
> Can you test again? the code has changed quite a bit since then, the destructor looks rewritten.

Yes I can still confirm it on both windows and linux.
Tried with dmd-2.098.0 on windows and build from current master on linux.

Added some logging to the Thread destructor with the output:

```D
press enter to start

~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
~Thread()
threads disposed, press enter to exit

~Thread()
passed
detach
```

So currently, it won't pass this https://github.com/dlang/druntime/blob/abd43eb4e760667fc4b1ebe1fe07d29340265596/src/core/thread/osthread.d#L292

And sorry for not permanent links, younger me hasn't known all he knows now ;-)

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

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

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

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

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

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

--