Merge pull request #4169 from Rohan-Salwan/dev

fix raising error during cli lazy loading
This commit is contained in:
David Lord 2021-08-05 19:17:52 -07:00 committed by GitHub
commit c3f923d0e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 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
@ -310,6 +311,23 @@ def test_scriptinfo(test_apps, monkeypatch):
assert app.name == "testapp" assert app.name == "testapp"
def test_lazy_load_error(monkeypatch):
"""When using lazy loading, the correct exception should be
re-raised.
"""
class BadExc(Exception):
pass
def bad_load():
raise BadExc
lazy = DispatchingApp(bad_load, use_eager_loading=False)
with pytest.raises(BadExc):
lazy._flush_bg_loading_exception()
def test_with_appcontext(runner): def test_with_appcontext(runner):
@click.command() @click.command()
@with_appcontext @with_appcontext