From 52f17c12e0ac58cee42964f9e624d5000f46c871 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 8 Dec 2021 21:11:37 +1000 Subject: [PATCH] Partially revert "Fix issue 1362 and add a default favicon.ico (#1369)" Kept the favicon, but have reverted the rest, as it unfortunately did not seem to prevent the issue from occurring. Original discussion: https://github.com/ankitects/anki/pull/1369 This reverts commit 6d0f7e7f0584f3d6d1c8932fa6c6eba701022a6e. --- qt/aqt/main.py | 1 - qt/aqt/mediasrv.py | 57 +--------------------------------------------- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 63fc3f131..eb11f5816 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -1698,7 +1698,6 @@ title="{}" {}>{}""".format( def setupMediaServer(self) -> None: self.mediaServer = aqt.mediasrv.MediaServer(self) self.mediaServer.start() - self.mediaServer.await_ready() def baseHTML(self) -> str: return f'' diff --git a/qt/aqt/mediasrv.py b/qt/aqt/mediasrv.py index f9e93ced8..33709dddc 100644 --- a/qt/aqt/mediasrv.py +++ b/qt/aqt/mediasrv.py @@ -3,20 +3,16 @@ from __future__ import annotations -import datetime import logging import mimetypes import os import re -import socket import sys import threading import time import traceback -from contextlib import contextmanager from dataclasses import dataclass -from http import HTTPStatus, client -from typing import Any, Generator +from http import HTTPStatus import flask import flask_cors # type: ignore @@ -36,18 +32,6 @@ from aqt.deckoptions import DeckOptionsDialog from aqt.operations.deck import update_deck_configs from aqt.qt import * - -@contextmanager -def http_connection( - *args: Any, **kwds: Any -) -> Generator[client.HTTPConnection, None, None]: - resource = client.HTTPConnection(*args, **kwds) - try: - yield resource - finally: - resource.close() - - app = flask.Flask(__name__, root_path="/fake") flask_cors.CORS(app) @@ -137,45 +121,6 @@ class MediaServer(threading.Thread): except KeyError: pass - def getHost(self) -> str: - self._ready.wait() - return str(self.server.effective_host) # type: ignore - - def await_ready(self) -> None: - """Block until webserver can respond to requests. - Potentially solves a race condition at startup where some resources - fail to load. - https://github.com/ankitects/anki/pull/1369 - """ - self._check_server(self.getHost(), self.getPort(), "/favicon.ico") - - @classmethod - def _check_server( - cls, - host: str, - port: int, - path_info: str = "/", - timeout: int = 1, - retries: int = 30, - ) -> int: - """Perform a request until the server reply""" - if retries < 0: - return 0 - # https://github.com/Pylons/webtest/blob/4b8a3ebf984185ff4fefb31b4d0cf82682e1fcf7/webtest/http.py#L123-L132 - for index in range(retries): - if dev_mode or index > 0: - print( - f"{datetime.datetime.now()} awaiting media server on {host}:{port}..." - ) - try: - with http_connection(host, port, timeout=timeout) as conn: - conn.request("GET", path_info) - res = conn.getresponse() - return res.status - except (socket.error, client.HTTPException): - time.sleep(0.3) - return 0 - @app.route("/favicon.ico") def favicon() -> Response: