Skip to content

Cached aiohttp.web.Response objects cause requests to hang #3020

Closed
@lpedrosa

Description

Long story short

If you use a cached aiohttp.web.Response in a middleware function it will cause any subsequent requests to hang.

Expected behaviour

After thinking for a bit, I suggest that we either:

  • Improve documentation, by showing an example of what to do if you wish to cache the response body, or
  • Make the response object re-usable between requests. It can still remain unchangeable after prepare() has been called, preserving its semantics. However, this is probably not feasible with the current architecture.

Actual behaviour

As far as I understand, aiohttp.web.Response is a Finite State Machine. Once a Response
object is dispatched by the server, it must be emptying some internal buffer. This renders it unusable for subsequent dispatches.

The current solution for avoiding this behaviour seems to be having the middleware generate a new Response object, everytime it is called.

Steps to reproduce

Here is a simple application that reproduces the behaviour:

from aiohttp import web


def my_middleware_factory():

    # this could have been passed in as a default arg, or exist as a global
    state = web.Response(status=200, text='this is cached')

    @web.middleware
    async def middleware(request, handler):
        res = await handler(request)
        return state

    return middleware


async def handler(request):
    return web.Response(status=200, text='Hello World!')


def main():
    app = web.Application()
    app.router.add_get('/', handler)
    app.middlewares.append(my_middleware_factory())

    web.run_app(app)


if __name__ == '__main__':
    main()
  • When you hit http://localhost:8080 twice
  • Then the second request should hang your http client

Your environment

Python 3.6
aiohttp=3.2.1
Ubuntu 16.04
'server'

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions