eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:

Further to Perl threads test intermittently hangs on Windows, the simple program below appears to be leaking handles on Windows. That is, at the end of the run, Windows Task Manager (and sysinternals pslist command) both show the perl process has 1017 open handles. It seems the underlying Win32 thread handles are not being closed. Is there a way to force them to be closed? (I am using Active State Perl 5.8.3 build 809 on Windows XP).

#!/usr/bin/perl -w use strict; use threads; sub do_one_thread { warn "in kid\n" } sub do_thread { my $t = threads->new(\&do_one_thread); warn "parent $$: waiting for join\n"; $t->join(); warn "parent $$: thread exited\n"; } for my $i (1..1000) { warn "i=$i:---\n"; do_thread(); } warn "sleeping for 60 seconds (check handle count in Task Manager)\n"; sleep(60);

At the end of the run, the sysinternals pslist command reports 1017 open handles:

Name Pid Pri Thd Hnd Mem User Time Kernel Time Ela +psed Time perl 6560 8 1 1017 3232 0:00:15.203 0:00:00.203 0: +00:18.390

Replies are listed 'Best First'.
Re: Perl threads test on Windows: how to close thread handles?
by BrowserUk (Patriarch) on Jun 04, 2004 at 03:49 UTC

    Sorry, I spoke too soon. They are indeed thread handles (at least 1000 of them are). And there is a way of avoiding the leak. Detaching the threads rather than joining them prevents it.

    #!/usr/bin/perl -w use strict; use threads; sub do_one_thread { warn "in kid\n"; return 1; } sub do_thread { my $t = threads->new(\&do_one_thread); $t->detach; } for my $i (1..1000) { warn "i=$i:---\n"; do_thread(); } warn "sleeping for 60 seconds (check handle count in Task Manager)\n"; <STDIN>;

    Of course thats no good if you need the return values from the threads, and so it is still a bug.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Perl threads test on Windows: how to close thread handles?
by bageler (Hermit) on Jun 04, 2004 at 02:48 UTC
      # Loop through all the threads foreach $thr (threads->list) { $thr->yield; }

      I don't get it. How will calling yield at any point in my test program cause the underlying Win32 thread handle to be closed?

      To clarify, I think my test program above demonstrates a perl bug, but, being very new to threads, I need confirmation that I'm not being silly before perl-bugging it.

Re: Perl threads test on Windows: how to close thread handles?
by BrowserUk (Patriarch) on Jun 04, 2004 at 03:30 UTC

    The handles may not be thread handles. They could be event object handles or memory object handles or just about any other type of system handle.

    I also see the handle leak. I agree that there is a bug here.

    Whatever type of handle they are, without access to them, there is nothing you can do about closing them from user level code.

    I think that raising a perlbug incorporating your testcase is called for. Of course, if you could track down what type of handle is leaking first (not easy!), then that would be even better:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Of course, if you could track down what type of handle is leaking first (not easy!)

      Actually, it is easy. ;-) The sysinternals nthandle utility will tell you.

      perl.exe pid: 3980 4: Thread perl.exe(3980): 5816 8: Thread perl.exe(3980): 3388 c: Thread perl.exe(3980): 3604 10: Thread perl.exe(3980): 4528 14: Thread perl.exe(3980): 1340 18: Thread perl.exe(3980): 3316 ...

        Yeah! Once I found the right place to look, http://www.smidgeonsoft.coms PEBrowse showed me the same thing.

        Process Information: Kernel Objects: 0004 - Key \KernelObjects\CritSecOutOfMemoryEvent (0xE10085F0) 0008 - Directory \KnownDlls (0xE14BC318) 000C - File C:\Perl\test (0x81CC1568) 0010 - Key HKLM (0xE1138618) 0014 - Directory \Windows (0xE13B7D10) 0018 - Port (0xE1B25838) 001C - Section (0xE14DAAF0) 0020 - Event (0x81EC2FF0) 0024 - WindowStation \Windows\WindowStations\WinSta0 (0x82124AD8) 0028 - Event (0x821446D0) 002C - WindowStation \Windows\WindowStations\WinSta0 (0x82124AD8) 0030 - Desktop (0x82203278) 0034 - Key HKLM\SYSTEM\ControlSet001\Control\Nls\Locale (0xE1C85688) 0038 - Key HKLM\SYSTEM\ControlSet001\Control\Nls\Locale\Alternate Sort +s (0xE1B0FBB0) 003C - Key HKLM\SYSTEM\ControlSet001\Control\Nls\Language Groups (0xE1 +A886C8) 0040 - Key HKCU (0xE214BCE8) 0044 - Thread (0x818FD208) 0048 - Thread (0x81CC3CA8) 004C - Thread (0x81851DA8) 0050 - Thread (0x81D1CAF8) ...

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail