Skip to content

Commit 4766f93

Browse files
Merge pull request #120 from postgrespro/fix-already-used-port-startup-error
Fix error 'Is another postmaster already running on port XXX'
2 parents 4c70b80 + fba3bd3 commit 4766f93

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

testgres/node.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -726,14 +726,30 @@ def start(self, params=[], wait=True):
726726
"start"
727727
] + params # yapf: disable
728728

729-
try:
730-
exit_status, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
731-
if error and 'does not exist' in error:
732-
raise Exception
733-
except Exception as e:
734-
msg = 'Cannot start node'
735-
files = self._collect_special_files()
736-
raise_from(StartNodeException(msg, files), e)
729+
startup_retries = 5
730+
while True:
731+
try:
732+
exit_status, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
733+
if error and 'does not exist' in error:
734+
raise Exception
735+
except Exception as e:
736+
files = self._collect_special_files()
737+
if any(len(file) > 1 and 'Is another postmaster already '
738+
'running on port' in file[1].decode() for
739+
file in files):
740+
print("Detected an issue with connecting to port {0}. "
741+
"Trying another port after a 5-second sleep...".format(self.port))
742+
self.port = reserve_port()
743+
options = {}
744+
options['port'] = str(self.port)
745+
self.set_auto_conf(options)
746+
startup_retries -= 1
747+
time.sleep(5)
748+
continue
749+
750+
msg = 'Cannot start node'
751+
raise_from(StartNodeException(msg, files), e)
752+
break
737753
self._maybe_start_logger()
738754
self.is_started = True
739755
return self

tests/test_simple.py

+8
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,14 @@ def test_parse_pg_version(self):
10331033
# Macos
10341034
assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9"
10351035

1036+
def test_the_same_port(self):
1037+
with get_new_node() as node:
1038+
node.init().start()
1039+
1040+
with get_new_node() as node2:
1041+
node2.port = node.port
1042+
node2.init().start()
1043+
10361044

10371045
if __name__ == '__main__':
10381046
if os.environ.get('ALT_CONFIG'):

0 commit comments

Comments
 (0)