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

reloader should survive SyntaxError #994

Merged
merged 4 commits into from Mar 18, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions gunicorn/util.py
Expand Up @@ -532,3 +532,15 @@ def warn(msg):

print("!!!\n", file=sys.stderr)
sys.stderr.flush()


def make_fail_app(msg):

def app(environ, start_response):
start_response("500 Internal Server Error", [
("Content-Type", "text/plain"),
("Content-Length", str(len(msg)))
])
return [msg]

return app
20 changes: 18 additions & 2 deletions gunicorn/workers/base.py
Expand Up @@ -8,6 +8,7 @@
import signal
import sys
import time
import traceback
from random import randint


Expand Down Expand Up @@ -116,14 +117,29 @@ def changed(fname):

self.init_signals()

self.wsgi = self.app.wsgi()

self.cfg.post_worker_init(self)

self.load_wsgi()

# Enter main run loop
self.booted = True
self.run()

def load_wsgi(self):
try:
self.wsgi = self.app.wsgi()
except SyntaxError as e:
if not self.cfg.reload:
raise

self.log.exception(e)

exc_type, exc_val, exc_tb = sys.exc_info()
self.reloader.add_extra_file(exc_val.filename)

tb_string = traceback.format_exc(exc_tb)
self.wsgi = util.make_fail_app(tb_string)

def init_signals(self):
# reset signaling
[signal.signal(s, signal.SIG_DFL) for s in self.SIGNALS]
Expand Down