user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 2/2] lei: fix output race in client/daemon mode
  2021-01-03 11:24  5% ` [PATCH 0/2] fix race from stdout buffering in FD pass exit Eric Wong
@ 2021-01-03 11:24  7%   ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2021-01-03 11:24 UTC (permalink / raw)
  To: meta

The daemon needs to flush stdout before disconnecting or killing
clients, otherwise they may reread empty data on redirected
outputs.  We also don't want to unbuffer stdout too early in
case we have lots of small chunks of data to output.

The received ($self->{2}) will always have autoflush, matching normal
STDERR behavior.
---
 lib/PublicInbox/LEI.pm | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 3ad5e01a..6f21da35 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -236,6 +236,7 @@ my %CONFIG_KEYS = (
 
 sub x_it ($$) { # pronounced "exit"
 	my ($self, $code) = @_;
+	$self->{1}->autoflush(1); # make sure client sees stdout before exit
 	if (my $sig = ($code & 127)) {
 		kill($sig, $self->{pid} // $$);
 	} else {
@@ -635,6 +636,7 @@ sub accept_dispatch { # Listener {post_accept} callback
 		say $sock "timed out waiting to recv FDs";
 		return;
 	}
+	$self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
 	# $ARGV_STR = join("]\0[", @ARGV);
 	# $ENV_STR = join('', map { "$_=$ENV{$_}\0" } keys %ENV);
 	# $line = "$$\0\0>$ARGV_STR\0\0>$ENV_STR\0\0";
@@ -773,4 +775,8 @@ sub oneshot {
 	}, __PACKAGE__), @ARGV);
 }
 
+# ensures stdout hits the FS before sock disconnects so a client
+# can immediately reread it
+sub DESTROY { $_[0]->{1}->autoflush(1) }
+
 1;

^ permalink raw reply related	[relevance 7%]

* [PATCH 0/2] fix race from stdout buffering in FD pass exit
  @ 2021-01-03 11:24  5% ` Eric Wong
  2021-01-03 11:24  7%   ` [PATCH 2/2] lei: fix output race in client/daemon mode Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2021-01-03 11:24 UTC (permalink / raw)
  To: meta

I implemented 1/2 thinking in hopes it would help, but it's
a nice syscall reduction anyways.

2/2 is the actual fix I've been struggling to find, and I could
only reproduce it on this FreeBSD VM I have access to.  It seems
so obvious in retrospect :x

Eric Wong (2):
  send and receive all 3 FDs at once
  lei: fix output race in client/daemon mode

 lib/PublicInbox/LEI.pm   | 33 +++++++++++++++----------
 lib/PublicInbox/Spawn.pm | 53 ++++++++++++++++++++++++++--------------
 script/lei               |  6 ++---
 t/lei.t                  |  2 +-
 t/spawn.t                | 33 ++++++++++++++++---------
 5 files changed, 80 insertions(+), 47 deletions(-)

^ permalink raw reply	[relevance 5%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2021-01-03  9:48     [PATCH 0/3] lei-related test fixes Eric Wong
2021-01-03 11:24  5% ` [PATCH 0/2] fix race from stdout buffering in FD pass exit Eric Wong
2021-01-03 11:24  7%   ` [PATCH 2/2] lei: fix output race in client/daemon mode Eric Wong

Code repositories for project(s) associated with this public inbox

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

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).