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 2/4] msgmap: mid_insert: reraise on unexpected errors
Date: Tue, 23 Jul 2024 21:28:35 +0000	[thread overview]
Message-ID: <20240723212837.3931413-3-e@80x24.org> (raw)
In-Reply-To: <20240723212837.3931413-1-e@80x24.org>

SQLITE_CONSTRAINT is the only SQLite error we really expect under
normal circumstances.  This avoids infinite loops when writing
to inboxes after hitting ENOSPC.

Reported-by: Robin H. Johnson <robbat2@orbis-terrarum.net>
Link: https://public-inbox.org/git/robbat2-20240722T060013-765939809Z@orbis-terrarum.net/
---
 lib/PublicInbox/Msgmap.pm |  6 +++++-
 t/msgmap.t                | 18 +++++++++++++++++-
 t/v2writable.t            | 18 +++++++++++++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/lib/PublicInbox/Msgmap.pm b/lib/PublicInbox/Msgmap.pm
index cb4bb295..c4bc766d 100644
--- a/lib/PublicInbox/Msgmap.pm
+++ b/lib/PublicInbox/Msgmap.pm
@@ -12,6 +12,7 @@ use strict;
 use v5.10.1;
 use DBI;
 use DBD::SQLite;
+use DBD::SQLite::Constants qw(SQLITE_CONSTRAINT);
 use PublicInbox::Over;
 use Scalar::Util qw(blessed);
 
@@ -113,7 +114,10 @@ sub mid_insert {
 	my $sth = $self->{dbh}->prepare_cached(<<'');
 INSERT INTO msgmap (mid) VALUES (?)
 
-	return unless eval { $sth->execute($mid) };
+	unless (eval { $sth->execute($mid) }) {
+		return if $self->{dbh}->err == SQLITE_CONSTRAINT;
+		die $@;
+	}
 	my $num = $self->{dbh}->last_insert_id(undef, undef, 'msgmap', 'num');
 	$self->num_highwater($num) if defined($num);
 	$num;
diff --git a/t/msgmap.t b/t/msgmap.t
index 124d3b10..031854e0 100644
--- a/t/msgmap.t
+++ b/t/msgmap.t
@@ -3,6 +3,9 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use v5.12;
 use PublicInbox::TestCommon;
+use autodie;
+use Config;
+use PublicInbox::Spawn qw(popen_rd);
 require_mods('DBD::SQLite');
 use_ok 'PublicInbox::Msgmap';
 my ($tmpdir, $for_destroy) = tmpdir();
@@ -70,4 +73,17 @@ is(eval {
 	'ok'
 }, 'ok', 'atfork_* work on tmp_clone');
 
-done_testing();
+SKIP: {
+	my $strace = strace_inject;
+	open my $fh, '>', my $trace = "$tmpdir/trace.out";
+	my $rd = popen_rd([ $strace, '-p', $$, '-o', $trace,
+		'-e', 'inject=pwrite64:error=ENOSPC'], undef, { 2 => 1 });
+	$rd->poll_in(10) or die 'strace not ready';
+	is eval { $d->mid_insert('this-better-trigger-ENOSPC@error') },
+		undef, 'insert fails w/ ENOSPC';
+	like $@, qr/ disk is full/, 'set $@ for ENOSPC';
+	kill 'TERM', $rd->attached_pid;
+	$rd->close;
+}
+
+done_testing;
diff --git a/t/v2writable.t b/t/v2writable.t
index 1b7e9e7d..a062d1b3 100644
--- a/t/v2writable.t
+++ b/t/v2writable.t
@@ -6,6 +6,8 @@ use Test::More;
 use PublicInbox::Eml;
 use PublicInbox::ContentHash qw(content_digest content_hash);
 use PublicInbox::TestCommon;
+use PublicInbox::Spawn qw(popen_rd);
+use Config;
 use Cwd qw(abs_path);
 require_git(2.6);
 require_mods(qw(DBD::SQLite Xapian));
@@ -335,4 +337,18 @@ ok($@, 'V2Writable fails on non-existent dir');
 	is($mode, 0664, sprintf('0%03o', $mode).' is 0664');
 }
 
-done_testing();
+SKIP: {
+	my $strace = strace_inject;
+	my $eml = eml_load 't/plack-qp.eml';
+	open my $fh, '>', my $trace = "$inboxdir/trace.out";
+	my $rd = popen_rd([ $strace, '-p', $$, '-o', $trace,
+		'-e', 'inject=pwrite64:error=ENOSPC'], undef, { 2 => 1 });
+	$rd->poll_in(10) or die 'strace not ready';
+	ok ! eval { $im->add($eml) }, 'v2w->add fails on ENOSPC';
+	like $@, qr/ disk is full/, 'set $@ for ENOSPC';
+	$im->done;
+	kill 'TERM', $rd->attached_pid;
+	$rd->close;
+}
+
+done_testing;

  parent reply	other threads:[~2024-07-23 21:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-23 21:28 [PATCH 0/4] fix ENOSPC on writes Eric Wong
2024-07-23 21:28 ` [PATCH 1/4] t/msgmap: updates for Perl 5.12+ and copyrights Eric Wong
2024-07-23 21:28 ` Eric Wong [this message]
2024-07-24  7:22   ` [PATCH 2/4] msgmap: mid_insert: reraise on unexpected errors, [PATCH 3/4] t/v2writable: test ENOSPC from fast-import Štěpán Němec
2024-07-25  0:38     ` Eric Wong
2024-07-25  6:42       ` Štěpán Němec
2024-07-23 21:28 ` Eric Wong
2024-07-23 21:28 ` [PATCH 4/4] t/v2writable: use 5.10.1 and autodie more Eric Wong
2024-07-26 21:31 ` [PATCH v2 0/4] fix ENOSPC on writes Eric Wong
2024-07-26 21:31   ` [PATCH v2 1/4] t/msgmap: updates for Perl 5.12+ and copyrights Eric Wong
2024-07-26 21:31   ` [PATCH v2 2/4] msgmap: mid_insert: reraise on unexpected errors Eric Wong
2024-07-26 21:31   ` [PATCH v2 3/4] t/v2writable: test ENOSPC from fast-import Eric Wong
2024-07-26 21:31   ` [PATCH v2 4/4] t/v2writable: use 5.10.1 and autodie more 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=20240723212837.3931413-3-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).