question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flask extension raises StopIteration during certain tests

See original GitHub issue

It seems that the Flask extension is causing the Werkzeug test client to raise a StopIteration exception.

Here’s part of the traceback:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
virtualenv/lib/python2.7/site-packages/werkzeug/test.py:772: in post
    return self.open(*args, **kw)
virtualenv/lib/python2.7/site-packages/flask/testing.py:108: in open
    follow_redirects=follow_redirects)
virtualenv/lib/python2.7/site-packages/werkzeug/test.py:736: in open
    response = self.run_wsgi_app(environ, buffered=buffered)
virtualenv/lib/python2.7/site-packages/werkzeug/test.py:659: in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

app = <Flask 'honeycomb'>
environ = {'CONTENT_LENGTH': '29', 'CONTENT_TYPE': 'application/json', 'HTTP_CONTENT_LENGTH': '29', 'HTTP_CONTENT_TYPE': 'application/json', ...}
buffered = False

    def run_wsgi_app(app, environ, buffered=False):
        """Return a tuple in the form (app_iter, status, headers) of the
        application output.  This works best if you pass it an application that
        returns an iterator all the time.

        Sometimes applications may use the `write()` callable returned
        by the `start_response` function.  This tries to resolve such edge
        cases automatically.  But if you don't get the expected output you
        should set `buffered` to `True` which enforces buffering.

        If passed an invalid WSGI application the behavior of this function is
        undefined.  Never pass non-conforming WSGI applications to this function.

        :param app: the application to execute.
        :param buffered: set to `True` to enforce buffering.
        :return: tuple in the form ``(app_iter, status, headers)``
        """
        environ = _get_environ(environ)
        response = []
        buffer = []

        def start_response(status, headers, exc_info=None):
            if exc_info is not None:
                reraise(*exc_info)
            response[:] = [status, headers]
            return buffer.append

        app_iter = app(environ, start_response)

        # when buffering we emit the close call early and convert the
        # application iterator into a regular list
        if buffered:
            close_func = getattr(app_iter, 'close', None)
            try:
                app_iter = list(app_iter)
            finally:
                if close_func is not None:
                    close_func()

        # otherwise we iterate the application iter until we have
        # a response, chain the already received data with the already
        # collected data and wrap it in a new `ClosingIterator` if
        # we have a close callable.
        else:
            while not response:
>               buffer.append(next(app_iter))
E               StopIteration

virtualenv/lib/python2.7/site-packages/werkzeug/test.py:873: StopIteration

This only seems to happen for tests that use the test client and make POSTs. Interestingly if I use the Sentry middleware directly, I’m not able to reproduce the error leading me to believe the problem lies in the Flask extension. However, so far I haven’t been able to isolate the exact cause.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:6
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
PhilipGarnerocommented, Jul 17, 2015

This is because of sentry wrapping the default wsgi app with its own. To disable this, you can either run your app in debug, init sentry with Sentry(app, wrap_wsgi=False) or sentry.init_app(app, wrap_wsgi=False) or re-override the wsgi_app using

from raven.middleware import Sentry
if isinstance(app.wsgi_app, Sentry):
    app.wsgi_app = app.wsgi_app.application

The sentry middleware needs to be fixed in order to avoid such hacks.

1reaction
matteosimonecommented, Jun 12, 2015

+1 Ran into this while integrating sentry, fixed with buffered=True

Read more comments on GitHub >

github_iconTop Results From Across the Web

Complete Guide to Python StopIteration - eduCBA
When the specified number of iterations are done, StopIteration is raised by the next method in case of iterators and generators (works similar...
Read more >
generator raised StopIteration" every time I try to run app ...
To judge from the file paths, it looks like you're running Python 3.7. If so, you're getting caught by new-in-3.7 behavior:.
Read more >
Python Web Applications With Flask – Part II
In this part of our series on Building a Web Application with Flask, we'll set up user accounts, templates, and static files.
Read more >
Test Coverage — Flask Documentation (2.2.x)
TESTING tells Flask that the app is in test mode. Flask changes some internal behavior so it's easier to test, and other extensions...
Read more >
Changelog — pytest documentation
#10382: Do not break into pdb when raise unittest.SkipTest() appears top-level in a file. #7792: Marks are now inherited according to the full...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found