in source/idea/idea-sdk/src/ideasdk/server/soca_server.py [0:0]
def stop(self):
if not self.is_running():
return
# clear _is_running, so that no new client connections will be queued
self._is_running.clear()
# shutdown executor and wait for current pending requests to complete and allow to respond back
self._executor.shutdown(wait=True, cancel_futures=True)
# we are in main thread now...
mainloop = asyncio.get_event_loop()
connections = []
if self.options.enable_unix_socket:
self._logger.info('stopping unix server ...')
self._unix_server.close()
self._unix_server.wait_closed()
connections += self._unix_server.connections
self._logger.info('stopping http server ...')
self._http_server.close()
self._http_server.wait_closed()
connections += self._http_server.connections
for connection in connections:
connection.close_if_idle()
# Gracefully shutdown timeout
# refer to sanic.server.runners.serve() for more details.
graceful = self.options.graceful_shutdown_timeout
if connections and graceful > 0:
self._logger.info(f'waiting for graceful shutdown timeout: {graceful} ...')
start_shutdown: float = 0
while connections and (start_shutdown < graceful):
mainloop.run_until_complete(asyncio.sleep(0.1))
start_shutdown = start_shutdown + 0.1
self._http_app.shutdown_tasks(graceful - start_shutdown)
if self.options.enable_unix_socket:
self._unix_app.shutdown_tasks(graceful - start_shutdown)
for connection in connections:
if hasattr(connection, 'websocket') and connection.websocket:
connection.websocket.fail_connection(code=1001)
else:
connection.abort()
mainloop.run_until_complete(self._http_server.after_stop())
# close the loop
if self._server_loop.is_running():
# _loop.stop() is not threadsafe. since we start a loop from a different thread,
# call stop using call_soon_threadsafe
self._server_loop.call_soon_threadsafe(self._server_loop.stop)
self._server_thread.join()
if self.options.enable_unix_socket:
self._remove_unix_socket()
self._logger.info('server stopped.')