From: Michal Nazarewicz Date: Mon, 11 Feb 2013 16:23:37 GMT Subject: [PATCHv3 3/5] Git.pm: allow pipes to be closed prior to calling command_close_bidi_pipe Message-ID: In-Reply-To: From: Michal Nazarewicz The command_close_bidi_pipe() function will insist on closing both input and output pipes returned by command_bidi_pipe(). With this change it is possible to close one of the pipes in advance and pass undef as an argument. Signed-off-by: Michal Nazarewicz --- perl/Git.pm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/perl/Git.pm b/perl/Git.pm index 11f310a..9dded54 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -426,13 +426,26 @@ Note that you should not rely on whatever actually is in C; currently it is simply the command name but in future the context might have more complicated structure. +C and C may be C if they have been closed prior to +calling this function. This may be useful in a query-response type of +commands where caller first writes a query and later reads response, eg: + + my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check'); + print $out "000000000\n"; + close $out; + while (<$in>) { ... } + $r->command_close_bidi_pipe($pid, $in, undef, $ctx); + +This idiom may prevent potential dead locks caused by data sent to the output +pipe not being flushed and thus not reaching the executed command. + =cut sub command_close_bidi_pipe { local $?; my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_); foreach my $fh ($in, $out) { - unless (close $fh) { + if (defined $fh && !close $fh) { if ($!) { carp "error closing pipe: $!"; } elsif ($? >> 8) { -- 1.8.1.3.571.g3f8bed7.dirty