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 6d0f7e7f05.
This commit is contained in:
Damien Elmes 2021-12-08 21:11:37 +10:00
parent db5117ce1a
commit 52f17c12e0
2 changed files with 1 additions and 57 deletions

View file

@ -1698,7 +1698,6 @@ title="{}" {}>{}</button>""".format(
def setupMediaServer(self) -> None:
self.mediaServer = aqt.mediasrv.MediaServer(self)
self.mediaServer.start()
self.mediaServer.await_ready()
def baseHTML(self) -> str:
return f'<base href="{self.serverURL()}">'

View file

@ -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: