user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/2] ipc: fix "lei q" w/ HTTP(S) externals
@ 2021-05-25 22:19 Eric Wong
  2021-05-25 22:20 ` [PATCH 1/2] ipc: avoid potential stack-not-refcounted bug Eric Wong
  2021-05-25 22:20 ` [PATCH 2/2] ipc: wq: handle >MAX_ARG_STRLEN && <EMSGSIZE case Eric Wong
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Wong @ 2021-05-25 22:19 UTC (permalink / raw)
  To: meta

1/2 was found while making 2/2

Eric Wong (2):
  ipc: avoid potential stack-not-refcounted bug
  ipc: wq: handle >MAX_ARG_STRLEN && <EMSGSIZE case

 lib/PublicInbox/IPC.pm      | 47 +++++++++++++++++++++++--------------
 lib/PublicInbox/WQWorker.pm |  2 +-
 t/ipc.t                     | 11 ++++++---
 3 files changed, 38 insertions(+), 22 deletions(-)

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

* [PATCH 1/2] ipc: avoid potential stack-not-refcounted bug
  2021-05-25 22:19 [PATCH 0/2] ipc: fix "lei q" w/ HTTP(S) externals Eric Wong
@ 2021-05-25 22:20 ` Eric Wong
  2021-05-25 22:20 ` [PATCH 2/2] ipc: wq: handle >MAX_ARG_STRLEN && <EMSGSIZE case Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2021-05-25 22:20 UTC (permalink / raw)
  To: meta

This fixes a potential problem with Carp::longmess
firing somewhere deeper in the stack.  This is not a known
problem at this time, but something I noticed while chasing
something else.
---
 lib/PublicInbox/IPC.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm
index cf3723f7..24237773 100644
--- a/lib/PublicInbox/IPC.pm
+++ b/lib/PublicInbox/IPC.pm
@@ -251,7 +251,7 @@ sub wq_worker_loop ($) {
 
 sub do_sock_stream { # via wq_io_do, for big requests
 	my ($self, $len) = @_;
-	recv_and_run($self, delete $self->{0}, $len, 1);
+	recv_and_run($self, my $s2 = delete $self->{0}, $len, 1);
 }
 
 sub wq_broadcast {

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

* [PATCH 2/2] ipc: wq: handle >MAX_ARG_STRLEN && <EMSGSIZE case
  2021-05-25 22:19 [PATCH 0/2] ipc: fix "lei q" w/ HTTP(S) externals Eric Wong
  2021-05-25 22:20 ` [PATCH 1/2] ipc: avoid potential stack-not-refcounted bug Eric Wong
@ 2021-05-25 22:20 ` Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2021-05-25 22:20 UTC (permalink / raw)
  To: meta

WQWorkers are limited roughly to MAX_ARG_STRLEN (the kernel
limit of argv + environ) to avoid excessive memory growth.
Occasionally, we need to send larger messages via workqueues
that are too small to hit EMSGSIZE on the sender.

This fixes "lei q" when using HTTP(S) externals, since that
code path sends large Eml objects from lei_xsearch workers
directly to lei2mail WQ workers.
---
 lib/PublicInbox/IPC.pm      | 45 +++++++++++++++++++++++--------------
 lib/PublicInbox/WQWorker.pm |  2 +-
 t/ipc.t                     | 11 ++++++---
 3 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm
index 24237773..497a6035 100644
--- a/lib/PublicInbox/IPC.pm
+++ b/lib/PublicInbox/IPC.pm
@@ -17,6 +17,7 @@ use PublicInbox::Spawn;
 use PublicInbox::OnDestroy;
 use PublicInbox::WQWorker;
 use Socket qw(AF_UNIX MSG_EOR SOCK_STREAM);
+my $MY_MAX_ARG_STRLEN = 4096 * 33; # extra 4K for serialization
 my $SEQPACKET = eval { Socket::SOCK_SEQPACKET() }; # portable enough?
 our @EXPORT_OK = qw(ipc_freeze ipc_thaw);
 my $WQ_MAX_WORKERS = 4096;
@@ -213,7 +214,7 @@ sub ipc_sibling_atfork_child {
 
 sub recv_and_run {
 	my ($self, $s2, $len, $full_stream) = @_;
-	my @fds = $recv_cmd->($s2, my $buf, $len);
+	my @fds = $recv_cmd->($s2, my $buf, $len // $MY_MAX_ARG_STRLEN);
 	return if scalar(@fds) && !defined($fds[0]);
 	my $n = length($buf) or return 0;
 	my $nfd = 0;
@@ -268,27 +269,37 @@ sub wq_broadcast {
 	}
 }
 
+sub stream_in_full ($$$) {
+	my ($s1, $fds, $buf) = @_;
+	socketpair(my $r, my $w, AF_UNIX, SOCK_STREAM, 0) or
+		croak "socketpair: $!";
+	my $n = $send_cmd->($s1, [ fileno($r) ],
+			ipc_freeze(['do_sock_stream', length($buf)]),
+			MSG_EOR) // croak "sendmsg: $!";
+	undef $r;
+	$n = $send_cmd->($w, $fds, $buf, 0) // croak "sendmsg: $!";
+	while ($n < length($buf)) {
+		my $x = syswrite($w, $buf, length($buf) - $n, $n) //
+				croak "syswrite: $!";
+		croak "syswrite wrote 0 bytes" if $x == 0;
+		$n += $x;
+	}
+}
+
 sub wq_io_do { # always async
 	my ($self, $sub, $ios, @args) = @_;
 	if (my $s1 = $self->{-wq_s1}) { # run in worker
 		my $fds = [ map { fileno($_) } @$ios ];
 		my $buf = ipc_freeze([$sub, @args]);
-		my $n = $send_cmd->($s1, $fds, $buf, MSG_EOR);
-		return if defined($n); # likely
-		croak "sendmsg: $! (check RLIMIT_NOFILE)" if $!{ETOOMANYREFS};
-		croak "sendmsg: $!" if !$!{EMSGSIZE};
-		socketpair(my $r, my $w, AF_UNIX, SOCK_STREAM, 0) or
-			croak "socketpair: $!";
-		$n = $send_cmd->($s1, [ fileno($r) ],
-				ipc_freeze(['do_sock_stream', length($buf)]),
-				MSG_EOR) // croak "sendmsg: $!";
-		undef $r;
-		$n = $send_cmd->($w, $fds, $buf, 0) // croak "sendmsg: $!";
-		while ($n < length($buf)) {
-			my $x = syswrite($w, $buf, length($buf) - $n, $n) //
-					croak "syswrite: $!";
-			croak "syswrite wrote 0 bytes" if $x == 0;
-			$n += $x;
+		if (length($buf) > $MY_MAX_ARG_STRLEN) {
+			stream_in_full($s1, $fds, $buf);
+		} else {
+			my $n = $send_cmd->($s1, $fds, $buf, MSG_EOR);
+			return if defined($n); # likely
+			$!{ETOOMANYREFS} and
+				croak "sendmsg: $! (check RLIMIT_NOFILE)";
+			$!{EMSGSIZE} ? stream_in_full($s1, $fds, $buf) :
+			croak("sendmsg: $!");
 		}
 	} else {
 		@$self{0..$#$ios} = @$ios;
diff --git a/lib/PublicInbox/WQWorker.pm b/lib/PublicInbox/WQWorker.pm
index 3636321e..f7aa61c5 100644
--- a/lib/PublicInbox/WQWorker.pm
+++ b/lib/PublicInbox/WQWorker.pm
@@ -23,7 +23,7 @@ sub event_step {
 	my ($self) = @_;
 	my $n;
 	do {
-		$n = $self->{wq}->recv_and_run($self->{sock}, 4096 * 33);
+		$n = $self->{wq}->recv_and_run($self->{sock});
 	} while ($n);
 	return if !defined($n) && $! == EAGAIN; # likely
 	warn "wq worker error: $!\n" if !defined($n) && $! != ECONNRESET;
diff --git a/t/ipc.t b/t/ipc.t
index ca88eb59..7983fdc0 100644
--- a/t/ipc.t
+++ b/t/ipc.t
@@ -122,11 +122,16 @@ for my $t ('local', 'worker', 'worker again') {
 	$ipc->wq_io_do('test_sha', [ $wa, $wb ], 'hello world');
 	is(readline($rb), sha1_hex('hello world')."\n", "SHA small ($t)");
 	{
-		my $bigger = $big x 10;
+		my $bigger = $big x 10; # to hit EMSGSIZE
 		$ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
 		my $exp = sha1_hex($bigger)."\n";
-		undef $bigger;
-		is(readline($rb), $exp, "SHA big ($t)");
+		is(readline($rb), $exp, "SHA big for EMSGSIZE ($t)");
+
+		# to hit the WQWorker recv_and_run length
+		substr($bigger, my $MY_MAX_ARG_STRLEN = 4096 * 33, -1) = '';
+		$ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
+		$exp = sha1_hex($bigger)."\n";
+		is(readline($rb), $exp, "SHA WQWorker limit ($t)");
 	}
 	my $ppid = $ipc->wq_workers_start('wq', 1);
 	push(@ppids, $ppid);

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

end of thread, other threads:[~2021-05-25 22:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-25 22:19 [PATCH 0/2] ipc: fix "lei q" w/ HTTP(S) externals Eric Wong
2021-05-25 22:20 ` [PATCH 1/2] ipc: avoid potential stack-not-refcounted bug Eric Wong
2021-05-25 22:20 ` [PATCH 2/2] ipc: wq: handle >MAX_ARG_STRLEN && <EMSGSIZE case Eric Wong

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.mail.public-inbox.meta
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.mail.public-inbox.meta
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git