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 07/13] net: retry on EINTR and check for {quit} flag
Date: Thu,  9 Nov 2023 10:09:40 +0000	[thread overview]
Message-ID: <20231109100946.1440611-8-e@80x24.org> (raw)
In-Reply-To: <20231109100946.1440611-1-e@80x24.org>

This should allow us to detect shutdown signals in -watch
more quickly and not unnecessarily fail on inconsequential
signals such as SIGWINCH.
---
 lib/PublicInbox/NetNNTPSocks.pm |  1 +
 lib/PublicInbox/NetReader.pm    | 53 +++++++++++++++++++++++----------
 lib/PublicInbox/Watch.pm        |  2 +-
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/NetNNTPSocks.pm b/lib/PublicInbox/NetNNTPSocks.pm
index 306dcacb..d27efba1 100644
--- a/lib/PublicInbox/NetNNTPSocks.pm
+++ b/lib/PublicInbox/NetNNTPSocks.pm
@@ -19,6 +19,7 @@ sub new_socks {
 	} qw(ProxyAddr ProxyPort SocksVersion SocksDebug SocksResolve);
 	no warnings 'uninitialized'; # needed for $SOCKS_ERROR
 	my $ret = Net::NNTP->new(%opt); # calls PublicInbox::NetNNTPSocks::new
+	return $ret if $ret || $!{EINTR};
 	$ret // die "errors: \$!=$! SOCKS=",
 				eval('$IO::Socket::Socks::SOCKS_ERROR // ""'),
 				', SSL=',
