def _async_finish(request, output):
    if request._started:
        request.connection.close(False)
        _cleanup(request)
        return

    # Do things App style.
    with request._context as app:
        request.auto_finish = True

        try:
            if output is Finished:
                raise RuntimeError("Reached StopIteration in asynchronous "
                                   "request handler.")

            app.parse_output(output)
        except Exception as err:
            if request._started:
                request.connection.close(False)
                _cleanup(request)
                return

            request._tb = traceback.format_exc()

            try:
                body, status, headers = app.handle_500(request, err)

            except Exception:
                log.exception("There was a problem handling an asynchronous "
                              "request, and a problem running "
                              "Application.handle_500 for %r." % app)
                body, status, headers = error(500, request=request)

            request.send_status(status)
            if not 'Content-Length' in headers:
                headers['Content-Length'] = len(body)
            request.send_headers(headers)
            request.write(body)
            request.finish()

    # Finish cleanup.
    _cleanup(request)


###############################################################################
# Asynchronous Streams
###############################################################################
test updating policies from wrapper
dataset/ETHPy150Open ecdavis/pants/pants/web/asynchronous.py/_async_finish--crossover with--dataset/ETHPy150Open twaldear/flask-secure-headers/flask_secure_headers/tests/core_test.py/TestAppUseCase.test_wrapper_update_function