Skip to content

Commit

Permalink
clean up sys.exc_info calls to drop circular refs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulnivin committed Mar 20, 2016
1 parent 52088be commit 20d3a2d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
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

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

0 comments on commit 20d3a2d

Please sign in to comment.