Skip Menu |

This queue is for tickets about the IPC-Cmd CPAN distribution.

Report information
The Basics
Id: 56973
Status: resolved
Worked: 15 min
Priority: 0/
Queue: IPC-Cmd

People
Owner: BINGOS [...] cpan.org
Requestors: david.morel [...] booking.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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
Download smime.p7s
application/pkcs7-signature 4.5k

Message body not shown because it is not plain text.

The doc for run_forked needs to be adjusted too. The new option can be described like this << =item C<discard_output> Discards the buffering of the standard output and standard errors for return by run_forked(). With this option you have to use the std*_handlers to read what the command outputs. Useful for commands that send a lot of output. Show quoted text
>>
And later, in the return values section : << =item C<stdout> Holds the standard output of the executed command (or empty string if there were no stdout output or if discard_output was used; it's always defined!) =item C<stderr> Holds the standard error of the executed command (or empty string if there were no stderr output or if discard_output was used; it's always defined!) Show quoted text
>>
Thanks, this was applied and released. Changes for 0.58 Thu Apr 29 20:49:07 BST 2010 ================================================= * Applied patch from Petya Kohts, RT #50398, which adds 'terminate_on_parent_sudden_death' option to run_forked(). * Applied patches from David Morel RT #56973, which add 'discard_output' option to run_forked(). * Added documentation as suggested by Rafa<C3><AB>l Garcia-Suarez in RT #56973 Many thanks.