diff --git a/sanic/worker.py b/sanic/worker.py index 6fa9a25e..7a8303d8 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -64,6 +64,9 @@ class GunicornWorker(base.Worker): trigger_events(self._server_settings.get('after_start', []), self.loop) self.loop.run_until_complete(self._check_alive()) + trigger_events(self._server_settings.get('before_stop', []), + self.loop) + self.loop.run_until_complete(self.close()) finally: trigger_events(self._server_settings.get('after_stop', []), self.loop) @@ -72,8 +75,6 @@ class GunicornWorker(base.Worker): sys.exit(self.exit_code) async def close(self): - trigger_events(self._server_settings.get('before_stop', []), - self.loop) if self.servers: # stop accepting connections self.log.info("Stopping server: %s, connections: %s", @@ -115,8 +116,6 @@ class GunicornWorker(base.Worker): except (Exception, BaseException, GeneratorExit, KeyboardInterrupt): pass - await self.close() - @staticmethod def _create_ssl_context(cfg): """ Creates SSLContext instance for usage in asyncio.create_server.