From a21a35d6567251d5a7335ed81d09677e7a670fee Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Mon, 15 Jun 2020 20:40:22 -0700 Subject: [PATCH] Improve recovery scenario on poorly formatted queries. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because the client defaulted to dying, this left the server in a strange state as the query question wasn't cleared and the “fake failure” named pipe was never opened and processed, since it couldn't be. The server did eventually recover when the client fully died; however, the client has enough information to give up its query when it knows failure has occurred, so that now happens. --- bean-query-daemon-lib.pl | 8 ++++++-- bean-query-goofy-daemon.plx | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bean-query-daemon-lib.pl b/bean-query-daemon-lib.pl index ef3eafa..ad4eb1b 100644 --- a/bean-query-daemon-lib.pl +++ b/bean-query-daemon-lib.pl @@ -30,8 +30,12 @@ sub BeancountQuerySubmit($;$) { $BEANCOUNT_QUERY{format} = $format if defined $format; (tied %BEANCOUNT_QUERY)->shunlock; while (not defined $BEANCOUNT_QUERY{fifoName}) { sleep 1; } - die "Ceci n'est pas une pipe: BEANCOUNT_QUERY{fifoName}, $BEANCOUNT_QUERY{fifoName}:$!" - unless (-p $BEANCOUNT_QUERY{fifoName}); + unless (-p $BEANCOUNT_QUERY{fifoName}) { + (tied %BEANCOUNT_QUERY)->shlock; + $BEANCOUNT_QUERY{question} = $BEANCOUNT_QUERY{format} = undef; + (tied %BEANCOUNT_QUERY)->shunlock; + die "Ceci n'est pas une pipe: BEANCOUNT_QUERY{fifoName}, $BEANCOUNT_QUERY{fifoName}:$!" + } (tied %BEANCOUNT_QUERY)->shlock; return $BEANCOUNT_QUERY{fifoName}; } diff --git a/bean-query-goofy-daemon.plx b/bean-query-goofy-daemon.plx index 3bd2bea..8cfef8c 100755 --- a/bean-query-goofy-daemon.plx +++ b/bean-query-goofy-daemon.plx @@ -77,7 +77,7 @@ while (1) { (tied %query)->shlock; print STDERR "clearing fifo, $query{fifoName}..." if $VERBOSE > 2; no autodie 'unlink'; unlink($query{fifoName}); - $query{fifoName} = undef; + %query = (); (tied %query)->shunlock; print STDERR "fifo cleared & lock released." if $VERBOSE > 2; } @@ -87,9 +87,9 @@ while (1) { } elsif ($query{question} !~ /^[\-\@\w.\s\"\'\_\(\)]+$/) { print STDERR "Query string $query{question} looks suspicious, not running beancount query!\n"; (tied %query)->shlock; - $query{question} = $query{format} = undef; $query{fifoName} = mktemp("REJECTED_beancount-query-fifo-this-file-does-not-exist_${$}_XXXXXXXXX"); (tied %query)->shunlock; + sleep 2; } elsif (defined $query{format} and $query{format} !~ /^(?:csv|text)$/) { print STDERR "format string $query{format} is not text or csv, not running beancount query!\n"; (tied %query)->shlock; @@ -100,7 +100,7 @@ while (1) { my @cmd = ($BEANCOUNT_QUERY_CMD); push(@cmd, '-f', $query{format}) if defined $query{format}; push(@cmd, $LOAD_FILE, $query{question}); - print STDERR "Running query: $query{question}\n" if $VERBOSE > 0; + print STDERR "Runing query: $query{question}\n" if $VERBOSE > 0; open(my $beancountFH, "-|", @cmd); print STDERR "Running ", join(" ", @cmd), "\n" if $VERBOSE > 1; (tied %query)->shlock;