Skip to content

Commit f0b0b1d

Browse files
committed
Tighten TAP tests' tracking of postmaster state some more.
Commits 6c4a890 et al. had a couple of deficiencies: * The logic I added to Cluster::start to see if a PID file is present could be fooled by a stale PID file left over from a previous postmaster. To fix, if we're not sure whether we expect to find a running postmaster or not, validate the PID using "kill 0". * 017_shm.pl has a loop in which it just issues repeated Cluster::start calls; this will fail if some invocation fails but leaves self->_pid set. Per buildfarm results, the above fix is not enough to make this safe: we might have "validated" a PID for a postmaster that exits immediately after we look. Hence, match each failed start call with a stop call that will get us back to the self->_pid == undef state. Add a fail_ok option to Cluster::stop to make this work. Discussion: https://postgr.es/m/CA+hUKGKV6fOHvfiPt8=dOKzvswjAyLoFoJF1iQXMNpi7+hD1JQ@mail.gmail.com
1 parent 22d8c25 commit f0b0b1d

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,20 +826,37 @@ Note: if the node is already known stopped, this does nothing.
826826
However, if we think it's running and it's not, it's important for
827827
this to fail. Otherwise, tests might fail to detect server crashes.
828828
829+
With optional extra param fail_ok => 1, returns 0 for failure
830+
instead of bailing out.
831+
829832
=cut
830833

831834
sub stop
832835
{
833-
my ($self, $mode) = @_;
834-
my $port = $self->port;
836+
my ($self, $mode, %params) = @_;
835837
my $pgdata = $self->data_dir;
836838
my $name = $self->name;
839+
my $ret;
837840
$mode = 'fast' unless defined $mode;
838-
return unless defined $self->{_pid};
841+
return 1 unless defined $self->{_pid};
842+
839843
print "### Stopping node \"$name\" using mode $mode\n";
840-
TestLib::system_or_bail('pg_ctl', '-D', $pgdata, '-m', $mode, 'stop');
844+
$ret = TestLib::system_log('pg_ctl', '-D', $pgdata,
845+
'-m', $mode, 'stop');
846+
847+
if ($ret != 0)
848+
{
849+
print "# pg_ctl stop failed: $ret\n";
850+
851+
# Check to see if we still have a postmaster or not.
852+
$self->_update_pid(-1);
853+
854+
BAIL_OUT("pg_ctl stop failed") unless $params{fail_ok};
855+
return 0;
856+
}
857+
841858
$self->_update_pid(0);
842-
return;
859+
return 1;
843860
}
844861

845862
=pod
@@ -1038,9 +1055,20 @@ sub _update_pid
10381055
if (open my $pidfile, '<', $self->data_dir . "/postmaster.pid")
10391056
{
10401057
chomp($self->{_pid} = <$pidfile>);
1041-
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
10421058
close $pidfile;
10431059

1060+
# If we aren't sure what to expect, validate the PID using kill().
1061+
# This protects against stale PID files left by crashed postmasters.
1062+
if ($is_running == -1 && kill(0, $self->{_pid}) == 0)
1063+
{
1064+
print
1065+
"# Stale postmaster.pid file for node \"$name\": PID $self->{_pid} no longer exists\n";
1066+
$self->{_pid} = undef;
1067+
return;
1068+
}
1069+
1070+
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
1071+
10441072
# If we found a pidfile when there shouldn't be one, complain.
10451073
BAIL_OUT("postmaster.pid unexpectedly present") if $is_running == 0;
10461074
return;

src/test/recovery/t/017_shm.pl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ sub poll_start
196196
# Wait 0.1 second before retrying.
197197
usleep(100_000);
198198

199+
# Clean up in case the start attempt just timed out or some such.
200+
$node->stop('fast', fail_ok => 1);
201+
199202
$attempts++;
200203
}
201204

0 commit comments

Comments
 (0)