Skip to content

Commit 8a5e75f

Browse files
author
Sofia Kopikova
committed
Fix error 'Is another postmaster already running on port XXX'
Sometimes when we abnormally shutdown node its port stays busy. So I added retry attempts to start() function in case we encounter such error. Test for this case was added (test_the_same_port).
1 parent 4c70b80 commit 8a5e75f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

testgres/node.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -726,14 +726,28 @@ 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(en(file) > 1 and 'Is another postmaster already running on port' in file for file in files):
738+
print("Detected an issue with connecting to port {0}. "
739+
"Trying another port after a 5-second sleep...".format(self.port))
740+
self.port = reserve_port()
741+
options = {}
742+
options['port'] = str(self.port)
743+
self.set_auto_conf(options)
744+
startup_retries -= 1
745+
time.sleep(5)
746+
continue
747+
748+
msg = 'Cannot start node'
749+
raise_from(StartNodeException(msg, files), e)
750+
break
737751
self._maybe_start_logger()
738752
self.is_started = True
739753
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)