dagolden / Capture-Tiny

(Perl) Capture STDOUT and STDERR from Perl, XS or external programs
http://search.cpan.org/dist/Capture-Tiny/
39 stars 19 forks source link

Timeout detection is not working as expected #28

Closed hklausing closed 9 years ago

hklausing commented 9 years ago

I tried to used the timeout function to stop capturing with Perl v5.18.2. (Capture::Tiny v0.27). But my tests failed for this feature use with Linux Mint 17. Program execution is not stopped as expected if the defined timeout is over. The following code shows my tests for the two timeout variables.

use strict;
use warnings;
use Capture::Tiny qw(:all);
#
#
my $capute_tiny_timeout = $Capture::Tiny::TIMEOUT;  # get package default value
my ($out, $err, @result);
#
#
#
print "\n\ncheck timeout behaviour with timeout variable!!!\n";
$Capture::Tiny::TIMEOUT = 3;    # lower value than command execution !
($out, $err, @result) = eval {
    tee {system("echo '-start-'; sleep 5; echo '-end-' 1>&2");};
};
print "EVAL_ERROR (Timeout)!!\n" if $@;
print "Captured STDOUT was: '$out'\n";
print "Captured STDERR was: '$err'\n";
print "Return Code        : '" . join(', ', @result) . "'\n";
#
#
#
print "\n\ncheck timeout behaviour with environment variable!!!\n";
$Capture::Tiny::TIMEOUT = $capute_tiny_timeout;    # default value
$ENV{'PERL_CAPTURE_TINY_TIMEOUT'} = 3;             # lower value than command execution !
($out, $err, @result) = eval {
    tee {system("echo '-start-'; sleep 5; echo '-end-' 1>&2");};
};
print "EVAL_ERROR (Timeout)!!\n" if $@;
print "Captured STDOUT was: '$out'\n";
print "Captured STDERR was: '$err'\n";
print "Return Code        : '" . join(', ', @result) . "'\n";
#
#
#
print "\n\ncheck timeout behaviour with both timeout variables!!!\n";
$Capture::Tiny::TIMEOUT = 3;                                             # lower value than command execution !
$ENV{'PERL_CAPTURE_TINY_TIMEOUT'} = 3;                                   # lower value than command execution !
($out, $err, @result) = eval {
    tee {system("echo '-start-'; sleep 5; echo '-end-' 1>&2");};
};
print "EVAL_ERROR (Timeout)!!\n" if $@;
print "Captured STDOUT was: '$out'\n";
print "Captured STDERR was: '$err'\n";
print "Return Code        : '" . join(', ', @result) . "'\n";
dagolden commented 9 years ago

I think you've misunderstood what that variable does, which is my own fault for not having clearer documentation. It doesn't timeout the capture itself, it times out waiting for Capture::Tiny's own internal tee subprocesses to be ready to proceed.

ttyS4 commented 9 years ago

Do you plan to add such a feature? It is pretty common (for me) to use that when running external commands.

dagolden commented 9 years ago

I don't think it fits for this module, because it's not just about capturing from system commands. If you're on *nix, CPAN has System::Timeout, which looks like does something like what you want.