]> Sergey Matveev's repositories - public-inbox.git/commitdiff
reduce calls to close unless error checks are needed
authorEric Wong <e@80x24.org>
Sun, 28 Feb 2016 11:28:33 +0000 (11:28 +0000)
committerEric Wong <e@80x24.org>
Sun, 28 Feb 2016 11:30:33 +0000 (11:30 +0000)
We can rely on timely auto-destruction based on reference
counting; reducing the chance of redundant close(2) calls
which may hit the wront FD.

We do care about certain close calls (e.g. writing to a buffered
IO handle) if we require error-checking for write-integrity.  In
other cases, let things go out-of-scope so it can be freed
automatically after use.

lib/PublicInbox/Config.pm
lib/PublicInbox/Feed.pm
lib/PublicInbox/Git.pm
lib/PublicInbox/ProcessPipe.pm
lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/Spawn.pm

index 844f666ef6a484f759cc445ba7047359823a713f..b51163883317fea824a4b7492f1c91149747054d 100644 (file)
@@ -101,7 +101,6 @@ sub try_cat {
        if (open(my $fh, '<', $path)) {
                local $/;
                $rv = <$fh>;
-               close $fh;
        }
        $rv;
 }
index a5828a8e16bf9f475c1897b60f7cd4279e064c24..54cbf23c545a31afa7b52fbfcd8bb3acadfc07b9 100644 (file)
@@ -255,7 +255,6 @@ sub each_recent_blob {
                }
        }
 
-       close $log; # we may EPIPE here
        # for pagination
        ($first_commit, $last_commit);
 }
@@ -269,7 +268,6 @@ sub get_feedopts {
        my %rv;
        if (open my $fh, '<', "$ctx->{git_dir}/description") {
                chomp($rv{description} = <$fh>);
-               close $fh;
        } else {
                $rv{description} = '($GIT_DIR/description missing)';
        }
index 57d17d3382aaef9087eced1d10c34b90b5887f3f..0f92dd9a82f6e70bc5de58661fbfab0bce76bd47 100644 (file)
@@ -29,8 +29,6 @@ sub _bidi_pipe {
        my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
        my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
        $self->{$pid} = spawn(\@cmd, undef, $redir);
-       close $out_r or fail($self, "close failed: $!");
-       close $in_w or fail($self, "close failed: $!");
        $out_w->autoflush(1);
        $self->{$out} = $out_w;
        $self->{$in} = $in_r;
@@ -100,13 +98,9 @@ sub check {
 
 sub _destroy {
        my ($self, $in, $out, $pid) = @_;
-       my $p = $self->{$pid} or return;
-       $self->{$pid} = undef;
+       my $p = delete $self->{$pid} or return;
        foreach my $f ($in, $out) {
-               my $fh = $self->{$f};
-               defined $fh or next;
-               close $fh;
-               $self->{$f} = undef;
+               delete $self->{$f};
        }
        waitpid $p, 0;
 }
index eade524caada14ae99d4815822ec9738bef1f7ce..e088c1053f0b08877a277f281f3192925bac791d 100644 (file)
@@ -15,13 +15,12 @@ sub READ { sysread($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
 
 sub READLINE { readline($_[0]->{fh}) }
 
-sub CLOSE { close($_[0]->{fh}) }
+sub CLOSE { delete($_[0]->{fh}) }
 
 sub FILENO { fileno($_[0]->{fh}) }
 
 sub DESTROY {
-       my $fh = delete($_[0]->{fh});
-       close $fh if $fh;
+       delete($_[0]->{fh});
        waitpid($_[0]->{pid}, 0);
 }
 
index 1d0d926f28164a29a904643bdc0cacdc7218fc22..415decd16ee77ea97819275f24cd933fac34c823 100644 (file)
@@ -348,7 +348,6 @@ sub rlog {
                        $latest = $1;
                }
        }
-       close $log;
        $latest;
 }
 
@@ -446,7 +445,6 @@ sub _read_git_config_perm {
        my @cmd = qw(config core.sharedRepository);
        my $fh = PublicInbox::Git->new($self->{git_dir})->popen(@cmd);
        my $perm = <$fh>;
-       close $fh;
        chomp $perm if defined $perm;
        $perm;
 }
index 51ad2692b82e6ff3d6ec49abfda6f02d4d801ea3..02c5446f6dcf0f965471498656a60fb9a720ad07 100644 (file)
@@ -171,7 +171,6 @@ sub popen_rd {
        $r->blocking($blocking) if defined $blocking;
        $opts->{1} = fileno($w);
        my $pid = spawn($cmd, $env, $opts);
-       close $w;
        return ($r, $pid) if wantarray;
        my $ret = gensym;
        tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;