    async def shut_down(self, traceback=None, exception=None):
        """Shut down the scheduler.

        Shutting down includes running tasks that have
        ``on_shutdown`` as ``True``, handling the
        shutting down of already running tasks and
        restarting the scheduler in case restart was
        called.

        If non fatal exception was raised and ``instant_shutdown``
        is ``False``, remaining running tasks are waited to finish.
        Else all the tasks are terminated. If ``instant_shutdown``
        is ``True``, the scheduler won't wait for the terminated
        tasks to finish their termination.
        """

        self.logger.debug("Beginning shutdown sequence...")
        hooker = _Hooker(self.session.hooks.scheduler_shutdown)
        hooker.prerun(scheduler=self)

        # First the shut down tasks are run
        # Then all tasks are waited to finish or terminated
        # Then all threads/processes are wait to die
        try:
            try:
                try:
                    await self.run_shutdown_tasks()
                finally:
                    # Tasks are shut down/waited for shut down regardless if running shutdown
                    # tasks failed
                    self.logger.debug("Shutting down tasks...")
                    await self._shut_down_tasks(traceback, exception)
            finally:
                # Processes/threads are wait to shut down regardless if there has been any
                # additional errors previously
                await self.wait_task_alive() # Wait till all tasks' threads and processes are dead

                # Finally check logs once more
                # and raise TaskLoggingError if has occurred in a thread and they are not silenced
                self.handle_logs()
                self.check_thread_errors()
        finally:
            # Running hooks and finalize the shutdown
            hooker.postrun()
            self.is_alive = False
            self.logger.info("Shutdown completed. Good bye.")

        if isinstance(exception, SchedulerRestart):
            # Clean up finished, restart is finally
            # possible
            await self._restart()
Shut down the scheduler.

Shutting down includes running tasks that have
``on_shutdown`` as ``True``, handling the
shutting down of already running tasks and
restarting the scheduler in case restart was
called.

If non fatal exception was raised and ``instant_shutdown``
is ``False``, remaining running tasks are waited to finish.
Else all the tasks are terminated. If ``instant_shutdown``
is ``True``, the scheduler won't wait for the terminated
tasks to finish their termination.
rocketry/core/schedule.py