Skip to content

Commit 870d621

Browse files
committed
Prevent port collisions between concurrent TAP tests
Currently there is a race condition where if concurrent TAP tests both test that they can open a port they will assume that it is free and use it, causing one of them to fail. To prevent this we record a reservation using an exclusive lock, and any TAP test that discovers a reservation checks to see if the reserving process is still alive, and looks for another free port if it is. Ports are reserved in a directory set by the environment setting PG_TEST_PORT_DIR, or if that doesn't exist a subdirectory of the top build directory as set by Makefile.global, or its own tmp_check directory. The prove_check recipe in Makefile.global.in is extended to export top_builddir to the TAP tests. This was already exported by the prove_installcheck recipes. Per complaint from Andres Freund Backpatched from 9b4eafc to all live branches Discussion: https://postgr.es/m/20221002164931.d57hlutrcz4d2zi7@awork3.anarazel.de
1 parent 1b3ed75 commit 870d621

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/Makefile.global.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ rm -rf '$(CURDIR)'/tmp_check
471471
$(MKDIR_P) '$(CURDIR)'/tmp_check
472472
cd $(srcdir) && \
473473
TESTDIR='$(CURDIR)' $(with_temp_install) PGPORT='6$(DEF_PGPORT)' \
474+
top_builddir='$(CURDIR)/$(top_builddir)' \
474475
PG_REGRESS='$(CURDIR)/$(top_builddir)/src/test/regress/pg_regress' \
475476
$(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(if $(PROVE_TESTS),$(PROVE_TESTS),t/*.pl)
476477
endef

src/test/perl/PostgresNode.pm

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ use Carp;
9292
use Config;
9393
use Cwd;
9494
use Exporter 'import';
95-
use Fcntl qw(:mode);
95+
use Fcntl qw(:mode :flock :seek :DEFAULT);
9696
use File::Basename;
97-
use File::Path qw(rmtree);
97+
use File::Path qw(rmtree mkpath);
9898
use File::Spec;
9999
use File::stat qw(stat);
100100
use File::Temp ();
@@ -113,7 +113,10 @@ our @EXPORT = qw(
113113
);
114114

115115
our ($use_tcp, $test_localhost, $test_pghost, $last_host_assigned,
116-
$last_port_assigned, @all_nodes, $died);
116+
$last_port_assigned, @all_nodes, $died, $portdir);
117+
118+
# list of file reservations made by get_free_port
119+
my @port_reservation_files;
117120

118121
INIT
119122
{
@@ -129,6 +132,20 @@ INIT
129132

130133
# Tracking of last port value assigned to accelerate free port lookup.
131134
$last_port_assigned = int(rand() * 16384) + 49152;
135+
136+
# Set the port lock directory
137+
138+
# If we're told to use a directory (e.g. from a buildfarm client)
139+
# explicitly, use that
140+
$portdir = $ENV{PG_TEST_PORT_DIR};
141+
# Otherwise, try to use a directory at the top of the build tree
142+
# or as a last resort use the tmp_check directory
143+
my $build_dir = $ENV{top_builddir}
144+
|| $TestLib::tmp_check ;
145+
$portdir ||= "$build_dir/portlock";
146+
$portdir =~ s!\\!/!g;
147+
# Make sure the directory exists
148+
mkpath($portdir) unless -d $portdir;
132149
}
133150

134151
=pod
@@ -1409,8 +1426,8 @@ by test cases that need to start other, non-Postgres servers.
14091426
Ports assigned to existing PostgresNode objects are automatically
14101427
excluded, even if those servers are not currently running.
14111428
1412-
XXX A port available now may become unavailable by the time we start
1413-
the desired service.
1429+
The port number is reserved so that other concurrent test programs will not
1430+
try to use the same port.
14141431
14151432
=cut
14161433

@@ -1459,6 +1476,7 @@ sub get_free_port
14591476
last;
14601477
}
14611478
}
1479+
$found = _reserve_port($port) if $found;
14621480
}
14631481
}
14641482

@@ -1489,6 +1507,40 @@ sub can_bind
14891507
return $ret;
14901508
}
14911509

1510+
# Internal routine to reserve a port number
1511+
# Returns 1 if successful, 0 if port is already reserved.
1512+
sub _reserve_port
1513+
{
1514+
my $port = shift;
1515+
# open in rw mode so we don't have to reopen it and lose the lock
1516+
my $filename = "$portdir/$port.rsv";
1517+
sysopen(my $portfile, $filename, O_RDWR|O_CREAT)
1518+
|| die "opening port file $filename: $!";
1519+
# take an exclusive lock to avoid concurrent access
1520+
flock($portfile, LOCK_EX) || die "locking port file $filename: $!";
1521+
# see if someone else has or had a reservation of this port
1522+
my $pid = <$portfile>;
1523+
chomp $pid;
1524+
if ($pid +0 > 0)
1525+
{
1526+
if (kill 0, $pid)
1527+
{
1528+
# process exists and is owned by us, so we can't reserve this port
1529+
flock($portfile, LOCK_UN);
1530+
close($portfile);
1531+
return 0;
1532+
}
1533+
}
1534+
# All good, go ahead and reserve the port
1535+
seek($portfile, 0, SEEK_SET);
1536+
# print the pid with a fixed width so we don't leave any trailing junk
1537+
print $portfile sprintf("%10d\n",$$);
1538+
flock($portfile, LOCK_UN);
1539+
close($portfile);
1540+
push(@port_reservation_files, $filename);
1541+
return 1;
1542+
}
1543+
14921544
# Automatically shut down any still-running nodes (in the same order the nodes
14931545
# were created in) when the test script exits.
14941546
END
@@ -1508,6 +1560,8 @@ END
15081560
$node->clean_node if $exit_code == 0 && TestLib::all_tests_passing();
15091561
}
15101562

1563+
unlink @port_reservation_files;
1564+
15111565
$? = $exit_code;
15121566
}
15131567

0 commit comments

Comments
 (0)