pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Daniel Gustafsson <daniel@yesql.se>
Cc: Fabien COELHO <coelho@cri.ensmp.fr>
Cc: pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: psql: Add test for query canceling
Date: Tue, 24 Aug 2021 18:43:38 +0200
Message-ID: <9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com> (raw)
In-Reply-To: <1957503.1629485278@sss.pgh.pa.us>
References: <E1mH14Q-0002gh-HS@gemulon.postgresql.org>
	<DD279DFC-DD59-458E-B469-75DC941765A4@yesql.se>
	<alpine.DEB.2.22.394.2108201736060.498998@pseudo>
	<5F16EBAA-30D3-4C0D-840C-C2FF2739E370@yesql.se>
	<1957503.1629485278@sss.pgh.pa.us>

On 20.08.21 20:47, Tom Lane wrote:
> I think you should drop the overly-cute bit with a SIGALRM handler,
> and instead have a loop-with-delay around an attempt to read the
> psql.pid file, after launching the psql run without an immediate
> wait for termination.  That gets rid of the first problem (though
> you still want the loop to timeout eventually, it could wait up
> to say 180 seconds, as we do elsewhere).  Then the second problem
> is easy to solve by making the pg_sleep delay twice as much.

Here is a proposal.  It waits separately for the pid file to appear and 
also checks for the sleep query to be registered by the backend, so it 
doesn't have any more dependencies on things happening "fast enough". 
And it's also faster in the normal case now.  Thoughts?

Attachments:

0001-Make-psql-cancel-test-more-timing-robust.patch (text/plain, 2.3K) download From 4fba5b1e1b6f61acb3508b90825c668a8c6bf70c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter@eisentraut.org> Date: Tue, 24 Aug 2021 18:24:17 +0200 Subject: [PATCH] Make psql cancel test more timing robust --- src/bin/psql/t/020_cancel.pl | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/bin/psql/t/020_cancel.pl b/src/bin/psql/t/020_cancel.pl index 7713fff8e4..0319f6b3f1 100644 --- a/src/bin/psql/t/020_cancel.pl +++ b/src/bin/psql/t/020_cancel.pl @@ -7,6 +7,7 @@ use PostgresNode; use TestLib; use Test::More tests => 2; +use Time::HiRes qw(usleep); my $tempdir = TestLib::tempdir; @@ -28,19 +29,33 @@ my ($stdin, $stdout, $stderr); # Test whether shell supports $PPID. It's part of POSIX, but some - # pre-/non-POSIX shells don't support it (e.g., NetBSD, Solaris). + # pre-/non-POSIX shells don't support it (e.g., NetBSD). $stdin = "\\! echo \$PPID"; IPC::Run::run(['psql', '-X', '-v', 'ON_ERROR_STOP=1'], '<', \$stdin, '>', \$stdout, '2>', \$stderr); $stdout =~ /^\d+$/ or skip "shell apparently does not support \$PPID", 2; - local $SIG{ALRM} = sub { - my $psql_pid = TestLib::slurp_file("$tempdir/psql.pid"); - kill 'INT', $psql_pid; - }; - alarm 1; + # Now start the real test + my $h = IPC::Run::start(['psql', '-X', '-v', 'ON_ERROR_STOP=1'], \$stdin, \$stdout, \$stderr); - $stdin = "\\! echo \$PPID >$tempdir/psql.pid\nselect pg_sleep(3);"; - my $result = IPC::Run::run(['psql', '-X', '-v', 'ON_ERROR_STOP=1'], '<', \$stdin, '>', \$stdout, '2>', \$stderr); + # Get the PID + $stdout = ''; + $stderr = ''; + $stdin = "\\! echo \$PPID >$tempdir/psql.pid\n"; + pump $h while length $stdin; + my $count; + usleep(10_000) until -s "$tempdir/psql.pid" or ($count++ > 180 * 100 and die 'pid file did not appear'); + my $psql_pid = TestLib::slurp_file("$tempdir/psql.pid"); + + # Send sleep command and wait until the server has registered it + $stdin = "select pg_sleep(180);\n"; + pump $h while length $stdin; + $node->poll_query_until('postgres', q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ 'pg_sleep') > 0;}) + or die "timed out"; + + # Send cancel request + kill 'INT', $psql_pid; + + my $result = finish $h; ok(!$result, 'query failed as expected'); like($stderr, qr/canceling statement due to user request/, 'query was canceled'); -- 2.33.0

view thread (20+ messages)  latest in thread

Message-ID: <9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com>
Permalink:  ../9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com/
Also on:    postgresql.org/message-id/9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: peter.eisentraut@enterprisedb.com, tgl@sss.pgh.pa.us, daniel@yesql.se, coelho@cri.ensmp.fr
  Subject: Re: pgsql: psql: Add test for query canceling
  In-Reply-To: <9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com>
  References: <E1mH14Q-0002gh-HS@gemulon.postgresql.org> <DD279DFC-DD59-458E-B469-75DC941765A4@yesql.se> <alpine.DEB.2.22.394.2108201736060.498998@pseudo> <5F16EBAA-30D3-4C0D-840C-C2FF2739E370@yesql.se> <1957503.1629485278@sss.pgh.pa.us> <9f46b102-da4d-8312-54f0-1f3bc10b453c@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox