Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1orO2H-0007ji-Ff for pgsql-hackers@arkaria.postgresql.org; Sat, 05 Nov 2022 18:36:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1orO2F-0008Qg-UO for pgsql-hackers@arkaria.postgresql.org; Sat, 05 Nov 2022 18:36:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1orO2F-0008QX-KM for pgsql-hackers@lists.postgresql.org; Sat, 05 Nov 2022 18:36:51 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1orO2D-00021B-5H for pgsql-hackers@postgresql.org; Sat, 05 Nov 2022 18:36:51 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 3316132007E8; Sat, 5 Nov 2022 14:36:45 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sat, 05 Nov 2022 14:36:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1667673404; x=1667759804; bh=HAzUetTFxP jRmS9Eys0KXqGRSXp030mhNFsncrYDWTM=; b=TkXPbRDBLJ/QpDhjr33OofOU7E ku0+9A+8pb4MoXmsj3PLz/uLyRQeoUBKahpGSpi8RPJtTGI/r503NGps50kUNJSq zsGuVWcBqSatxYYDmGZAh/iXFOI8RHjqCk8NAPiR/puUiX6RtyqmxA1CWyW3+n9x AI3zStp4RzwpPDSw+AWDZvGRA0lZOZ2SoGMHkE75D6ajAg8lhM1myUx9wsjN4miq +2H2bhNkr76ZVa7keUlJELKhh2QRK27eEoOCu4Sow/yA8sE3Pew3zoqgiMFVLQoh IjaFCYfyP21Tiuov0m/7R6bp69jkNXIK3euLuEVCV6ERTU3HcQBsYkUHCKaw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1667673404; x=1667759804; bh=HAzUetTFxPjRmS9Eys0KXqGRSXp0 30mhNFsncrYDWTM=; b=IiEsQ0Q0LP6liRtkhGZJNAK+hC80yZ30bOr7IZKyJ14K xZ2LhIaqeZZhh9E5Co9j18mzzSGQz0o2PDpD53UP+ScdTV2efdb+qGqXL72TP839 lDyjx0+WkXsyB3/QM6mxuMqFvlIj5Y5Xn25frq6cKhrI2PYxQePGXk09eAWXnioN QKEsfTQJxLDXYz4grGD4yBkHjQnmwr5apzJ/3m+1BTHF/R5tbF0/hLk1xvARWRkm 8mlpuhIy8AKUW+T6u3fOoXafdszefOwFB2esjMMPT4jFYw1SNFqpF0Xiyv/Isp+i FF8UxRuLobG8W+Oc5DCEJpyYcweKrdBHzNil8JW+Vw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrvdefgdduuddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrf grthhtvghrnhepvdfffeevhfetveffgeeiteefhfdtvdffjeevhfeuteegleduheetvedu ieettddunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprghnughrvghssegrnhgrrhgriigvlhdruggv X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 5 Nov 2022 14:36:44 -0400 (EDT) Date: Sat, 5 Nov 2022 11:36:43 -0700 From: Andres Freund To: Andrew Dunstan Cc: pgsql-hackers@postgresql.org, Tom Lane Subject: Re: ssl tests aren't concurrency safe due to get_free_port() Message-ID: <20221105183643.v2nxsac3xlb3eazi@awork3.anarazel.de> References: <20221002164931.d57hlutrcz4d2zi7@awork3.anarazel.de> <7579f627-aad4-7a04-1a98-033cc6b3cca5@dunslane.net> <52266bf8-bf38-fdfa-9a77-1568f82b63b3@dunslane.net> <0891f64a-d30f-a1b7-9d0c-e447684cd8cd@dunslane.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2022-11-03 14:16:51 -0400, Andrew Dunstan wrote: > > Here's a patch which I think does the right thing. > Updated with a couple of thinkos fixed. Thanks! > diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm > index d80134b26f..aceca353d3 100644 > --- a/src/test/perl/PostgreSQL/Test/Cluster.pm > +++ b/src/test/perl/PostgreSQL/Test/Cluster.pm > @@ -93,9 +93,9 @@ use warnings; > > use Carp; > use Config; > -use Fcntl qw(:mode); > +use Fcntl qw(:mode :flock :seek O_CREAT O_RDWR); Does this do anything useful on windows? > # the minimum version we believe to be compatible with this package without > # subclassing. > @@ -140,6 +140,27 @@ INIT > > # Tracking of last port value assigned to accelerate free port lookup. > $last_port_assigned = int(rand() * 16384) + 49152; > + > + # Set the port lock directory > + > + # If we're told to use a directory (e.g. from a buildfarm client) > + # explicitly, use that > + $portdir = $ENV{PG_TEST_PORT_DIR}; > + # Otherwise, try to use a directory at the top of the build tree > + if (! $portdir && $ENV{MESON_BUILD_ROOT}) > + { > + $portdir = $ENV{MESON_BUILD_ROOT} . '/portlock' > + } > + elsif (! $portdir && ($ENV{TESTDATADIR} || "") =~ /\W(src|contrib)\W/p) > + { > + my $dir = ${^PREMATCH}; > + $portdir = "$dir/portlock" if $dir; > + } > + # As a last resort use a directory under tmp_check > + $portdir ||= $PostgreSQL::Test::Utils::tmp_check . '/portlock'; > + $portdir =~ s!\\!/!g; > + # Make sure the directory exists > + mkpath($portdir) unless -d $portdir; > } > > =pod > @@ -1505,6 +1526,7 @@ sub get_free_port > last; > } > } > + $found = _reserve_port($port) if $found; > } > } > > @@ -1535,6 +1557,38 @@ sub can_bind > return $ret; > } > > +# Internal routine to reserve a port number > +# Returns 1 if successful, 0 if port is already reserved. > +sub _reserve_port > +{ > + my $port = shift; > + # open in rw mode so we don't have to reopen it and lose the lock > + sysopen(my $portfile, "$portdir/$port.rsv", O_RDWR|O_CREAT) > + || die "opening port file"; > + # take an exclusive lock to avoid concurrent access > + flock($portfile, LOCK_EX) || die "locking port file"; > + # see if someone else has or had a reservation of this port > + my $pid = <$portfile>; > + chomp $pid; > + if ($pid +0 > 0) Gotta love perl. > + { > + if (kill 0, $pid) Does this work on windows? Greetings, Andres Freund