Skip to content

Commit cf680bd

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 156a846 commit cf680bd

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
@@ -863,23 +863,40 @@ Note: if the node is already known stopped, this does nothing.
863863
However, if we think it's running and it's not, it's important for
864864
this to fail. Otherwise, tests might fail to detect server crashes.
865865
866+
With optional extra param fail_ok => 1, returns 0 for failure
867+
instead of bailing out.
868+
866869
=cut
867870

868871
sub stop
869872
{
870-
my ($self, $mode) = @_;
871-
my $port = $self->port;
873+
my ($self, $mode, %params) = @_;
872874
my $pgdata = $self->data_dir;
873875
my $name = $self->name;
876+
my $ret;
874877

875878
local %ENV = $self->_get_env();
876879

877880
$mode = 'fast' unless defined $mode;
878-
return unless defined $self->{_pid};
881+
return 1 unless defined $self->{_pid};
882+
879883
print "### Stopping node \"$name\" using mode $mode\n";
880-
TestLib::system_or_bail('pg_ctl', '-D', $pgdata, '-m', $mode, 'stop');
884+
$ret = TestLib::system_log('pg_ctl', '-D', $pgdata,
885+
'-m', $mode, 'stop');
886+
887+
if ($ret != 0)
888+
{
889+
print "# pg_ctl stop failed: $ret\n";
890+
891+
# Check to see if we still have a postmaster or not.
892+
$self->_update_pid(-1);
893+
894+
BAIL_OUT("pg_ctl stop failed") unless $params{fail_ok};
895+
return 0;
896+
}
897+
881898
$self->_update_pid(0);
882-
return;
899+
return 1;
883900
}
884901

885902
=pod
@@ -1107,9 +1124,20 @@ sub _update_pid
11071124
if (open my $pidfile, '<', $self->data_dir . "/postmaster.pid")
11081125
{
11091126
chomp($self->{_pid} = <$pidfile>);
1110-
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
11111127
close $pidfile;
11121128

1129+
# If we aren't sure what to expect, validate the PID using kill().
1130+
# This protects against stale PID files left by crashed postmasters.
1131+
if ($is_running == -1 && kill(0, $self->{_pid}) == 0)
1132+
{
1133+
print
1134+
"# Stale postmaster.pid file for node \"$name\": PID $self->{_pid} no longer exists\n";
1135+
$self->{_pid} = undef;
1136+
return;
1137+
}
1138+
1139+
print "# Postmaster PID for node \"$name\" is $self->{_pid}\n";
1140+
11131141
# If we found a pidfile when there shouldn't be one, complain.
11141142
BAIL_OUT("postmaster.pid unexpectedly present") if $is_running == 0;
11151143
return;

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

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

210+
# Clean up in case the start attempt just timed out or some such.
211+
$node->stop('fast', fail_ok => 1);
212+
210213
$attempts++;
211214
}
212215

0 commit comments

Comments
 (0)