reloader should survive SyntaxError #994

Merged
merged 4 commits into from Mar 18, 2015

Projects

None yet

5 participants

@wong2
Contributor
wong2 commented Mar 11, 2015

This implements the feature requested in issue #964.

When there is SyntaxError raised while importing the app, provides a fallback app to display those errors instead of completely exit gunicorn.

@wong2
Contributor
wong2 commented Mar 12, 2015

@benoitc plz review

@berkerpeksag
Collaborator

Couldn't this be handled in https://github.com/benoitc/gunicorn/blob/master/gunicorn/workers/base.py#L89 or even in the Reloader class?

@berkerpeksag berkerpeksag and 1 other commented on an outdated diff Mar 12, 2015
gunicorn/util.py
@@ -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 iter([msg])
@berkerpeksag
berkerpeksag Mar 12, 2015 Collaborator

Why do you need to add iter() here? If I remember correctly, [msg] is enough in the WSGI spec.

@wong2
wong2 Mar 12, 2015 Contributor

oh I copied it from the example on http://gunicorn.org/ , and you're right, [msg] is enough

@wong2
wong2 Mar 14, 2015 Contributor

@berkerpeksag I've removed the iter() here

@wong2
Contributor
wong2 commented Mar 12, 2015

@berkerpeksag I don't think this can be handled in Reloader or reloader callback, since the SyntaxError is happened when the new worker is starting, while Reloader or reloader callback are in charge of terminating the old worker.

@tilgovi
Collaborator
tilgovi commented Mar 15, 2015

This looks pretty good to me.

@wong2
Contributor
wong2 commented Mar 16, 2015

@tilgovi what else should I do before this can be merged?

@tilgovi
Collaborator
tilgovi commented Mar 16, 2015

I think it seems fine, I just want to give others a chance to look at it.

@benoitc
Owner
benoitc commented Mar 16, 2015

The patch works for me. But I would do the following changes:

  • only display the traceback in debug mode so people using the reloader for another purpose won't have it displayed
  • make the error template configurable related to #993

Thoughts?

@berkerpeksag
Collaborator

Perhaps it would be good to make the try-except logic a method. This way, people can customize make_fail_app etc.

@berkerpeksag berkerpeksag and 1 other commented on an outdated diff Mar 16, 2015
gunicorn/workers/base.py
@@ -116,7 +117,19 @@ def changed(fname):
self.init_signals()
- self.wsgi = self.app.wsgi()
+ try:
+ self.wsgi = self.app.wsgi()
+ except SyntaxError as e:
+ if not self.reloader:
@berkerpeksag
berkerpeksag Mar 16, 2015 Collaborator

I'd use self.cfg.reload

@tilgovi
tilgovi Mar 16, 2015 Collaborator

+1

On Mon, Mar 16, 2015, 16:26 Berker Peksag notifications@github.com wrote:

In gunicorn/workers/base.py
#994 (comment):

@@ -116,7 +117,19 @@ def changed(fname):

     self.init_signals()
  •    self.wsgi = self.app.wsgi()
    
  •    try:
    
  •        self.wsgi = self.app.wsgi()
    
  •    except SyntaxError as e:
    
  •        if not self.reloader:
    

I'd use self.cfg.reload


Reply to this email directly or view it on GitHub
https://github.com/benoitc/gunicorn/pull/994/files#r26533913.

@wong2
Contributor
wong2 commented Mar 18, 2015

@benoitc is there still a debug mode in gunicorn? I can't find it in the doc

@benoitc
Owner
benoitc commented Mar 18, 2015

@wong2 no only debug log level (which provides the same informations) and spew setting.

@wong2
Contributor
wong2 commented Mar 18, 2015

I've moved the try-catch codes to a new method load_wsgi so it's more customizable

@tilgovi
Collaborator
tilgovi commented Mar 18, 2015

Looks great.

@benoitc benoitc merged commit 83be046 into benoitc:master Mar 18, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@berkerpeksag
Collaborator

Thanks @wong2 :)

@georgexsh
Contributor

🍺

@wong2 wong2 deleted the wong2:reloader branch Jul 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment