aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-04-25 12:28:49 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2016-04-25 12:28:49 -0400
commit40e89e2ab89cb2801f6bc02f08dcc24d547530fc (patch)
tree0e2e1f1059332638eb7bef5f072f6e52d98165d0
parent63417b4b2e38897ea02fef416bd96113baa3ed45 (diff)
downloadpostgresql-40e89e2ab89cb2801f6bc02f08dcc24d547530fc.tar.gz
postgresql-40e89e2ab89cb2801f6bc02f08dcc24d547530fc.zip
Try harder to detect a port conflict in PostgresNode.pm.
Commit fab84c7787f25756 tried to get away without doing an actual bind(), but buildfarm results show that that doesn't get the job done. So we must really bind to the target port --- and at least on my Linux box, we need a listen() as well, or conflicts won't be detected. We rely on SO_REUSEADDR to prevent problems from starting a postmaster on the socket immediately after we've bound to it in the test code. (There may be platforms where that doesn't work too well. But fortunately, we only really care whether this works on Windows, and there the default behavior should be OK.)
-rw-r--r--src/test/perl/PostgresNode.pm18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index cd2e974de18..4f9d4bb34cd 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -859,12 +859,18 @@ sub get_new_node
# to open a TCP port on Unix.
if ($found == 1)
{
- my $iaddr = inet_aton($test_localhost);
- my $paddr = sockaddr_in($port, $iaddr);
- my $proto = getprotobyname("tcp");
-
- socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die;
- $found = 0 if connect(SOCK, $paddr);
+ my $iaddr = inet_aton($test_localhost);
+ my $paddr = sockaddr_in($port, $iaddr);
+ my $proto = getprotobyname("tcp");
+
+ socket(SOCK, PF_INET, SOCK_STREAM, $proto)
+ or die "socket failed: $!";
+
+ # As in postmaster, don't use SO_REUSEADDR on Windows
+ setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
+ unless $TestLib::windows_os;
+ (bind(SOCK, $paddr) && listen(SOCK, SOMAXCONN))
+ or $found = 0;
close(SOCK);
}
}