From 8f34e194b1993392154d4a15f48eaa268757ad26 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson Date: Mon, 2 Feb 2026 20:30:30 +0100 Subject: [PATCH] Fix stopping of old test cluster when pg_ctl is not in the path If the pg_ctl binary was not in the path we would fail to stop the running database clusters so let's use our helper for getting the path to the binary. To keep the code simple we also move the stopping of the existing cluster to the code which starts it since that is only where we, at least currently, knows the path the the binaries. --- spec/helpers.rb | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/spec/helpers.rb b/spec/helpers.rb index ed46d981f..5935e2034 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -188,32 +188,6 @@ def log_and_run( logpath, *cmd ) extend Loggable - ### Check the current directory for directories that look like they're - ### testing directories from previous tests, and tell any postgres instances - ### running in them to shut down. - def stop_existing_postmasters - # tmp_test_0.22329534700318 - pat = Pathname.getwd + 'tmp_test_*' - Pathname.glob( pat.to_s ).each do |testdir| - datadir = testdir + 'data' - pidfile = datadir + 'postmaster.pid' - if pidfile.exist? && pid = pidfile.read.chomp.to_i - trace "pidfile (%p) exists: %d" % [ pidfile, pid ] - begin - Process.kill( 0, pid ) - rescue Errno::ESRCH - trace "No postmaster running for %s" % [ datadir ] - # Process isn't alive, so don't try to stop it - else - trace "Stopping lingering database at PID %d" % [ pid ] - run 'pg_ctl', '-D', datadir.to_s, '-m', 'fast', 'stop' - end - else - trace "No pidfile (%p)" % [ pidfile ] - end - end - end - class PostgresServer include Loggable @@ -238,6 +212,7 @@ def initialize(name, port: 23456, postgresql_conf: '') begin @pgdata.mkpath + stop_existing_cluster setup_cluster(postgresql_conf) start_cluster rescue => err @@ -317,6 +292,25 @@ def start_cluster log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt, '-D', @pgdata.to_s, 'start' end + def stop_existing_cluster + pidfile = @pgdata + 'postmaster.pid' + + if pidfile.exist? && pid = pidfile.read.chomp.to_i + trace "pidfile (%p) exists: %d" % [ pidfile, pid ] + begin + Process.kill( 0, pid ) + rescue Errno::ESRCH + trace "No postmaster running for %s" % [ @pgdata ] + # Process isn't alive, so don't try to stop it + else + trace "Stopping lingering database at PID %d" % [ pid ] + run pg_bin_path('pg_ctl'), '-D', @pgdata.to_s, '-m', 'fast', 'stop' + end + else + trace "No pidfile (%p)" % [ pidfile ] + end + end + def generate_ssl_certs(output_dir) gen = CertGenerator.new(output_dir) @@ -701,8 +695,6 @@ def set_etc_hosts(hostaddr, hostname) ### Automatically set up and tear down the database config.before(:suite) do |*args| - PG::TestingHelpers.stop_existing_postmasters - ENV['PGHOST'] = 'localhost' ENV['PGPORT'] ||= "23456" port = ENV['PGPORT'].to_i