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

Fix #1397 by calling load_wsgi after starting the reloader #1399

Merged
merged 4 commits into from Dec 12, 2016
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions gunicorn/workers/base.py
Expand Up @@ -112,8 +112,6 @@ def init_process(self):

self.init_signals()

self.load_wsgi()

# start the reloader
if self.cfg.reload and self.cfg.reload != 'off':
def changed(fname):
Expand All @@ -133,6 +131,7 @@ def changed(fname):
self.reloader = reloader_cls(callback=changed)
self.reloader.start()

self.load_wsgi()
self.cfg.post_worker_init(self)

# Enter main run loop
Expand All @@ -143,7 +142,7 @@ def load_wsgi(self):
try:
self.wsgi = self.app.wsgi()
except SyntaxError as e:
if not self.cfg.reload:
if self.cfg.reload == 'off':
raise

self.log.exception(e)
Expand All @@ -156,7 +155,7 @@ def load_wsgi(self):
exc_type, exc_val, exc_tb = sys.exc_info()
self.reloader.add_extra_file(exc_val.filename)

tb_string = traceback.format_tb(exc_tb)
tb_string = traceback.format_exc(exc_tb)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this? The usage of format_tb() was discussed in #1235 (comment).

Copy link
Contributor Author

@daker daker Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With traceback.format_tb, tb_string is an array while make_fail_app is expecting a string so if you have an error somewhere say in your wsgi file, you get this printed in the console instead showing the real error on the webpage

[2016-11-29 19:23:47 +0000] [8872] [ERROR] Error handling request
Traceback (most recent call last):
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
    resp.write(item)
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/http/wsgi.py", line 344, in write
    raise TypeError('%r is not a byte' % arg)
TypeError: ['  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 143, in load_wsgi\n    self.wsgi = self.app.wsgi()\n', '  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi\n    self.callable = self.load()\n', '  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load\n    return self.load_wsgiapp()\n', '  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp\n    return util.import_app(self.app_uri)\n', '  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/util.py", line 378, in import_app\n    __import__(module)\n'] is not a byte

You only get the real error on the console when gunicorn starts but if you hit the webpage you get nothing(ERR_CONTENT_LENGTH_MISMATCH) and the ...is not a byte error is displayed on the console

[2016-11-29 19:20:53 +0000] [8867] [INFO] Starting gunicorn 19.6.0
[2016-11-29 19:20:53 +0000] [8867] [INFO] Listening at: http://0.0.0.0:8000 (8867)
[2016-11-29 19:20:53 +0000] [8867] [INFO] Using worker: sync
[2016-11-29 19:20:53 +0000] [8872] [INFO] Booting worker with pid: 8872
[2016-11-29 19:20:53 +0000] [8872] [ERROR] unexpected indent (wsgi.py, line 7)
Traceback (most recent call last):
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 143, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/util.py", line 378, in import_app
    __import__(module)
  File "/home/daker/Projets/guni/wsgi.py", line 7
    start_response(status, response_headers)
    ^
IndentationError: unexpected indent

Copy link
Contributor Author

@daker daker Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it turns out format_exc takes a limit arg not a traceback, so this tb_string = ''.join(traceback.format_tb(exc_tb)) should fix the ...is not a byte error but gunicorn will not display the original IndentationError and display

  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 143, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/daker/Projets/guni/env/local/lib/python2.7/site-packages/gunicorn/util.py", line 378, in import_app
    __import__(module)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daker can you provide this change in another PR so wee can focus on the current issue?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean let's separate each concerns. The change for load_wsgi is fine for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benoitc i have fixed the PR

self.wsgi = util.make_fail_app(tb_string)
finally:
del exc_tb
Expand Down