diff --git a/lib/PublicInbox/NetReader.pm b/lib/PublicInbox/NetReader.pm
index 95cf1de8..e3e5d596 100644
--- a/lib/PublicInbox/NetReader.pm
+++ b/lib/PublicInbox/NetReader.pm
@@ -60,6 +60,7 @@ sub mic_new ($$$$) {
 	my ($self, $mic_arg, $sec, $uri) = @_;
 	my %mic_arg = (%$mic_arg, Keepalive => 1);
 	my $sa = $self->{cfg_opt}->{$sec}->{-proxy_cfg} || $self->{-proxy_cli};
+	my ($mic, $s, $t);
 	if ($sa) {
 		# this `require' needed for worker[1..Inf], since socks_args
 		# only got called in worker[0]
@@ -68,24 +69,37 @@ sub mic_new ($$$$) {
 		$opt{SocksDebug} = 1 if $mic_arg{Debug};
 		$opt{ConnectAddr} = delete $mic_arg{Server};
 		$opt{ConnectPort} = delete $mic_arg{Port};
-		my $s = IO::Socket::Socks->new(%opt) or die
-			"E: <$uri> ".eval('$IO::Socket::Socks::SOCKS_ERROR');
+		do {
+			$! = 0;
+			$s = IO::Socket::Socks->new(%opt);
+		} until ($s || !$!{EINTR} || $self->{quit});
+		return if $self->{quit};
+		$s or die "E: <$uri> ".eval('$IO::Socket::Socks::SOCKS_ERROR');
+		$mic_arg{Socket} = $s;
 		if (my $o = delete $mic_arg{Ssl}) { # for imaps://
 			$o = mic_tls_opt($o, $opt{ConnectAddr});
-			$s = IO::Socket::SSL->start_SSL($s, @$o) or die
-				"E: <$uri> ".(IO::Socket::SSL->errstr // '');
+			do {
+				$! = 0;
+				$t = IO::Socket::SSL->start_SSL($s, @$o);
+			} until ($t || !$!{EINTR} || $self->{quit});
+			return if $self->{quit};
+			$t or die "E: <$uri> ".(IO::Socket::SSL->errstr // '');
+			$mic_arg{Socket} = $t;
 		} elsif ($o = $mic_arg{Starttls}) {
 			# Mail::IMAPClient will use this:
 			$mic_arg{Starttls} = mic_tls_opt($o, $opt{ConnectAddr});
 		}
-		$mic_arg{Socket} = $s;
 	} elsif ($mic_arg{Ssl} || $mic_arg{Starttls}) {
 		for my $f (qw(Ssl Starttls)) {
 			my $o = $mic_arg{$f} or next;
 			$mic_arg{$f} = mic_tls_opt($o, $mic_arg{Server});
 		}
 	}
-	PublicInbox::IMAPClient->new(%mic_arg);
+	do {
+		$! = 0;
+		$mic = PublicInbox::IMAPClient->new(%mic_arg);
+	} until ($mic || !$!{EINTR} || $self->{quit});
+	$mic;
 }
 
 sub auth_anon_cb { '' }; # for Mail::IMAPClient::Authcallback
@@ -152,6 +166,7 @@ sub mic_for ($$$$) { # mic = Mail::IMAPClient
 	};
 	require PublicInbox::IMAPClient;
 	my $mic = mic_new($self, $mic_arg, $sec, $uri);
+	return if $self->{quit};
 	($mic && $mic->IsConnected) or
 		die "E: <$uri> new: $@".onion_hint($lei, $uri);
 
@@ -202,16 +217,20 @@ sub mic_for ($$$$) { # mic = Mail::IMAPClient
 	$mic;
 }
 
-sub nn_new ($$$) {
-	my ($nn_arg, $nntp_cfg, $uri) = @_;
+sub nn_new ($$$$) {
+	my ($self, $nn_arg, $nntp_cfg, $uri) = @_;
 	my $nn;
+	my ($Net_NNTP, $new) = qw(Net::NNTP new);
 	if (defined $nn_arg->{ProxyAddr}) {
 		require PublicInbox::NetNNTPSocks;
+		($Net_NNTP, $new) = qw(PublicInbox::NetNNTPSocks new_socks);
 		$nn_arg->{SocksDebug} = 1 if $nn_arg->{Debug};
-		$nn = PublicInbox::NetNNTPSocks->new_socks(%$nn_arg) or return;
-	} else {
-		$nn = Net::NNTP->new(%$nn_arg) or return;
 	}
+	do {
+		$! = 0;
+		$nn = $Net_NNTP->$new(%$nn_arg);
+	} until ($nn || !$!{EINTR} || $self->{quit});
+	$nn // return;
 	setsockopt($nn, Socket::SOL_SOCKET(), Socket::SO_KEEPALIVE(), 1);
 
 	# default to using STARTTLS if it's available, but allow
@@ -268,8 +287,9 @@ sub nn_for ($$$$) { # nn = Net::NNTP
 	$nn_arg->{SSL} = 1 if $uri->secure; # snews == nntps
 	my $sa = $self->{-proxy_cli};
 	%$nn_arg = (%$nn_arg, %$sa) if $sa;
-	my $nn = nn_new($nn_arg, $nntp_cfg, $uri) or
-		die "E: <$uri> new: $@".onion_hint($lei, $uri);
+	my $nn = nn_new($self, $nn_arg, $nntp_cfg, $uri);
+	return if $self->{quit};
+	$nn // die "E: <$uri> new: $@".onion_hint($lei, $uri);
 	if ($cred) {
 		$cred->fill($lei) unless defined($p); # may prompt user here
 		if ($nn->authinfo($u, $p)) {
@@ -382,8 +402,9 @@ sub imap_common_init ($;$) {
 		my $sec = uri_section($orig_uri);
 		my $uri = PublicInbox::URIimap->new("$sec/");
 		my $mic = $mics->{$sec} //=
-				mic_for($self, $uri, $mic_common, $lei) //
-				die "Unable to continue\n";
+				mic_for($self, $uri, $mic_common, $lei);
+		return if $self->{quit};
+		$mic // die "Unable to continue\n";
 		next unless $self->isa('PublicInbox::NetWriter');
 		next if $self->{-skip_creat};
 		my $dst = $orig_uri->mailbox // next;
@@ -751,7 +772,7 @@ sub nn_get {
 	my $nn_arg = $self->{net_arg}->{$sec} or
 			die "BUG: no Net::NNTP->new arg for $sec";
 	my $nntp_cfg = $self->{cfg_opt}->{$sec};
-	$nn = nn_new($nn_arg, $nntp_cfg, $uri) or return;
+	$nn = nn_new($self, $nn_arg, $nntp_cfg, $uri) or return;
 	if (my $postconn = $nntp_cfg->{-postconn}) {
 		for my $m_arg (@$postconn) {
 			my ($method, @args) = @$m_arg;
diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm
index c2b1312a..1cdf12a5 100644
--- a/lib/PublicInbox/Watch.pm
+++ b/lib/PublicInbox/Watch.pm
@@ -494,7 +494,7 @@ sub poll_fetch_reap { # awaitpid callback
 
 sub watch_imap_init ($$) {
 	my ($self, $poll) = @_;
-	my $mics = PublicInbox::NetReader::imap_common_init($self);
+	my $mics = PublicInbox::NetReader::imap_common_init($self) or return;
 	my $idle = []; # [ uri1, intvl1, uri2, intvl2 ]
 	for my $uri (@{$self->{imap_order}}) {
 		my $sec = uri_section($uri);

  parent reply	other threads:[~2023-11-09 10:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 10:09 [PATCH 00/13] misc error handling stuff and simplifications Eric Wong
2023-11-09 10:09 ` [PATCH 01/13] lei_xsearch: put query in process title for debugging Eric Wong
2023-11-09 10:09 ` [PATCH 02/13] lei: use cached $daemon_pid when possible Eric Wong
2023-11-09 10:09 ` [PATCH 03/13] lei: reuse FDs atfork and close explicitly Eric Wong
2023-11-09 10:09 ` [PATCH 04/13] lei_up: use v5.12 Eric Wong
2023-11-09 10:09 ` [PATCH 05/13] net_nntp_socks: more comments around how it works Eric Wong
2023-11-09 10:09 ` [PATCH 06/13] lei ls-mail-source: gracefully handle network failures Eric Wong
2023-11-09 10:09 ` Eric Wong [this message]
2023-11-09 10:09 ` [PATCH 08/13] lei_mirror: note missing local manifests are non-fatal Eric Wong
2023-11-09 10:09 ` [PATCH 09/13] ipc: simplify partial sendmsg fallback Eric Wong
2023-11-09 10:09 ` [PATCH 10/13] lei_input: always close single `eml' inputs Eric Wong
2023-11-09 10:09 ` [PATCH 11/13] xapcmd: get rid of scalar wantarray popen_rd Eric Wong
2023-11-09 10:09 ` [PATCH 12/13] lei: get rid of autoreap usage Eric Wong
2023-11-09 10:09 ` [PATCH 13/13] spawn: get rid of wantarray popen_rd/popen_wr 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=20231109100946.1440611-8-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).