[Issue 20680] New: core.thread.Thread leaks OS handle when not joined
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Mar 17 11:35:39 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=20680
Issue ID: 20680
Summary: core.thread.Thread leaks OS handle when not joined
Product: D
Version: D2
Hardware: x86_64
OS: Windows
Status: NEW
Severity: normal
Priority: P1
Component: druntime
Assignee: nobody at puremagic.com
Reporter: chalucha at gmail.com
When thread is just created and forgotten about - which is pretty common case
for worker threads (ie with std.concurrency.spawn) it leaks the OS handle (at
least on windows) as CloseHandle is not called in Thread destructor.
My guess is that it's because of these lines:
* https://github.com/dlang/druntime/blob/master/src/core/thread/osthread.d#L304
* https://github.com/dlang/druntime/blob/master/src/core/thread/osthread.d#L697
The last line yields true so CloseHandle isn't called in Thread destructor.
See the test case below and watch what happens with process handles ie in
sysinternals procexp or handles utilities.
```D
import core.thread;
import core.time;
import std;
extern(C) __gshared string[] rt_options = [ "gcopt=parallel:0" ];
void main() {
writeln("press enter to start");
readln();
Thread[10] threads;
foreach (i; 0..10) threads[i] = new Thread(&threadFn).start();
Thread.sleep(1.seconds); // make sure they are done
foreach (i; 0..10) {
// threads[i].join(); # if joined, OS handle is closed as expected
destroy(threads[i]); # enforce destructors to be called
}
writeln("threads disposed, press enter to exit");
readln();
}
void threadFn() { }
```
--
More information about the Digitalmars-d-bugs
mailing list