book/System-Command

Unclosed System::Command object can obscure perl exit status

mmcclimon opened this issue · 4 comments

While I recognize that this is a bit of a PEBKAC, I spent a long time tracking down this bug in production, so I thought I'd mention it here. We had some code that was instantiating a System::Command object on the fly and immediately getting its stdout. The code just after this would die, but the program would exit 0 because the command run by System::Command exited 0. This small program reproduces the problem:

use strict;
use warnings;
use System::Command;

my $out = System::Command->new("ls")->stdout;
my $line = <$out>;

die "uh oh, the zombies are coming";

I had a look at seeing if I could fix this myself, thinking it was a matter of localizing $? somewhere, but then I saw all the comments in _reap() and got scared. 😛

I believe the problem is in you're DESTROY method. It should localize $! and/or $? I believe

I should also mention that the fix for this was straightforward: we just changed the code in question to

my $cmd = System::Command->new("ls");
my $out = $cmd->stdout;
my $line = <$out>;
$cmd->close;

On second thought, the fix was as easy as I thought, I was just looking in the wrong place: see #28. Thanks!

book commented

Thanks for the report, the detailed explanation and the fix!