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] gcf2: fix loading at runtime
Date: Mon, 20 Sep 2021 13:00:33 +0000	[thread overview]
Message-ID: <20210920130033.3554-1-e@80x24.org> (raw)

We need to waitpid synchronously on pkg-config to use $?.

When loading Gcf2 inside the event loop, implicit dwaitpid
done by PublicInbox::ProcessPipe would not call waitpid in
time to zero $?.  This was causing one of my -httpd to
occasionally fall back to git(1) instead of using Gcf2.

This was noted in:
Link: https://public-inbox.org/meta/20210914085322.25517-1-e@80x24.org/
---
 lib/PublicInbox/Gcf2.pm | 43 +++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/lib/PublicInbox/Gcf2.pm b/lib/PublicInbox/Gcf2.pm
index 0d31b014..64945ca6 100644
--- a/lib/PublicInbox/Gcf2.pm
+++ b/lib/PublicInbox/Gcf2.pm
@@ -17,36 +17,43 @@ BEGIN {
 		die 'PERL_INLINE_DIRECTORY not defined';
 	my $f = "$inline_dir/.public-inbox.lock";
 	open my $fh, '+>', $f or die "open($f): $!";
+
+	# CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
+	flock($fh, LOCK_EX) or die "LOCK_EX($f): $!\n";
+
 	my $pc = which($ENV{PKG_CONFIG} // 'pkg-config') //
 		die "pkg-config missing for libgit2";
 	my ($dir) = (__FILE__ =~ m!\A(.+?)/[^/]+\z!);
-	my $rdr = {};
-	open $rdr->{2}, '>', '/dev/null' or die "open /dev/null: $!";
+	my $ef = "$inline_dir/.public-inbox.pkg-config.err";
+	open my $err, '+>', $ef or die "open($ef): $!";
 	for my $x (qw(libgit2)) {
-		my $l = popen_rd([$pc, '--libs', $x], undef, $rdr);
+		my $rdr = { 2 => $err };
+		my ($l, $pid) = popen_rd([$pc, '--libs', $x], undef, $rdr);
 		$l = do { local $/; <$l> };
+		waitpid($pid, 0);
 		next if $?;
-		my $c = popen_rd([$pc, '--cflags', $x], undef, $rdr);
+		(my $c, $pid) = popen_rd([$pc, '--cflags', $x], undef, $rdr);
 		$c = do { local $/; <$c> };
+		waitpid($pid, 0);
 		next if $?;
 
 		# note: we name C source files .h to prevent
 		# ExtUtils::MakeMaker from automatically trying to
 		# build them.
 		my $f = "$dir/gcf2_$x.h";
-		if (open(my $fh, '<', $f)) {
-			chomp($l, $c);
-			local $/;
-			defined($c_src = <$fh>) or die "read $f: $!";
-			$CFG{LIBS} = $l;
-			$CFG{CCFLAGSEX} = $c;
-			last;
-		} else {
-			die "E: $f: $!";
-		}
+		open(my $src, '<', $f) or die "E: open($f): $!";
+		chomp($l, $c);
+		local $/;
+		defined($c_src = <$src>) or die "read $f: $!";
+		$CFG{LIBS} = $l;
+		$CFG{CCFLAGSEX} = $c;
+		last;
+	}
+	unless ($c_src) {
+		seek($err, 0, SEEK_SET);
+		$err = do { local $/; <$err> };
+		die "E: libgit2 not installed: $err\n";
 	}
-	die "E: libgit2 not installed\n" unless $c_src;
-
 	open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
 	open my $olderr, '>&', \*STDERR or die "dup(2): $!";
 	open STDOUT, '>&', $fh or die "1>$f: $!";
@@ -54,14 +61,12 @@ BEGIN {
 	STDERR->autoflush(1);
 	STDOUT->autoflush(1);
 
-	# CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
-	flock($fh, LOCK_EX) or die "LOCK_EX($f): $!\n";
 	# we use Capitalized and ALLCAPS for compatibility with old Inline::C
 	eval <<'EOM';
 use Inline C => Config => %CFG, BOOT => q[git_libgit2_init();];
 use Inline C => $c_src, BUILD_NOISY => 1;
 EOM
-	my $err = $@;
+	$err = $@;
 	open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
 	open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
 	if ($err) {

                 reply	other threads:[~2021-09-20 13:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210920130033.3554-1-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).