digitalmars.D.bugs - [Issue 20680] New: core.thread.Thread leaks OS handle when not joined
- d-bugmail puremagic.com (43/43) Mar 17 2020 https://issues.dlang.org/show_bug.cgi?id=20680
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 puremagic.com Reporter: chalucha 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) { } writeln("threads disposed, press enter to exit"); readln(); } void threadFn() { } ``` --
Mar 17 2020