From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 12/21] mbox_lock: retry on EINTR and use autodie
Date: Wed, 4 Oct 2023 03:49:24 +0000 [thread overview]
Message-ID: <20231004034933.3343930-13-e@80x24.org> (raw)
In-Reply-To: <20231004034933.3343930-1-e@80x24.org>
EINTR needs to be retried in case signalfd and kevent aren't
available. And autodie makes it easier to focus on more
important stuff.
---
lib/PublicInbox/MboxLock.pm | 49 +++++++++++++++++--------------------
1 file changed, 22 insertions(+), 27 deletions(-)
diff --git a/lib/PublicInbox/MboxLock.pm b/lib/PublicInbox/MboxLock.pm
index 856b1e21..95aa9862 100644
--- a/lib/PublicInbox/MboxLock.pm
+++ b/lib/PublicInbox/MboxLock.pm
@@ -1,15 +1,15 @@
-# Copyright (C) 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>
# Various mbox locking methods
package PublicInbox::MboxLock;
-use strict;
-use v5.10.1;
+use v5.12;
use PublicInbox::OnDestroy;
use Fcntl qw(:flock F_SETLK F_SETLKW F_RDLCK F_WRLCK
O_CREAT O_EXCL O_WRONLY SEEK_SET);
use Carp qw(croak);
use PublicInbox::DS qw(now); # ugh...
+use autodie qw(chdir opendir unlink);
our $TMPL = do {
if ($^O eq 'linux') { \'s @32' }
@@ -58,16 +58,13 @@ sub acq_dotlock {
rand(0xffffffff), $pid, time);
if (sysopen(my $fh, $tmp, O_CREAT|O_EXCL|O_WRONLY)) {
if (link($tmp, $dot_lock)) {
- unlink($tmp) or die "unlink($tmp): $!";
+ unlink($tmp);
$self->{".lock$pid"} = $dot_lock;
- if (substr($dot_lock, 0, 1) ne '/') {
- opendir(my $dh, '.') or
- die "opendir . $!";
- $self->{dh} = $dh;
- }
+ substr($dot_lock, 0, 1) eq '/' or
+ opendir($self->{dh}, '.');
return;
}
- unlink($tmp) or die "unlink($tmp): $!";
+ unlink($tmp);
select(undef, undef, undef, $self->{delay});
} else {
croak "open $tmp (for $dot_lock): $!" if !$!{EXIST};
@@ -83,18 +80,20 @@ sub acq_flock {
my $end = now + $self->{timeout};
do {
return if flock($self->{fh}, $op);
- select(undef, undef, undef, $self->{delay});
+ if ($!{EWOULDBLOCK}) {
+ select(undef, undef, undef, $self->{delay});
+ } elsif (!$!{EINTR}) {
+ croak "flock($self->{f} ($self->{fh}): $!";
+ }
} while (now < $end);
die "flock timeout $self->{f}: $!\n";
}
sub acq {
my ($cls, $f, $rw, $methods) = @_;
- my $fh;
- unless (open $fh, $rw ? '+>>' : '<', $f) {
- croak "open($f): $!" if $rw || !$!{ENOENT};
- }
- my $self = bless { f => $f, fh => $fh, rw => $rw }, $cls;
+ my $self = bless { f => $f, rw => $rw }, $cls;
+ my $ok = open $self->{fh}, $rw ? '+>>' : '<', $f;
+ croak "open($f): $!" if !$ok && ($rw || !$!{ENOENT});
my $m = "@$methods";
if ($m ne 'none') {
my @m = map {
@@ -116,20 +115,16 @@ sub acq {
$self;
}
-sub _fchdir { chdir($_[0]) } # OnDestroy callback
-
sub DESTROY {
my ($self) = @_;
- if (my $f = $self->{".lock$$"}) {
- my $x;
- if (my $dh = delete $self->{dh}) {
- opendir my $c, '.' or die "opendir . $!";
- $x = PublicInbox::OnDestroy->new(\&_fchdir, $c);
- chdir($dh) or die "chdir (for $f): $!";
- }
- unlink($f) or die "unlink($f): $! (lock stolen?)";
- undef $x;
+ my $f = $self->{".lock$$"} or return;
+ my $x;
+ if (my $dh = delete $self->{dh}) {
+ opendir my $c, '.';
+ $x = PublicInbox::OnDestroy->new(\&chdir, $c);
+ chdir($dh);
}
+ CORE::unlink($f) or die "unlink($f): $! (lock stolen?)";
}
1;
next prev 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 ` Eric Wong [this message]
2023-10-04 3:49 ` [PATCH 13/21] lock: retry on EINTR, improve error reporting Eric Wong
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-13-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).