diff options
| author | Armin Ronacher <armin.ronacher@active-4.com> | 2016-08-31 23:17:27 +0200 |
|---|---|---|
| committer | Armin Ronacher <armin.ronacher@active-4.com> | 2016-08-31 23:17:27 +0200 |
| commit | 5cc06dbdded644720b62a28b48043f21f0b3af75 (patch) | |
| tree | d76209356663df6ef658ab50f8b2f67e49169117 | |
| parent | 96437e207519037b359a419bc33a6c852d44cbee (diff) | |
| download | raven-workaround/close-on-end.tar.gz | |
Automatically close iterator on end of iterationworkaround/close-on-end
| -rw-r--r-- | raven/middleware.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/raven/middleware.py b/raven/middleware.py index a6b9b06..c9c5566 100644 --- a/raven/middleware.py +++ b/raven/middleware.py @@ -44,21 +44,33 @@ class ClosingIterator(Iterator): self.sentry = sentry self.environ = environ self.iterable = iter(iterable) + self.closed = False def __iter__(self): return self def __next__(self): - with common_exception_handling(self.environ, self.sentry): - return next(self.iterable) + try: + with common_exception_handling(self.environ, self.sentry): + return next(self.iterable) + except StopIteration: + # We auto close here if we reach the end because some WSGI + # middleware does not really like to close things. To avoid + # massive leaks we just close automatically at the end of + # iteration. + self.close() + raise def close(self): + if self.closed: + return try: if hasattr(self.iterable, 'close'): with common_exception_handling(self.environ, self.sentry): self.iterable.close() finally: self.sentry.client.context.clear() + self.closed = True class Sentry(object): |
