Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure to shutdown on keyboard interrupt #3982

Merged
merged 3 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ task outputs/message triggers are now validated.
workflow in a sub-directory of a run directory (as `cylc scan` would not be
able to find it).

[#3982](https://github.com/cylc/cylc-flow/pull/3982) - Fix bug preventing
workflow from shutting down properly on a keyboard interrupt (Ctrl+C) in
Python 3.8+.

-------------------------------------------------------------------------------
## __cylc-8.0a2 (2020-07-03)__

Expand Down
38 changes: 25 additions & 13 deletions cylc/flow/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,8 @@ async def start_scheduler(self):
await self.shutdown(exc)
raise exc from None

except Exception as exc:
try:
await self.shutdown(exc)
except Exception as exc2:
# In case of exceptions in the shutdown method itself
LOG.exception(exc2)
raise exc from None
except (KeyboardInterrupt, asyncio.CancelledError, Exception) as exc:
await self.handle_exception(exc)

else:
# main loop ends (not used?)
Expand All @@ -643,9 +638,8 @@ async def run(self):
await self.configure()
await self.start_servers()
await self.log_start()
except Exception as exc:
await self.shutdown(exc)
raise
except (KeyboardInterrupt, asyncio.CancelledError, Exception) as exc:
await self.handle_exception(exc)
else:
# note start_scheduler handles its own shutdown logic
await self.start_scheduler()
Expand Down Expand Up @@ -1619,16 +1613,19 @@ async def shutdown(self, reason):

"""
if isinstance(reason, SchedulerStop):
LOG.info('Suite shutting down - %s', reason.args[0])
LOG.info(f'Suite shutting down - {reason.args[0]}')
elif isinstance(reason, SchedulerError):
LOG.error('Suite shutting down - %s', reason)
LOG.error(f'Suite shutting down - {reason}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

elif isinstance(reason, SuiteConfigError):
LOG.error(f'{SuiteConfigError.__name__}: {reason}')
elif isinstance(reason, PlatformLookupError):
LOG.error(f'{PlatformLookupError.__name__}: {reason}')
else:
LOG.exception(reason)
LOG.critical('Suite shutting down - %s', reason)
if str(reason):
LOG.critical(f'Suite shutting down - {reason}')
else:
LOG.critical('Suite shutting down')

if self.proc_pool:
self.proc_pool.close()
Expand Down Expand Up @@ -1836,3 +1833,18 @@ def process_cylc_stop_point(self):
if stoppoint is not None:
self.options.stopcp = str(stoppoint)
self.pool.set_stop_point(get_point(self.options.stopcp))

async def handle_exception(self, exc):
"""Gracefully shut down the scheduler.

This re-raises the caught exception, to be caught higher up.

Args:
exc: The caught exception to be logged during the shutdown.
"""
try:
await self.shutdown(exc)
except Exception as exc2:
# In case of exceptions in the shutdown method itself
LOG.exception(exc2)
raise exc from None
8 changes: 1 addition & 7 deletions cylc/flow/scheduler_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,7 @@ async def _run(parser, options, reg, is_restart, scheduler):
# stop cylc stop
except SchedulerError:
ret = 1
except KeyboardInterrupt as exc:
try:
await scheduler.shutdown(exc)
except Exception as exc2:
# In case of exceptions in the shutdown method itself.
LOG.exception(exc2)
raise exc2 from None
Comment on lines -409 to -415
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this anymore as the shutdown is handled in scheduler.run() or scheduler.start_scheduler()

except (KeyboardInterrupt, asyncio.CancelledError):
ret = 2
except Exception:
ret = 3
Expand Down