Skip to content

Commit

Permalink
Merge pull request #797 from minrk/actually-start-futures
Browse files Browse the repository at this point in the history
make sure futures can be cancelled
  • Loading branch information
consideRatio authored Oct 19, 2023
2 parents 6d9d9a3 + bbc9205 commit 9b83e8c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,11 +2467,18 @@ async def _stop_all_reflectors(cls):
reflector = cls.reflectors.pop(key)
tasks.append(reflector.stop())

# make sure all tasks are Futures so we can cancel them later
# in case of error
futures = [asyncio.ensure_future(task) for task in tasks]
try:
await asyncio.gather(*tasks)
await asyncio.gather(*futures)
except Exception:
for task in tasks:
task.cancel()
# cancel any unfinished tasks before re-raising
# because gather doesn't cancel unfinished tasks.
# TaskGroup would do this cancel for us, but requires Python 3.11
for future in futures:
if not future.done():
future.cancel()
raise

def start(self):
Expand Down Expand Up @@ -2659,14 +2666,21 @@ async def _start(self):

# namespace can be changed via kubespawner_override, start watching pods only after
# load_user_options() is called
start_futures = [self._start_watching_pods()]
start_tasks = [self._start_watching_pods()]
if self.events_enabled:
start_futures.append(self._start_watching_events())
start_tasks.append(self._start_watching_events())
# create Futures for coroutines so we can cancel them
# in case of an error
start_futures = [asyncio.ensure_future(task) for task in start_tasks]
try:
await asyncio.gather(*start_futures)
except Exception:
# cancel any unfinished tasks before re-raising
# because gather doesn't cancel unfinished tasks.
# TaskGroup would do this cancel for us, but requires Python 3.11
for future in start_futures:
future.cancel()
if not future.done():
future.cancel()
raise

# record latest event so we don't include old
Expand Down

0 comments on commit 9b83e8c

Please sign in to comment.