Skip to content

Commit 0b761ad

Browse files
Merge commit 'c3b25b22f6004d592e1f53d0366486d367fb3b48' into master-fix244-v000
2 parents a5cd97a + c3b25b2 commit 0b761ad

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

testgres/backup.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,19 @@ def spawn_replica(self, name=None, destroy=True, slot=None):
184184
"""
185185

186186
# Build a new PostgresNode
187-
with clean_on_error(self.spawn_primary(name=name,
188-
destroy=destroy)) as node:
187+
node = self.spawn_primary(name=name, destroy=destroy)
188+
assert node is not None
189189

190+
try:
190191
# Assign it a master and a recovery file (private magic)
191192
node._assign_master(self.original_node)
192193
node._create_recovery_conf(username=self.username, slot=slot)
194+
except: # noqa: E722
195+
# TODO: Pass 'final=True' ?
196+
node.cleanup(release_resources=True)
197+
raise
193198

194-
return node
199+
return node
195200

196201
def cleanup(self):
197202
"""

testgres/node.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ def __enter__(self):
237237
return self
238238

239239
def __exit__(self, type, value, traceback):
240-
self.free_port()
241-
242240
# NOTE: Ctrl+C does not count!
243241
got_exception = type is not None and type != KeyboardInterrupt
244242

@@ -252,6 +250,8 @@ def __exit__(self, type, value, traceback):
252250
else:
253251
self._try_shutdown(attempts)
254252

253+
self._release_resources()
254+
255255
def __repr__(self):
256256
return "{}(name='{}', port={}, base_dir='{}')".format(
257257
self.__class__.__name__,
@@ -663,6 +663,9 @@ def _try_shutdown(self, max_attempts, with_force=False):
663663
ps_output,
664664
ps_command)
665665

666+
def _release_resources(self):
667+
self.free_port()
668+
666669
@staticmethod
667670
def _throw_bugcheck__unexpected_result_of_ps(result, cmd):
668671
assert type(result) == str # noqa: E721
@@ -1340,7 +1343,7 @@ def free_port(self):
13401343
self._port = None
13411344
self._port_manager.release_port(port)
13421345

1343-
def cleanup(self, max_attempts=3, full=False):
1346+
def cleanup(self, max_attempts=3, full=False, release_resources=False):
13441347
"""
13451348
Stop node if needed and remove its data/logs directory.
13461349
NOTE: take a look at TestgresConfig.node_cleanup_full.
@@ -1363,6 +1366,9 @@ def cleanup(self, max_attempts=3, full=False):
13631366

13641367
self.os_ops.rmdirs(rm_dir, ignore_errors=False)
13651368

1369+
if release_resources:
1370+
self._release_resources()
1371+
13661372
return self
13671373

13681374
@method_decorator(positional_args_hack(['dbname', 'query']))

0 commit comments

Comments
 (0)