From 8712866489658947d207ad27ea537cf918ceb562 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 21 Mar 2017 18:06:45 +0100 Subject: [PATCH] Fix Can't connect to compute local on some computers Chicken and egg problem we try to connect to compute before http was available. I rewrote the code to prevent erase of settings and rollback the beta4 code for init. Fix #946 --- gns3server/controller/__init__.py | 1 - gns3server/handlers/api/controller/server_handler.py | 12 +++++++++++- gns3server/web/web_server.py | 10 +++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index d7ae7d49..26c6d712 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -248,7 +248,6 @@ class Controller: """ Store settings shared by the different GUI will be replace by dedicated API later. Dictionnary """ - assert self._settings is not None return self._settings @settings.setter diff --git a/gns3server/handlers/api/controller/server_handler.py b/gns3server/handlers/api/controller/server_handler.py index f6551550..a0208a69 100644 --- a/gns3server/handlers/api/controller/server_handler.py +++ b/gns3server/handlers/api/controller/server_handler.py @@ -103,7 +103,15 @@ class ServerHandler: description="Retrieve gui settings from the server. Temporary will we removed in later release") def read_settings(request, response): - response.json(Controller.instance().settings) + settings = None + while True: + # The init of the server could take some times + # we ensure settings are loaded before returning them + settings = Controller.instance().settings + if settings is not None: + break + yield from asyncio.sleep(0.5) + response.json(settings) @Route.post( r"/settings", @@ -113,6 +121,8 @@ class ServerHandler: }) def write_settings(request, response): controller = Controller.instance() + if controller.settings is None: # Server is not loaded ignore settings update to prevent buggy client sync issue + return controller.settings = request.json try: controller.save() diff --git a/gns3server/web/web_server.py b/gns3server/web/web_server.py index cffea18f..1e8bd1b8 100644 --- a/gns3server/web/web_server.py +++ b/gns3server/web/web_server.py @@ -186,6 +186,13 @@ class WebServer: atexit.register(close_asyncio_loop) + @asyncio.coroutine + def _on_startup(self, *args): + """ + Called when the HTTP server start + """ + yield from Controller.instance().start() + def run(self): """ Starts the server. @@ -225,8 +232,9 @@ class WebServer: for key, val in os.environ.items(): log.debug("ENV %s=%s", key, val) - self._loop.run_until_complete(Controller.instance().start()) self._app = aiohttp.web.Application() + # Background task started with the server + self._app.on_startup.append(self._on_startup) # Allow CORS for this domains cors = aiohttp_cors.setup(self._app, defaults={