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 delayed socket close caused by circular ref from assigning traceback to local var #1228

Merged
merged 1 commit into from Mar 20, 2016
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
7 changes: 2 additions & 5 deletions gunicorn/_compat.py
Expand Up @@ -107,11 +107,8 @@ def _wrap_error(exc, mapping, key):
new_err = new_err_cls(*exc.args)

# raise a new exception with the original traceback
if hasattr(exc, '__traceback__'):
traceback = exc.__traceback__
else:
traceback = sys.exc_info()[2]
six.reraise(new_err_cls, new_err, traceback)
six.reraise(new_err_cls, new_err,
exc.__traceback__ if hasattr(exc, '__traceback__') else sys.exc_info()[2])

if PY33:
import builtins
Expand Down
6 changes: 2 additions & 4 deletions gunicorn/workers/async.py
Expand Up @@ -55,13 +55,11 @@ def handle(self, listener, client, addr):
except StopIteration as e:
self.log.debug("Closing connection. %s", e)
except ssl.SSLError:
exc_info = sys.exc_info()
# pass to next try-except level
six.reraise(exc_info[0], exc_info[1], exc_info[2])
six.reraise(*sys.exc_info())
except EnvironmentError:
exc_info = sys.exc_info()
# pass to next try-except level
six.reraise(exc_info[0], exc_info[1], exc_info[2])
six.reraise(*sys.exc_info())
except Exception as e:
self.handle_error(req, client, addr, e)
except ssl.SSLError as e:
Expand Down
17 changes: 12 additions & 5 deletions gunicorn/workers/base.py
Expand Up @@ -134,11 +134,18 @@ def load_wsgi(self):

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)
# fix from PR #1228
# storing the traceback into exc_tb will create a circular reference.
# per https://docs.python.org/2/library/sys.html#sys.exc_info warning,
# delete the traceback after use.
try:
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)
finally:
del 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.

We could use a comment to explain what del exc_tb does here (and perhaps a reference to this PR).

Could you also squash the commits into one?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment and squashed the commits.


def init_signals(self):
# reset signaling
Expand Down
3 changes: 1 addition & 2 deletions gunicorn/workers/gthread.py
Expand Up @@ -339,9 +339,8 @@ def handle_request(self, req, conn):
self.log.debug("Closing connection.")
return False
except EnvironmentError:
exc_info = sys.exc_info()
# pass to next try-except level
six.reraise(exc_info[0], exc_info[1], exc_info[2])
six.reraise(*sys.exc_info())
except Exception:
if resp and resp.headers_sent:
# If the requests have already been sent, we should close the
Expand Down
3 changes: 1 addition & 2 deletions gunicorn/workers/sync.py
Expand Up @@ -182,9 +182,8 @@ def handle_request(self, listener, req, client, addr):
if hasattr(respiter, "close"):
respiter.close()
except EnvironmentError:
exc_info = sys.exc_info()
# pass to next try-except level
six.reraise(exc_info[0], exc_info[1], exc_info[2])
six.reraise(*sys.exc_info())
except Exception:
if resp and resp.headers_sent:
# If the requests have already been sent, we should close the
Expand Down