user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 05/14] treewide: use ->close to call ProcessIO->CLOSE
Date: Thu,  2 Nov 2023 09:35:30 +0000	[thread overview]
Message-ID: <20231102093539.2067470-6-e@80x24.org> (raw)
In-Reply-To: <20231102093539.2067470-1-e@80x24.org>

This will open the door for us to drop `tie' usage from
ProcessIO completely in favor of OO method dispatch.  While
OO method dispatches (e.g. `$fh->close') are slower than normal
subroutine calls, it hardly matters in this case since process
teardown is a fairly rare operation and we continue to use
`close($fh)' for Maildir writes.
---
 lib/PublicInbox/Config.pm        |  2 +-
 lib/PublicInbox/GitCredential.pm |  4 ++--
 lib/PublicInbox/Import.pm        |  6 +++---
 lib/PublicInbox/LEI.pm           |  8 ++++----
 lib/PublicInbox/LeiInput.pm      |  2 +-
 lib/PublicInbox/LeiRediff.pm     |  2 +-
 lib/PublicInbox/LeiViewText.pm   |  2 +-
 lib/PublicInbox/LeiXSearch.pm    |  8 ++++----
 lib/PublicInbox/MboxReader.pm    |  4 ++--
 lib/PublicInbox/SearchIdx.pm     |  2 +-
 lib/PublicInbox/Spawn.pm         |  2 +-
 lib/PublicInbox/V2Writable.pm    |  2 +-
 script/public-inbox-convert      |  4 ++--
 t/httpd-corner.t                 |  2 +-
 t/lei-q-kw.t                     |  4 ++--
 t/spawn.t                        | 10 +++++-----
 xt/git_async_cmp.t               |  4 ++--
 xt/httpd-async-stream.t          |  2 +-
 18 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 0a572103..01cb536d 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -192,7 +192,7 @@ sub git_config_dump {
 	push(@cmd, '-f', $file) if !@opt_c && defined($file);
 	my $fh = popen_rd(\@cmd, \%env, $opt);
 	my $rv = config_fh_parse($fh, "\0", "\n");
-	close $fh or die "@cmd failed: \$?=$?\n";
+	$fh->close or die "@cmd failed: \$?=$?\n";
 	$rv->{-opt_c} = \@opt_c if @opt_c; # for ->urlmatch
 	$rv->{-f} = $file;
 	bless $rv, $class;
diff --git a/lib/PublicInbox/GitCredential.pm b/lib/PublicInbox/GitCredential.pm
index 10114a10..a4444e2c 100644
--- a/lib/PublicInbox/GitCredential.pm
+++ b/lib/PublicInbox/GitCredential.pm
@@ -30,7 +30,7 @@ sub run ($$;$) {
 	close $in_w or die "close (git credential $op): $!";
 	return $out_r if $op eq 'fill';
 	<$out_r> and die "unexpected output from `git credential $op'\n";
-	close $out_r or die "`git credential $op' failed: \$!=$! \$?=$?\n";
+	$out_r->close or die "`git credential $op' failed: \$!=$! \$?=$?\n";
 }
 
 sub check_netrc {
@@ -61,7 +61,7 @@ sub fill {
 		/\A([^=]+)=(.*)\z/ or die "bad line: $_\n";
 		$self->{$1} = $2;
 	}
-	close $out_r or die "git credential fill failed: \$!=$! \$?=$?\n";
+	$out_r->close or die "git credential fill failed: \$!=$! \$?=$?\n";
 	$self->{filled} = 1;
 }
 
diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index 6eee8774..e12a56e8 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -476,7 +476,7 @@ sub done {
 	my $io = delete $self->{io} or return;
 	eval {
 		print $io "done\n" or wfail;
-		close $io; # reaps and dies on error
+		$io->close or croak "close fast-import \$?=$?"; # reaps
 	};
 	my $wait_err = $@;
 	my $nchg = delete $self->{nchg};
@@ -489,7 +489,7 @@ sub done {
 	die $wait_err if $wait_err;
 }
 
-sub atfork_child { close(delete($_[0]->{io}) // return) }
+sub atfork_child { (delete($_[0]->{io}) // return)->close }
 
 sub digest2mid ($$;$) {
 	my ($dig, $hdr, $fallback_time) = @_;
@@ -598,7 +598,7 @@ sub replace_oids {
 			push @buf, $_;
 		}
 	}
-	close $rd;
+	$rd->close or die "E: git @export (\$?=$?)";
 	if (@buf) {
 		print $io @buf or wfail;
 	}
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 0f6f7f6f..2832db63 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -501,7 +501,7 @@ sub err ($;@) {
 	my @eor = (substr($_[-1]//'', -1, 1) eq "\n" ? () : ("\n"));
 	print $err @_, @eor and return;
 	my $old_err = delete $self->{2};
-	close($old_err) if $! == EPIPE && $old_err;
+	$old_err->close if $! == EPIPE && $old_err;
 	$err = $self->{2} = ($self->{pgr} // [])->[2] // *STDERR{GLOB};
 	print $err @_, @eor or print STDERR @_, @eor;
 }
@@ -516,7 +516,7 @@ sub qfin { # show message on finalization (LeiFinmsg)
 
 sub fail_handler ($;$$) {
 	my ($lei, $code, $io) = @_;
-	close($io) if $io; # needed to avoid warnings on SIGPIPE
+	$io->close if $io; # needed to avoid warnings on SIGPIPE
 	_drop_wq($lei);
 	x_it($lei, $code // (1 << 8));
 }
@@ -565,7 +565,7 @@ sub child_error { # passes non-fatal curl exit codes to user
 
 sub note_sigpipe { # triggers sigpipe_handler
 	my ($self, $fd) = @_;
-	close(delete($self->{$fd})); # explicit close silences Perl warning
+	delete($self->{$fd})->close; # explicit close silences Perl warning
 	$self->{pkt_op_p}->pkt_do('sigpipe_handler') if $self->{pkt_op_p};
 	x_it($self, 13);
 }
@@ -1129,7 +1129,7 @@ sub stop_pager {
 	my ($self) = @_;
 	my $pgr = delete($self->{pgr}) or return;
 	$self->{2} = $pgr->[2];
-	close(delete($self->{1})) if $self->{1};
+	delete($self->{1})->close if $self->{1};
 	$self->{1} = $pgr->[1];
 }
 
diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm
index 28b73ca9..f7c3f573 100644
--- a/lib/PublicInbox/LeiInput.pm
+++ b/lib/PublicInbox/LeiInput.pm
@@ -246,7 +246,7 @@ sub input_path_url {
 			my $fh = popen_rd($fp, undef, $rdr);
 			eval { $self->input_fh('eml', $fh, $input, @args) };
 			my @err = ($@ ? $@ : ());
-			close($fh) or push @err, "\$?=$?";
+			$fh->close or push @err, "\$?=$?";
 			$lei->child_error($?, "@$fp failed: @err") if @err;
 		} else {
 			$self->folder_missing("$ifmt:$input");
diff --git a/lib/PublicInbox/LeiRediff.pm b/lib/PublicInbox/LeiRediff.pm
index 230f3e83..fdff4b4b 100644
--- a/lib/PublicInbox/LeiRediff.pm
+++ b/lib/PublicInbox/LeiRediff.pm
@@ -126,7 +126,7 @@ EOM
 			qw(fast-import --quiet --done --date-format=raw)],
 			$lei->{env}, { 2 => $lei->{2} });
 	print $w $ta, "\n", $tb, "\ndone\n" or die "print fast-import: $!";
-	close $w or die "close w fast-import: \$?=$? \$!=$!";
+	$w->close or die "close w fast-import: \$?=$? \$!=$!";
 
 	my $cmd = [ 'diff' ];
 	_lei_diff_prepare($lei, $cmd);
diff --git a/lib/PublicInbox/LeiViewText.pm b/lib/PublicInbox/LeiViewText.pm
index 70441867..ce9f248e 100644
--- a/lib/PublicInbox/LeiViewText.pm
+++ b/lib/PublicInbox/LeiViewText.pm
@@ -75,7 +75,7 @@ sub new {
 	my @cmd = qw(git config -z --includes -l); # reuse normal git config
 	my $r = popen_rd(\@cmd, undef, { 2 => $lei->{2} });
 	my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n");
-	if (!close($r)) {
+	if ($r->close) {
 		warn "# @cmd failed, no color (non-fatal \$?=$?)\n";
 		return $self;
 	}
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index 241b9dab..5443188d 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -353,14 +353,14 @@ print STDERR $_;
 						$lei, $each_smsg);
 		$lei->sto_done_request if delete($self->{-sto_imported});
 		my $nr = delete $lei->{-nr_remote_eml} // 0;
-		close $cfh;
+		$cfh->close;
 		my $code = $?;
 		if (!$code) { # don't update if no results, maybe MTA is down
 			$lei->{lss}->cfg_set($key, $start) if $key && $nr;
 			mset_progress($lei, $lei->{-current_url}, $nr, $nr);
 			next;
 		}
-		close(delete($rdr->{2})) if @lbf_tee;
+		delete($rdr->{2})->close if @lbf_tee;
 		seek($cerr, 0, SEEK_SET);
 		read($cerr, my $err, -s $cerr);
 		truncate($cerr, 0);
@@ -396,8 +396,8 @@ sub query_done { # EOF callback for main daemon
 	$lei->{ovv}->ovv_end($lei);
 	if ($l2m) { # close() calls LeiToMail reap_compress
 		if (my $out = delete $lei->{old_1}) {
-			if (my $mbout = $lei->{1}) {
-				close($mbout) or die <<"";
+			if (my $mbout = $lei->{1}) { # compressor pipe process
+				$mbout->close or die <<"";
 Error closing $lei->{ovv}->{dst}: \$!=$! \$?=$?
 
 			}
diff --git a/lib/PublicInbox/MboxReader.pm b/lib/PublicInbox/MboxReader.pm
index d67fb4eb..3d78ca23 100644
--- a/lib/PublicInbox/MboxReader.pm
+++ b/lib/PublicInbox/MboxReader.pm
@@ -29,7 +29,7 @@ sub _mbox_from {
 	my @raw;
 	while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
 		if ($r == 0) { # close here to check for "curl --fail"
-			close($mbfh) or die "error closing mbox: \$?=$? $!";
+			$mbfh->close or die "error closing mbox: \$?=$? $!";
 			@raw = ($buf);
 		} else {
 			@raw = split(/$from_strict/mos, $buf, -1);
@@ -88,7 +88,7 @@ sub _mbox_cl ($$$;@) {
 	my $buf = '';
 	while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
 		if ($r == 0) { # detect "curl --fail"
-			close($mbfh) or
+			$mbfh->close or
 				die "error closing mboxcl/mboxcl2: \$?=$? $!";
 			undef $mbfh;
 		}
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 3c64c715..78519b22 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -980,7 +980,7 @@ sub log2stack ($$$) {
 			$stk->push_rec('m', $at, $ct, $oid, $cmt);
 		}
 	}
-	close $fh or die "git log failed: \$?=$?";
+	$fh->close or die "git log failed: \$?=$?";
 	$stk //= PublicInbox::IdxStack->new;
 	$stk->read_prepare;
 }
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index e7d2c6ea..b4f37bea 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -421,7 +421,7 @@ sub run_qx {
 		local $/;
 		$ret[0] = <$fh>;
 	}
-	close $fh; # caller should check $?
+	$fh->close; # caller should check $?
 	read_out_err($opt);
 	wantarray ? @ret : $ret[0];
 }
diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index 191c5588..4d606dfe 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -1070,7 +1070,7 @@ sub unindex_todo ($$$) {
 		/\A:\d{6} 100644 $OID ($OID) [AM]\tm$/o or next;
 		$self->git->cat_async($1, $unindex_oid, { %$sync, oid => $1 });
 	}
-	close $fh or die "git log failed: \$?=$?";
+	$fh->close or die "git log failed: \$?=$?";
 	$self->git->async_wait_all;
 
 	return unless $sync->{-opt}->{prune};
diff --git a/script/public-inbox-convert b/script/public-inbox-convert
index 96f6d2ea..d8186809 100755
--- a/script/public-inbox-convert
+++ b/script/public-inbox-convert
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <http://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use v5.10.1;
@@ -158,7 +158,7 @@ while (<$rd>) {
 	last if $_ eq "done\n";
 	print $io $_ or $im->wfail;
 }
-close $rd or die "fast-export: \$?=$? \$!=$!\n";
+$rd->close or die "fast-export: \$?=$? \$!=$!\n";
 $io = undef;
 $v2w->done;
 if (my $old_mm = $old->mm) {
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 2d2d1061..da1c24b9 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -368,7 +368,7 @@ SKIP: {
 		$n += $r;
 		$buf =~ /\A\0+\z/ or $non_zero++;
 	}
-	close $fh or die "close curl pipe: $!";
+	$fh->close or die "close curl pipe: $!";
 	is($?, 0, 'curl succesful');
 	is($n, 30 * 1024 * 1024, 'got expected output from curl');
 	is($non_zero, 0, 'read all zeros');
diff --git a/t/lei-q-kw.t b/t/lei-q-kw.t
index 4edee72a..06e1df6c 100644
--- a/t/lei-q-kw.t
+++ b/t/lei-q-kw.t
@@ -1,5 +1,5 @@
 #!perl -w
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict; use v5.10.1; use PublicInbox::TestCommon;
 use POSIX qw(mkfifo);
@@ -51,7 +51,7 @@ SKIP: {
 		'--import-before fails on non-seekable output');
 	like($lei_err, qr/not seekable/, 'unseekable noted in error');
 	is(do { local $/; <$cat> }, '', 'no output on FIFO');
-	close $cat;
+	$cat->close;
 	$cat = popen_rd(['cat', $o]);
 	lei_ok(qw(q m:qp@example.com -o), "mboxrd:$o");
 	my $buf = do { local $/; <$cat> };
diff --git a/t/spawn.t b/t/spawn.t
index 4b3baae4..938a2e5e 100644
--- a/t/spawn.t
+++ b/t/spawn.t
@@ -80,7 +80,7 @@ EOF
 	is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
 	ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
 	is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
-	ok(close $rd, 'popen_rd close works');
+	ok($rd->close, 'popen_rd close works');
 	PublicInbox::DS::sig_setmask($oldset);
 }
 
@@ -133,13 +133,13 @@ EOF
 	is($buf, "hello\n", 'tied gets works');
 	is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
 	$? = 1;
-	ok(close($fh), 'close succeeds');
+	ok($fh->close, 'close succeeds');
 	is($?, 0, '$? set properly');
 }
 
 {
 	my $fh = popen_rd([qw(false)]);
-	ok(!close($fh), 'close fails on false');
+	ok(!$fh->close, 'close fails on false');
 	isnt($?, 0, '$? set properly: '.$?);
 }
 
@@ -152,7 +152,7 @@ EOF
 { # ->CLOSE vs ->DESTROY waitpid caller distinction
 	my @c;
 	my $fh = popen_rd(['true'], undef, undef, sub { @c = caller });
-	ok(close($fh), '->CLOSE fired and successful');
+	ok($fh->close, '->CLOSE fired and successful');
 	ok(scalar(@c), 'callback fired by ->CLOSE');
 	ok(grep(!m[/PublicInbox/DS\.pm\z], @c), 'callback not invoked by DS');
 
@@ -183,7 +183,7 @@ EOF
 	my @w;
 	local $SIG{__WARN__} = sub { push @w, @_ };
 	close $w;
-	close $fh;
+	$fh->close; # may set $?
 	is($?, 0, 'cat exited');
 	is(scalar(@arg), 2, 'callback got args');
 	is($arg[1], 'hi', 'passed arg');
diff --git a/xt/git_async_cmp.t b/xt/git_async_cmp.t
index 9edc1f37..4038898b 100644
--- a/xt/git_async_cmp.t
+++ b/xt/git_async_cmp.t
@@ -31,7 +31,7 @@ my $async = timeit($nr, sub {
 		my ($oid, undef, undef) = split(/ /);
 		$git->cat_async($oid, $cb);
 	}
-	close $cat or die "cat: $?";
+	$cat->close or xbail "cat: $?";
 	$git->async_wait_all;
 	push @dig, ['async', $dig->hexdigest ];
 });
@@ -44,7 +44,7 @@ my $sync = timeit($nr, sub {
 		my $bref = $git->cat_file($oid);
 		$dig->add($$bref);
 	}
-	close $cat or die "cat: $?";
+	$cat->close or xbail "cat: $?";
 	push @dig, ['sync', $dig->hexdigest ];
 });
 
diff --git a/xt/httpd-async-stream.t b/xt/httpd-async-stream.t
index 904f2ae9..099ceb79 100644
--- a/xt/httpd-async-stream.t
+++ b/xt/httpd-async-stream.t
@@ -67,7 +67,7 @@ my $do_get_all = sub {
 	}
 	my $res = $dig->hexdigest;
 	my $elapsed = sprintf('%0.3f', now() - $t0);
-	close $rd or die "close curl failed: $! \$?=$?\n";
+	$rd->close or xbail "close curl failed: $! \$?=$?\n";
 	print STDERR "# $job $$ ($?) $res (${elapsed}s) $bytes bytes\n";
 	$res;
 };

  parent reply	other threads:[~2023-11-02  9:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-02  9:35 [PATCH 00/14] IO/IPC-related cleanups Eric Wong
2023-11-02  9:35 ` [PATCH 01/14] xap_helper.pm: use do_fork to Reset and reseed Eric Wong
2023-11-02  9:35 ` [PATCH 02/14] ds: replace FD map hash table with array Eric Wong
2023-11-02  9:35 ` [PATCH 03/14] treewide: use ->close method rather than CORE::close Eric Wong
2023-11-02 21:35   ` [PATCH 15/14] ds: don't try ->close after ->accept_SSL failure Eric Wong
2023-11-02  9:35 ` [PATCH 04/14] cindex: drop redundant close on regular FH Eric Wong
2023-11-02  9:35 ` Eric Wong [this message]
2023-11-02  9:35 ` [PATCH 06/14] multi_git: use autodie Eric Wong
2023-11-02  9:35 ` [PATCH 07/14] git_credential: use autodie where appropriate Eric Wong
2023-11-02  9:35 ` [PATCH 08/14] replace ProcessIO with untied PublicInbox::IO Eric Wong
2023-11-02  9:35 ` [PATCH 09/14] io: introduce write_file helper sub Eric Wong
2023-11-02  9:35 ` [PATCH 10/14] spawn: support PerlIO layer in scalar redirects Eric Wong
2023-11-02  9:35 ` [PATCH 11/14] treewide: check alternates writes with eof + autodie Eric Wong
2023-11-02  9:35 ` [PATCH 12/14] treewide: use eof and close to detect readline errors Eric Wong
2023-11-02  9:35 ` [PATCH 13/14] move read_all, try_cat, and poll_in to PublicInbox::IO Eric Wong
2023-11-02 20:59   ` www: squash read_all usage fix Eric Wong
2023-11-02  9:35 ` [PATCH 14/14] t/cindex+extsearch: use write_file, autodie, etc Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231102093539.2067470-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).