Subject: | IPC::Cmd patch: "discard_output" |
Date: | Tue, 27 Apr 2010 16:11:09 +0200 |
To: | bug-ipc-cmd [...] rt.cpan.org |
From: | David Morel <david.morel [...] booking.com> |
Hi Chris,
my esteemed colleague Rafael Garcia-Suarez suggested I forward you this patch to IPC::Cmd.
The intent is to discard the internal buffers in run_forked, as I have seen on many occasions
that some cronjobs that I manage trigger OOM conditions when something bad happens
(like 700Mb of warnings, that sort of things). Hope you can review and apply it, thanks!
David Morel
diff -ru IPC-Cmd-0.56 2/lib/IPC/Cmd.pm IPC-Cmd-0.56/lib/IPC/Cmd.pm
--- IPC-Cmd-0.56 2/lib/IPC/Cmd.pm 2010-02-03 15:16:47.000000000 +0100
+++ IPC-Cmd-0.56/lib/IPC/Cmd.pm 2010-04-27 14:43:33.000000000 +0200
@@ -729,17 +729,20 @@
}
while (my $l = <$child_stdout_socket>) {
- $child_stdout .= $l;
- $child_merged .= $l;
+ if (!$opts->{discard_output}) {
+ $child_stdout .= $l;
+ $child_merged .= $l;
+ }
if ($opts->{'stdout_handler'} && ref($opts->{'stdout_handler'}) eq 'CODE') {
$opts->{'stdout_handler'}->($l);
}
}
while (my $l = <$child_stderr_socket>) {
- $child_stderr .= $l;
- $child_merged .= $l;
-
+ if (!$opts->{discard_output}) {
+ $child_stderr .= $l;
+ $child_merged .= $l;
+ }
if ($opts->{'stderr_handler'} && ref($opts->{'stderr_handler'}) eq 'CODE') {
$opts->{'stderr_handler'}->($l);
}
Only in IPC-Cmd-0.56/t: .01_IPC-Cmd.t.swp
diff -ru IPC-Cmd-0.56 2/t/01_IPC-Cmd.t IPC-Cmd-0.56/t/01_IPC-Cmd.t
--- IPC-Cmd-0.56 2/t/01_IPC-Cmd.t 2009-11-09 00:13:19.000000000 +0100
+++ IPC-Cmd-0.56/t/01_IPC-Cmd.t 2010-04-27 14:45:24.000000000 +0200
@@ -171,6 +171,25 @@
ok($r->{'stderr'}, "stderr: " . $r->{'stderr'});
}
+
+# try discarding the out+err
+{
+ my $out;
+ my $cmd = "echo out ; echo err >&2";
+ my $r = run_forked(
+ $cmd,
+ { discard_output => 1,
+ stderr_handler => sub { $out .= shift },
+ stdout_handler => sub { $out .= shift }
+ });
+
+ ok(ref($r) eq 'HASH', "executed: $cmd");
+ ok(!$r->{'stdout'}, "stdout discarded");
+ ok(!$r->{'stderr'}, "stderr discarded");
+ ok($out =~ m/out/, "stdout handled");
+ ok($out =~ m/err/, "stderr handled");
+}
+
__END__
### special call to check that output is interleaved properly
--
<david.morel@booking.com> - <http://www.booking.com/>
office: +33 4 72 83 48 23 - gsm: +33 6 80 38 56 83
Booking.com - 23 rue Jules Vallès - F-69100 Villeurbanne
Message body not shown because it is not plain text.