in reply to Re^2: Having Win32 Perl crashs using ithreads
in thread Having Win32 Perl crashs using ithreads

Hello dchidelf,

The following is helpful if wanting the ProcOpen module to free up the handles automatically. Basically, this becomes important when omitting the closing of $out and $err handles at the application level.

Save the $out and $err handles after construction inside the procopen method.

tie *OFH, "IOQueue", $obuff, $self; tie *EFH, "IOQueue", $ebuff, $self; $$out = $self->{'outh'} = \*OFH; $$err = $self->{'errh'} = \*EFH;

Inside the close method, *untie* the $out and $err handles prior to joining the threads associated with the handles.

if (! defined $self->{'rc'}) { ... my $inh = $self->{'inh'}; my $outh = $self->{'outh'}; my $errh = $self->{'errh'}; my $pid = $self->{'pid'}; print "About to close\n"; ... $self->{'rc'} = $rc; untie *{$outh}; untie *{$errh}; print "Join Threads $othr $ethr\n"; $othr->join(); print "part done\n"; print "Join Threads $othr $ethr\n"; $ethr->join(); print "Threads joined\n"; ...

Well, that works just as well. IMHO, leave the *CLOSE* method inside IOQueue in the event one chooses to close the handles at the application level.

The above changes makes ProcOpen resilient to applications omitting the closing of $out and $err handles.

Regards, Mario

Replies are listed 'Best First'.
Re^4: Having Win32 Perl crashs using ithreads
by dchidelf (Novice) on Feb 19, 2016 at 18:06 UTC
    Thanks I added calls to my IOQueue close functions in the ProcOpen::close and also untied the variables as you suggested.

    Since I got around the crashes I have also replaced the temp files used for the process STDOUT/STDERR with pipes, so that simplifies some of the processing (don't need to tail the files, can just read from the pipe until it closes). All seems well with it.

    Here is the "final" module if anyone is interested.

      I like your update with the CLOSE method. If by chance close is called twice at the application level, this will *not* raise an error. Nice ++

      sub CLOSE { $_[0] = {}; bless $_[0]; }