Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamReaderPayload is broken #2557

Closed
asvetlov opened this issue Nov 24, 2017 · 9 comments
Closed

StreamReaderPayload is broken #2557

asvetlov opened this issue Nov 24, 2017 · 9 comments
Labels

Comments

@asvetlov
Copy link
Member

asvetlov commented Nov 24, 2017

It works for sending data from request.content to response like return web.Response(body=request.content) but pretty useless for sending data from client HTTP request like

async def test_stream_reader(test_client):
    DATA = b'1234567890' * (2**16)
    async def h1(request):
        return web.Response(body=DATA)

    async def h2(request):
        async with aiohttp.ClientSession() as sess:
            async with sess.get(client.make_url('/h1')) as resp:
                return web.Response(body=resp.content)

    app = web.Application()
    app.router.add_get('/h1', h1)
    app.router.add_get('/h2', h2)

    client = await test_client(app)
    resp = await client.get('/h2')
    assert await resp.read() == DATA

Obviously resp.content from h2 web handler is closed before sending response body.

web.Response(body=request.content) is very rare usage -- usually web site does some processing of request BODY instead of just returning it back to client untouched.

I doubt if we need this payload writer at all.

@fafhrd91 what is the use case?

@fafhrd91
Copy link
Member

Problem is not in StreamReaderPayload but in ClientSession. I think we discussed this in some issue.

Main reason was for proxy style endpoints. Actually because of client session you can not do proper streaming processing otherwise StreamReaderPayload should work

@asvetlov
Copy link
Member Author

Do you have an evidence of real use case?

The problem not in client session but in ClientResponse -- the response is closed in web handler before starting StreamReadingPayload machinery. But the response should be closed on exit from web handler -- otherwise lifetime of such objects becomes too complicated. Transferring ownership from web-handler to StreamReaderPayload looks too error-prone.

For proxying the payload is useless: it could be safely applied to request.content only, all other streams are closed on exit from web-handler, isn't it?

@fafhrd91
Copy link
Member

You can remove it

asvetlov added a commit that referenced this issue Nov 25, 2017
* Fix for #2557: Drop StreamReader payload

* Add changenote

* Drop failed tests
@asvetlov
Copy link
Member Author

Fixed by #2558

@arthurdarcet
Copy link
Contributor

arthurdarcet commented Mar 27, 2018

@asvetlov I don't see how the "proxy" usecase works now, could you write an exemple?
Something to handle this kind of requests:

async def handle(request):
    async with aiohttp.ClientSession() as sess:
        async with sess.put('some external endpoint', data=request.content) as resp:
            pass
        return web.Response(body=b'ok')

request.content could be a very large body, and loading it in RAM first is not an option (and would be very inefficient anyway)

--

The mirrored usage is useful too, for instance an API service that is doing authentication but then forwarding data sent by an external service:

async def handle(request):
    resp = web.StreamResponse()
    await resp.prepare(request)
    async with aiohttp.ClientSession() as sess:
        async with sess.get('some external endpoint') as external:
            await resp.write(external.content)
    return resp

Here again, the external service could return very large amount of data, or could be streaming data slowly, and exposing properly that to the client is very useful

@asvetlov
Copy link
Member Author

@arthurdarcet you are just in time :)
I've initiated a new aiohttp 3.1.1 release.
Please wait for release bot finish and run pip install -U aiohttp :)

@arthurdarcet
Copy link
Contributor

Perfect, thanks.

@asvetlov
Copy link
Member Author

Sure

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants