From e52dc56a64186812d48ca4b7d7e318ff24401142 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Mon, 17 Feb 2020 17:01:11 +0100 Subject: [PATCH] Prevent crashing on PermissionError when importing http.server (#7095) * Fix PermissionError when importing http.server Instead of crashing, now it'll just not start the server. This means that you won't be able to receive the response from the log-in so you won't be able to log in. The browser gives an error that it can't find the page on localhost. But at least it doesn't crash. Fixes crash CURA-3Q. * Log an error when the HTTP server can't be started Contributes to crash CURA-3Q. * Indicate that types are optional Attempt to fix the typing issue with MyPy. I can't run this locally so the CI server will have to tell me if this fixed it. Contributes to Sentry issue CURA-3Q. --- cura/OAuth2/LocalAuthorizationServer.py | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/cura/OAuth2/LocalAuthorizationServer.py b/cura/OAuth2/LocalAuthorizationServer.py index 780adf54aa..0e4e491e46 100644 --- a/cura/OAuth2/LocalAuthorizationServer.py +++ b/cura/OAuth2/LocalAuthorizationServer.py @@ -1,13 +1,18 @@ -# Copyright (c) 2019 Ultimaker B.V. +# Copyright (c) 2020 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. import threading -from typing import Optional, Callable, Any, TYPE_CHECKING +from typing import Any, Callable, Optional, TYPE_CHECKING from UM.Logger import Logger -from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer -from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler +got_server_type = False +try: + from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer + from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler + got_server_type = True +except PermissionError: # Bug in http.server: Can't access MIME types. This will prevent the user from logging in. See Sentry bug Cura-3Q. + Logger.error("Can't start a server due to a PermissionError when starting the http.server.") if TYPE_CHECKING: from cura.OAuth2.Models import AuthenticationResponse @@ -50,15 +55,16 @@ class LocalAuthorizationServer: Logger.log("d", "Starting local web server to handle authorization callback on port %s", self._web_server_port) # Create the server and inject the callback and code. - self._web_server = AuthorizationRequestServer(("0.0.0.0", self._web_server_port), AuthorizationRequestHandler) - self._web_server.setAuthorizationHelpers(self._auth_helpers) - self._web_server.setAuthorizationCallback(self._auth_state_changed_callback) - self._web_server.setVerificationCode(verification_code) - self._web_server.setState(state) + if got_server_type: + self._web_server = AuthorizationRequestServer(("0.0.0.0", self._web_server_port), AuthorizationRequestHandler) + self._web_server.setAuthorizationHelpers(self._auth_helpers) + self._web_server.setAuthorizationCallback(self._auth_state_changed_callback) + self._web_server.setVerificationCode(verification_code) + self._web_server.setState(state) - # Start the server on a new thread. - self._web_server_thread = threading.Thread(None, self._web_server.serve_forever, daemon = self._daemon) - self._web_server_thread.start() + # Start the server on a new thread. + self._web_server_thread = threading.Thread(None, self._web_server.serve_forever, daemon = self._daemon) + self._web_server_thread.start() ## Stops the web server if it was running. It also does some cleanup. def stop(self) -> None: