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 13/21] lock: retry on EINTR, improve error reporting
Date: Wed,  4 Oct 2023 03:49:25 +0000	[thread overview]
Message-ID: <20231004034933.3343930-14-e@80x24.org> (raw)
In-Reply-To: <20231004034933.3343930-1-e@80x24.org>

We'll also add a handy ->new function since there's
a bunch of places we just create objects with bless.
---
 lib/PublicInbox/Lock.pm | 52 +++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/lib/PublicInbox/Lock.pm b/lib/PublicInbox/Lock.pm
index 0ee2a8bd..ddaf3312 100644
--- a/lib/PublicInbox/Lock.pm
+++ b/lib/PublicInbox/Lock.pm
@@ -1,36 +1,42 @@
-# Copyright (C) 2018-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>
 
-# Base class for per-inbox locking
+# Base class for per-inbox locking, subclassed by several
+# only uses {lock_path} and {lockfh} fields
 package PublicInbox::Lock;
-use strict;
-use v5.10.1;
-use Fcntl qw(:flock :DEFAULT);
+use v5.12;
+use Fcntl qw(LOCK_UN LOCK_EX O_RDWR O_CREAT);
 use Carp qw(croak);
 use PublicInbox::OnDestroy;
+use Errno qw(EINTR);
+use autodie qw(close sysopen syswrite);
+
+sub xflock ($$) {
+	until (flock($_[0], $_[1])) { return if $! != EINTR }
+	1;
+}
+
+sub new { bless { lock_path => $_[1] }, $_[0] }
 
 # we only acquire the flock if creating or reindexing;
 # PublicInbox::Import already has the lock on its own.
 sub lock_acquire {
 	my ($self) = @_;
-	my $lock_path = $self->{lock_path};
-	croak 'already locked '.($lock_path // '(undef)') if $self->{lockfh};
-	return unless defined($lock_path);
-	sysopen(my $lockfh, $lock_path, O_RDWR|O_CREAT) or
-		croak "failed to open $lock_path: $!\n";
-	flock($lockfh, LOCK_EX) or croak "lock $lock_path failed: $!\n";
-	$self->{lockfh} = $lockfh;
+	my $fn = $self->{lock_path};
+	croak 'already locked '.($fn // '(undef)') if $self->{lockfh};
+	$fn // return;
+	sysopen(my $fh, $fn, O_RDWR|O_CREAT);
+	xflock($fh, LOCK_EX) or croak "LOCK_EX $fn: $!";
+	$self->{lockfh} = $fh;
 }
 
 sub lock_release {
 	my ($self, $wake) = @_;
-	defined(my $lock_path = $self->{lock_path}) or return;
-	my $lockfh = delete $self->{lockfh} or croak "not locked: $lock_path";
-
-	syswrite($lockfh, '.') if $wake;
-
-	flock($lockfh, LOCK_UN) or croak "unlock $lock_path failed: $!\n";
-	close $lockfh or croak "close $lock_path failed: $!\n";
+	my $fn = $self->{lock_path} // return;
+	my $fh = delete $self->{lockfh} or croak "not locked: $fn";
+	syswrite($fh, '.') if $wake;
+	xflock($fh, LOCK_UN) or croak "LOCK_UN $fn: $!";
+	close $fh; # may detect errors
 }
 
 # caller must use return value
@@ -41,13 +47,13 @@ sub lock_for_scope {
 }
 
 sub lock_acquire_fast {
-	$_[0]->{lockfh} or return lock_acquire($_[0]);
-	flock($_[0]->{lockfh}, LOCK_EX) or croak "lock (fast) failed: $!";
+	my $fh = $_[0]->{lockfh} or return lock_acquire($_[0]);
+	xflock($fh, LOCK_EX) or croak "LOCK_EX $_[0]->{lock_path}: $!";
 }
 
 sub lock_release_fast {
-	flock($_[0]->{lockfh} // return, LOCK_UN) or
-			croak "unlock (fast) $_[0]->{lock_path}: $!";
+	xflock($_[0]->{lockfh} // return, LOCK_UN) or
+		croak "LOCK_UN $_[0]->{lock_path}: $!"
 }
 
 # caller must use return value

  parent reply	other threads:[~2023-10-04  3:49 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-04  3:49 [PATCH 00/21] lei + IPC related stuff Eric Wong
2023-10-04  3:49 ` [PATCH 01/21] lei: drop stores explicitly at daemon shutdown Eric Wong
2023-10-04  3:49 ` [PATCH 02/21] ds: hoist out close_non_busy Eric Wong
2023-10-04  3:49 ` [PATCH 03/21] ds: don't pass FD map to post_loop_do callback Eric Wong
2023-10-04  3:49 ` [PATCH 04/21] move all non-test @post_loop_do into named subs Eric Wong
2023-10-04  3:49 ` [PATCH 05/21] lei: close DirIdle (inotify) early at daemon shutdown Eric Wong
2023-10-04  3:49 ` [PATCH 06/21] input_pipe: {args} is never undefined Eric Wong
2023-10-04  3:49 ` [PATCH 07/21] lei: do_env combines fchdir and local Eric Wong
2023-10-04  3:49 ` [PATCH 08/21] lei: get rid of l2m_progress PktOp callback Eric Wong
2023-10-04  3:49 ` [PATCH 09/21] t/lei_to_mail: modernize and document test Eric Wong
2023-10-04  3:49 ` [PATCH 10/21] lei: reuse PublicInbox::Config::noop Eric Wong
2023-10-04  3:49 ` [PATCH 11/21] lei: keep signals blocked on daemon shutdown Eric Wong
2023-10-04  3:49 ` [PATCH 12/21] mbox_lock: retry on EINTR and use autodie Eric Wong
2023-10-04  3:49 ` Eric Wong [this message]
2023-10-04  3:49 ` [PATCH 14/21] treewide: use PublicInbox::Lock->new Eric Wong
2023-10-04  3:49 ` [PATCH 15/21] gcf2: use PublicInbox::Lock Eric Wong
2023-10-04  3:49 ` [PATCH 16/21] spawn: use autodie and PublicInbox::Lock Eric Wong
2023-10-04  3:49 ` [PATCH 17/21] xap_helper: retry flock on EINTR Eric Wong
2023-10-04  3:49 ` [PATCH 18/21] xap_helper.pm: use EINTR-aware recv_cmd Eric Wong
2023-10-04  3:49 ` [PATCH 19/21] spawn: drop checks for directory writability Eric Wong
2023-10-04  3:49 ` [PATCH 20/21] lei: document and local-ize $OPT hashref Eric Wong
2023-10-04  3:49 ` [PATCH 21/21] searchidx: fix redundant `in' in warning message 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=20231004034933.3343930-14-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).