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

No response and No exception traceback when render_template failed and DEBUG=True #1995

Closed
guyskk opened this issue Aug 27, 2016 · 22 comments
Closed

Comments

@guyskk
Copy link

guyskk commented Aug 27, 2016

Here is the code:

app.py

from flask import Flask, render_template

app = Flask(__name__)
app.config['DEBUG'] = True

@app.route('/')
def index():
    return render_template('unknown')

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('127.0.0.1', 5000, app)

Run it by

python app.py

or

export FLASK_APP=app.py 
flask run

then visit http://127.0.0.1:5000 will see ERR_EMPTY_RESPONSE in browser and nothing in console.

@liuliqiang
Copy link

liuliqiang commented Aug 27, 2016

you are using werkzueg's simple_run, in this way , app.config.DEBUG will not work, if you want it to work , you should use:
run_simple('127.0.0.1', 5000, app, use_debugger=True)
or
app.run('127.0.0.1', 5000, debug=True)

if you want to use config.DEBUG = True work, you may run as wsgi server.

@guyskk
Copy link
Author

guyskk commented Aug 27, 2016

I expect it print exception traceback in console and show 500 internal server error in browser.
when DEBUG is False, it will print exception traceback and show 500 in browser, but when DEBUG is True, it not, so I think it is a bug.

@liuliqiang
Copy link

@guyskk which version Flask does you using?
i checkout at v0.10.1 it print exception trackback in console when DEBUG is True and not print when DEBUG is False
and at v0.11.1 both DEBUG is True or False, it show trackback in console.

@guyskk
Copy link
Author

guyskk commented Aug 27, 2016

@luke0922 flask 0.11.1 and python3.5

@liuliqiang
Copy link

@guyskk it seems work as you said, i just try in python 3.5.1 and get the same result, maybe it's a bug.

@untitaker
Copy link
Contributor

Can reproduce. No problems with Python 2.

@untitaker untitaker added the bug label Aug 27, 2016
@qingyunha
Copy link

I reproduced this with flask 0.11.1 and python 3.5.2

@qingyunha
Copy link

I found the jinja2.exceptions.TemplateNotFound exception was catch by wrong place

        try:
            execute(self.server.app)
        except (socket.error, socket.timeout) as e: # go here
            self.connection_dropped(e, environ)
        except Exception:
            if self.server.passthrough_errors:
                raise

and in python3.5

issubclass(jinja2.exceptions.TemplateNotFound, socket.error) 
True

@qingyunha
Copy link

here is the thing:

In [23]: jinja2.exceptions.TemplateNotFound.__bases__
Out[23]: (OSError, LookupError, jinja2.exceptions.TemplateError)

In [24]: socket.error is OSError
Out[24]: True

@untitaker
Copy link
Contributor

I see, I think under Python 3 we should use one of the other error types that the socket module has to offer: https://docs.python.org/3/library/socket.html#exceptions -- not sure if herror and gaierror catch everything though.

@untitaker
Copy link
Contributor

Also thank you so much @qingyunha for finding this!

@mitsuhiko
Copy link
Contributor

Cool stuff. I think we could keep a tuple of error types for socket stuff in the compat module and catch on that.

Not my favorite change.

@untitaker
Copy link
Contributor

Apparently this change was done in Python 3.3 too.

@untitaker
Copy link
Contributor

BTW, the reason this exception even bubbles up this far is pallets/werkzeug#954

@mitsuhiko
Copy link
Contributor

We could try to detect the actual error codes relevant for socket loss.

@qingyunha
Copy link

is this reasonable that Template-Not-Found is an OS error?

@untitaker
Copy link
Contributor

I don't really know but changing it won't solve the problem (because there might be other errors that get accidentally caught)

On 28 August 2016 11:58:09 CEST, qingyunha notifications@github.com wrote:

is this reasonable that Template-Not-Found is an OS error?

You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#1995 (comment)

Sent from my Android device with K-9 Mail. Please excuse my brevity.

@untitaker
Copy link
Contributor

We could also detect if the exception is of the exact type socket.error, and not a subclass.

@Luavis
Copy link

Luavis commented Sep 14, 2016

We can probably determine error is not subclass of OSError but socket.error. socket.error inherit not only OSError but also IOError (FYI).

issubclass(socket.error, IOError)  # True
issubclass(socket.error, OSError)  # True

@untitaker
Copy link
Contributor

Yes, but also:

issubclass(OSError, IOError)  # True
issubclass(IOError, OSError)  # True

So I don't think this will help us much.

@davidism
Copy link
Member

davidism commented Sep 14, 2016

>>> # Python 3
>>> socket.error is OSError
True
>>> socket.error()
OSError()

In Python 3, socket.error isn't a subclass, it's just an alias of OSError. In Python 2 it is a distinct type, and not a subclass of OSError.

>>> # Python 2
>>> issubclass(socket.error, OSError)
False

@davidism
Copy link
Member

This is an issue with Werkzeug's dev server, not with Flask. Other servers such as Gunicorn do not have the issue. Reported at pallets/werkzeug#1127.

@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.
Projects
None yet
Development

No branches or pull requests

7 participants