user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] extindex: guard against buggy unrefs
@ 2021-10-14  5:49 Eric Wong
  2021-10-14  6:06 ` [PATCH v2] " Eric Wong
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Wong @ 2021-10-14  5:49 UTC (permalink / raw)
  To: meta

I noticed some unref messages which shouldn't have been
happening, but they were.  Which is troubling.  So add
a guard around an unref path until we can get to the bottom
of this.
---
 lib/PublicInbox/ExtSearchIdx.pm | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm
index 750ced5c..363e5665 100644
--- a/lib/PublicInbox/ExtSearchIdx.pm
+++ b/lib/PublicInbox/ExtSearchIdx.pm
@@ -18,6 +18,7 @@ use strict;
 use v5.10.1;
 use parent qw(PublicInbox::ExtSearch PublicInbox::Lock);
 use Carp qw(croak carp);
+use Scalar::Util qw(blessed);
 use Sys::Hostname qw(hostname);
 use POSIX qw(strftime);
 use File::Glob qw(bsd_glob GLOB_NOSORT);
@@ -143,6 +144,14 @@ sub _unref_doc ($$$$$;$) {
 		$smsg = $docid;
 		$docid = $smsg->{num};
 	}
+	if (defined($oidbin) && defined($xnum) && blessed($ibx) $ibx->over) {
+		my $smsg = $ibx->over->get_art($xnum);
+		if ($smsg && pack('H*', $smsg->{blob}) eq $oidbin) {
+			carp("BUG: (non-fatal) ".$ibx->eidx_key.
+				" #$xnum $smsg->{blob} still valid");
+			return;
+		}
+	}
 	my $s = 'DELETE FROM xref3 WHERE oidbin = ?';
 	$s .= ' AND ibx_id = ?' if defined($ibx);
 	$s .= ' AND xnum = ?' if defined($xnum);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] extindex: guard against buggy unrefs
  2021-10-14  5:49 [PATCH] extindex: guard against buggy unrefs Eric Wong
@ 2021-10-14  6:06 ` Eric Wong
  2021-10-14 21:09   ` Eric Wong
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Wong @ 2021-10-14  6:06 UTC (permalink / raw)
  To: meta

I noticed some unref messages which shouldn't have been
happening, but they were.  Which is troubling.  So add
a guard around an unref path until we can get to the bottom
of this.
---
 v2: added missing '&&', v1 didn't even compile :x

 lib/PublicInbox/ExtSearchIdx.pm | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm
index 750ced5c..3d7a6e7d 100644
--- a/lib/PublicInbox/ExtSearchIdx.pm
+++ b/lib/PublicInbox/ExtSearchIdx.pm
@@ -18,6 +18,7 @@ use strict;
 use v5.10.1;
 use parent qw(PublicInbox::ExtSearch PublicInbox::Lock);
 use Carp qw(croak carp);
+use Scalar::Util qw(blessed);
 use Sys::Hostname qw(hostname);
 use POSIX qw(strftime);
 use File::Glob qw(bsd_glob GLOB_NOSORT);
@@ -143,6 +144,14 @@ sub _unref_doc ($$$$$;$) {
 		$smsg = $docid;
 		$docid = $smsg->{num};
 	}
+	if (defined($oidbin) && defined($xnum) && blessed($ibx) && $ibx->over) {
+		my $smsg = $ibx->over->get_art($xnum);
+		if ($smsg && pack('H*', $smsg->{blob}) eq $oidbin) {
+			carp("BUG: (non-fatal) ".$ibx->eidx_key.
+				" #$xnum $smsg->{blob} still valid");
+			return;
+		}
+	}
 	my $s = 'DELETE FROM xref3 WHERE oidbin = ?';
 	$s .= ' AND ibx_id = ?' if defined($ibx);
 	$s .= ' AND xnum = ?' if defined($xnum);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] extindex: guard against buggy unrefs
  2021-10-14  6:06 ` [PATCH v2] " Eric Wong
@ 2021-10-14 21:09   ` Eric Wong
  2021-10-16  1:48     ` [PATCH] extindex: avoid triggering a buggy unref Eric Wong
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Wong @ 2021-10-14 21:09 UTC (permalink / raw)
  To: meta

Eric Wong <e@80x24.org> wrote:
> I noticed some unref messages which shouldn't have been
> happening, but they were.  Which is troubling.  So add
> a guard around an unref path until we can get to the bottom
> of this.

