gunicorn icon indicating copy to clipboard operation
gunicorn copied to clipboard

Display NameError on import

Open c23009u opened this issue 6 years ago • 5 comments

If user code raises a NameError when gunicorn is importing the application, the error will be swallowed. Gunicorn is catching NameError to handle the case where the entrypoint to the user app is not valid. However this also swallows other NameErrors in the user code, such as accessing an undefined variable. This PR makes the correct traceback available.

c23009u avatar Jun 28 '18 19:06 c23009u

Thanks for the pull request! @berkerpeksag do you think it's okay to log tracebacks for this even when the app is not in debug? It seems alright to me, but would love a second opinion.

tilgovi avatar Jul 03 '18 19:07 tilgovi

I had the same thought, if it's any help my reasoning was that it's not printing anything to stdout. Previously it would print the traceback to stdout if debug was enabled, and always raise an exception regardless of debug mode. This simply adds the traceback to the end of the exception message and altogether removes the printing. It's the same exception class being raised. I can't imagine having the additional information in the exception message would break anyone's code.

c23009u avatar Jul 03 '18 21:07 c23009u

I think you can leave the error unchanged, since it's being raised. At least on Python 3 chained exceptions should make the original traceback available. We should change the self.log.debug call In line 588 of arbiter.py to be an error level, though.

tilgovi avatar Jul 03 '18 22:07 tilgovi

@berkerpeksag is there any better way to make sure the exception will be raised at debug level? Maybe just losing the exception there would be enough ?

benoitc avatar Jul 04 '18 04:07 benoitc

I believe this is no longer needed since https://github.com/benoitc/gunicorn/pull/2043 - there is no longer any confusion between NameError from looking up the application and during import.

pajod avatar Apr 22 '24 02:04 pajod