correctly handle raising deferred errors in cli lazy loading

This commit is contained in:
Rohan salwan 2021-06-23 05:06:03 +05:30 committed by David Lord
parent 66d9d4fa8d
commit aa6dd09c2c
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 16 additions and 8 deletions

View File

@ -17,6 +17,8 @@ Unreleased
:issue:`4150` :issue:`4150`
- ``jsonify`` handles ``decimal.Decimal`` by encoding to ``str``. - ``jsonify`` handles ``decimal.Decimal`` by encoding to ``str``.
:issue:`4157` :issue:`4157`
- Correctly handle raising deferred errors in CLI lazy loading.
:issue:`4096`
Version 2.0.1 Version 2.0.1

View File

@ -312,7 +312,7 @@ class DispatchingApp:
self.loader = loader self.loader = loader
self._app = None self._app = None
self._lock = Lock() self._lock = Lock()
self._bg_loading_exc_info = None self._bg_loading_exc = None
if use_eager_loading is None: if use_eager_loading is None:
use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true" use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true"
@ -328,23 +328,24 @@ class DispatchingApp:
with self._lock: with self._lock:
try: try:
self._load_unlocked() self._load_unlocked()
except Exception: except Exception as e:
self._bg_loading_exc_info = sys.exc_info() self._bg_loading_exc = e
t = Thread(target=_load_app, args=()) t = Thread(target=_load_app, args=())
t.start() t.start()
def _flush_bg_loading_exception(self): def _flush_bg_loading_exception(self):
__traceback_hide__ = True # noqa: F841 __traceback_hide__ = True # noqa: F841
exc_info = self._bg_loading_exc_info exc = self._bg_loading_exc
if exc_info is not None:
self._bg_loading_exc_info = None if exc is not None:
raise exc_info self._bg_loading_exc = None
raise exc
def _load_unlocked(self): def _load_unlocked(self):
__traceback_hide__ = True # noqa: F841 __traceback_hide__ = True # noqa: F841
self._app = rv = self.loader() self._app = rv = self.loader()
self._bg_loading_exc_info = None self._bg_loading_exc = None
return rv return rv
def __call__(self, environ, start_response): def __call__(self, environ, start_response):

View File

@ -17,6 +17,7 @@ from flask import Blueprint
from flask import current_app from flask import current_app
from flask import Flask from flask import Flask
from flask.cli import AppGroup from flask.cli import AppGroup
from flask.cli import DispatchingApp
from flask.cli import dotenv from flask.cli import dotenv
from flask.cli import find_best_app from flask.cli import find_best_app
from flask.cli import FlaskGroup from flask.cli import FlaskGroup
@ -659,3 +660,7 @@ def test_click_7_deprecated():
pytest.deprecated_call(cli_main, match=".* Click 7 is deprecated") pytest.deprecated_call(cli_main, match=".* Click 7 is deprecated")
else: else:
cli_main() cli_main()
def test_load_in_background():
pytest.raises(Exception, DispatchingApp, "appname123")