Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask error handler caching leads to inconsistent behavior #2267

Closed
ngaya-ll opened this issue May 2, 2017 · 1 comment · Fixed by #2362
Closed

Flask error handler caching leads to inconsistent behavior #2267

ngaya-ll opened this issue May 2, 2017 · 1 comment · Fixed by #2362
Assignees
Labels
Milestone

Comments

@ngaya-ll
Copy link

ngaya-ll commented May 2, 2017

When Flask finds an error handler for a given exception, it caches it in the handler map. This can lead to incorrect behavior for subclasses of the exception.

Here is some code demonstrating the problem:

from flask import Flask
from werkzeug.exceptions import InternalServerError


class E1(Exception):
    pass


class E2(Exception):
    pass


class E(E1, E2):
    pass


app = Flask(__name__)


@app.errorhandler(E2)
def handle_e2(e):
    return "E2", 500


@app.errorhandler(Exception)
def handle_exception(e):
    return "Exception", 500


@app.route("/<exception>", methods=['POST'])
def raise_exception(exception):
    exc = globals()[exception]
    raise exc


def test_errorhandler_precedence():
    client = app.test_client()

    response1 = client.post('/E1')
    assert response1.data == "Exception"

    response2 = client.post('/E')
    assert response2.data == "E2"

In this example, we have two exception classes, E1 and E2, and a third exception E that inherits from both. We register error handlers for E2 and Exception. When E is raised, Flask should invoke the E2 error handler as E2 comes before Exception in the MRO of E. But if E1 has been raised in the past, then the Exception error handler will be "cached" as the handler for E1, so that handler will be invoked instead.

@ngaya-ll ngaya-ll changed the title Flask error handler caching behavior leads to inconsistent behavior Flask error handler caching leads to inconsistent behavior May 2, 2017
@davidism
Copy link
Member

Related to #1433

@davidism davidism modified the milestone: 1.0 Jun 2, 2017
@davidism davidism added the bug label Jun 4, 2017
@davidism davidism self-assigned this Jun 4, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants