unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] lei_store: fix locking w.r.t epoch creation
Date: Thu, 29 Apr 2021 19:49:57 +0000	[thread overview]
Message-ID: <20210429194957.19303-1-e@80x24.org> (raw)

Prior to this change, it was possible for oneshot lei processes
to race on epoch creation/rollover.  lei-daemon normally
prevents the problem by funnelling all writes to a single
socket, but oneshot lei has no such protection.
---
 lib/PublicInbox/ExtSearchIdx.pm | 2 +-
 lib/PublicInbox/LeiStore.pm     | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm
index 9d6b3b9d..08f2295a 100644
--- a/lib/PublicInbox/ExtSearchIdx.pm
+++ b/lib/PublicInbox/ExtSearchIdx.pm
@@ -926,7 +926,7 @@ sub update_last_commit { # overrides V2Writable
 
 sub _idx_init { # with_umask callback
 	my ($self, $opt) = @_;
-	PublicInbox::V2Writable::_idx_init($self, $opt);
+	PublicInbox::V2Writable::_idx_init($self, $opt); # acquires ei.lock
 	$self->{midx} = PublicInbox::MiscIdx->new($self);
 }
 
diff --git a/lib/PublicInbox/LeiStore.pm b/lib/PublicInbox/LeiStore.pm
index fcc9224d..8af740fd 100644
--- a/lib/PublicInbox/LeiStore.pm
+++ b/lib/PublicInbox/LeiStore.pm
@@ -88,6 +88,7 @@ sub importer {
 		$self->checkpoint;
 		$max = $self->git_epoch_max + 1;
 	}
+	my (undef, $tl) = eidx_init($self); # acquire lock
 	my $pfx = $self->git_pfx;
 	$max //= $self->git_epoch_max;
 	while (1) {
@@ -97,7 +98,9 @@ sub importer {
 		my $git = PublicInbox::Git->new($latest);
 		if (!$old) {
 			$git->qx(qw(config core.sharedRepository 0600));
-			$self->done; # force eidx_init on next round
+			$self->done; # unlock
+			# re-acquire lock, update alternates for new epoch
+			(undef, $tl) = eidx_init($self);
 		}
 		my $packed_bytes = $git->packed_bytes;
 		my $unpacked_bytes = $packed_bytes / $self->packing_factor;
@@ -130,7 +133,7 @@ sub eidx_init {
 	my $tl = wantarray && $self->{-err_wr} ?
 			PublicInbox::OnDestroy->new($$, \&_tail_err, $self) :
 			undef;
-	$eidx->idx_init({-private => 1});
+	$eidx->idx_init({-private => 1}); # acquires lock
 	wantarray ? ($eidx, $tl) : $eidx;
 }
 
@@ -204,7 +207,7 @@ sub set_sync_info ($$$) {
 sub add_eml {
 	my ($self, $eml, $vmd, $xoids) = @_;
 	my $im = $self->importer; # may create new epoch
-	my ($eidx, $tl) = eidx_init($self); # updates/writes alternates file
+	my ($eidx, $tl) = eidx_init($self);
 	my $oidx = $eidx->{oidx}; # PublicInbox::Import::add checks this
 	my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
 	my $im_mark = $im->add($eml, undef, $smsg);

                 reply	other threads:[~2021-04-29 19:49 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=20210429194957.19303-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.
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).