And now I'm not seeing it...  Heisenbugs :<

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] extindex: avoid triggering a buggy unref
  2021-10-14 21:09   ` Eric Wong
@ 2021-10-16  1:48     ` Eric Wong
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2021-10-16  1:48 UTC (permalink / raw)
  To: meta

Eric Wong <e@80x24.org> wrote:
> Eric Wong <e@80x24.org> wrote:
> > I noticed some unref messages which shouldn't have been
> > happening, but they were.  Which is troubling.  So add
> > a guard around an unref path until we can get to the bottom
> > of this.
> 
> And now I'm not seeing it...  Heisenbugs :<

OK, it worked as intended and help me find a bug \o/

-----------8<----------
Subject: [PATCH] extindex: avoid triggering a buggy unref

We can't attempt to unref messages beyond the highwater mark of
an inbox.  This bugfix was found by commit c485036d0b1ce7ed
(extindex: guard against buggy unrefs, 2021-10-14), which
actually did its intended job and guarded against a buggy unref.

The window for the bug is when one process is doing -index
against an inbox, -extdindex --reindex is working on the same
inbox.
---
 lib/PublicInbox/ExtSearchIdx.pm | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm
index a08a9451..69d048fb 100644
--- a/lib/PublicInbox/ExtSearchIdx.pm
+++ b/lib/PublicInbox/ExtSearchIdx.pm
@@ -839,7 +839,7 @@ sub _unref_stale_range ($$$) {
 		$r = $sync->{self}->{oidx}->dbh->selectall_arrayref(
 			<<EOS, undef, $ibx->{-ibx_id});
 SELECT docid,xnum,oidbin FROM xref3
-WHERE ibx_id = ? AND xnum $lt_or_gt LIMIT $lim
+WHERE ibx_id = ? AND $lt_or_gt LIMIT $lim
 EOS
 		return if $sync->{quit};
 		for (@$r) { # hopefully rare, not worth optimizing:
@@ -859,7 +859,7 @@ sub _reindex_check_ibx ($$$) {
 	my $opt = { limit => $slice };
 	my ($beg, $end) = (1, $slice);
 	my $err = sync_inbox($self, $sync, $ibx) and return;
-	my $max = $ibx->over->max;
+	my $max = $ibx->mm->num_highwater;
 	$end = $max if $end > $max;
 
 	# first, check if we missed any messages in target $ibx
@@ -869,7 +869,7 @@ sub _reindex_check_ibx ($$$) {
 	local $sync->{-regen_fmt} = "$ekey checking %u/$max\n";
 	${$sync->{nr}} = 0;
 	my $fast = $sync->{-opt}->{fast};
-	my $dsu; # _unref_stale_range (< $lo) called
+	my $usr; # _unref_stale_range (< $lo) called
 	my ($lo, $hi);
 	while (scalar(@{$msgs = $ibx->over->query_xover($beg, $end, $opt)})) {
 		${$sync->{nr}} = $beg;
@@ -880,7 +880,7 @@ sub _reindex_check_ibx ($$$) {
 			reindex_checkpoint($self, $sync); # release lock
 		}
 		($lo, $hi) = ($msgs->[0]->{num}, $msgs->[-1]->{num});
-		$dsu //= _unref_stale_range($sync, $ibx, "< $lo");
+		$usr //= _unref_stale_range($sync, $ibx, "xnum < $lo");
 		my $x3a = $self->{oidx}->dbh->selectall_arrayref(
 			<<"", undef, $ibx_id, $lo, $hi);
 SELECT xnum,oidbin,docid FROM xref3 WHERE
@@ -921,7 +921,8 @@ ibx_id = ? AND xnum >= ? AND xnum <= ?
 			}
 		}
 	}
-	_unref_stale_range($sync, $ibx, "> $hi") if defined($hi);
+	defined($hi) and ($hi < $max) and
+		_unref_stale_range($sync, $ibx, "xnum > $hi AND xnum <= $max");
 }
 
 sub _reindex_inbox ($$$) {

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-16  1:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14  5:49 [PATCH] extindex: guard against buggy unrefs Eric Wong
2021-10-14  6:06 ` [PATCH v2] " Eric Wong
2021-10-14 21:09   ` Eric Wong
2021-10-16  1:48     ` [PATCH] extindex: avoid triggering a buggy unref Eric Wong

Code repositories for project(s) associated with this 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).