Skip to content

Commit 4828a80

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 3168073 commit 4828a80

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
@@ -831,20 +831,37 @@ Note: if the node is already known stopped, this does nothing.
831831
However, if we think it's running and it's not, it's important for
832832
this to fail. Otherwise, tests might fail to detect server crashes.
833833
834+
With optional extra param fail_ok => 1, returns 0 for failure
835+
instead of bailing out.
836+
834837
=cut
835838

836839
sub stop
837840
{
838-
my ($self, $mode) = @_;
839-
my $port = $self->port;
841+
my ($self, $mode, %params) = @_;
840842
my $pgdata = $self->data_dir;
841843
my $name = $self->name;
844+
my $ret;
842845
$mode = 'fast' unless defined $mode;
843-
return unless defined $self->{_pid};
846+
return 1 unless defined $self->{_pid};
847+
844848
print "### Stopping node \"$name\" using mode $mode\n";
845-
TestLib::system_or_bail('pg_ctl', '-D', $pgdata, '-m', $mode, 'stop');
849+
$ret = TestLib::system_log('pg_ctl', '-D', $pgdata,
850+
'-m', $mode, 'stop');
851+
852+
if ($ret != 0)
853+
{
854+
print "# pg_ctl stop failed: $ret\n";
855+
856+
# Check to see if we still have a postmaster or not.
857+
$self->_update_pid(-1);
858+
859+
BAIL_OUT("pg_ctl stop failed") unless $params{fail_ok};
860+
return 0;
861+
}
862+
846863
$self->_update_pid(0);
847-
return;
864+
return 1;
848865
}
849866

850867
=pod
@@ -1066,9 +1083,20 @@ sub _update_pid
10661083
if (open my $pidfile, '<', $self->data_dir . "/postmaster.pid")
10671084
{
10681085
chomp($self->{_pid} = <$pidfile>);
1069-
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
10701086
close $pidfile;
10711087

1088+
# If we aren't sure what to expect, validate the PID using kill().
1089+
# This protects against stale PID files left by crashed postmasters.
1090+
if ($is_running == -1 && kill(0, $self->{_pid}) == 0)
1091+
{
1092+
print
1093+
"# Stale postmaster.pid file for node \"$name\": PID $self->{_pid} no longer exists\n";
1094+
$self->{_pid} = undef;
1095+
return;
1096+
}
1097+
1098+
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
1099+
10721100
# If we found a pidfile when there shouldn't be one, complain.
10731101
BAIL_OUT("postmaster.pid unexpectedly present") if $is_running == 0;
10741102
return;

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

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

207+
# Clean up in case the start attempt just timed out or some such.
208+
$node->stop('fast', fail_ok => 1);
209+
207210
$attempts++;
208211
}
209212

0 commit comments

Comments
 (0)