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

Serving static files with add_static() breaks event loop on windows #1401

Closed
major12 opened this issue Nov 14, 2016 · 4 comments
Closed

Serving static files with add_static() breaks event loop on windows #1401

major12 opened this issue Nov 14, 2016 · 4 comments
Labels

Comments

@major12
Copy link

major12 commented Nov 14, 2016

Long story short

Serving big static file makes impossible to process new requests at this time. Event loop does not pass execution to other coroutines.
If client stops to receive file in the middle of transfer then server becomes broken - it cannot receive/process new requests.

Expected behaviour

Other requests can be processed while file is served.

Actual behaviour

Server hangs for some time or becomes broken forever.

Steps to reproduce

Server:

from aiohttp import web

def index(request):
    return web.Response(text='hello world')

app = web.Application()
app.router.add_get('/', index)
app.router.add_static('/static/', path=str('./test/'))
web.run_app(app)

Client:

import asyncio
from aiohttp import ClientSession

async def download(file):
    async with ClientSession() as session:
        async with session.get(file) as resp:
            while True:
                data = await resp.content.read(1000)
                if not data:
                    break
                await asyncio.sleep(1)

loop = asyncio.get_event_loop()
loop.run_until_complete(download('http://127.0.0.1:8080/static/1.mkv'))
loop.close()
  1. Copy your favourite movie to test/1.mkv
  2. Run server
  3. Try to fetch page http://127.0.0.1:8080/
  4. Run client
  5. Try to fetch page http://127.0.0.1:8080/ one more time

Your environment

aiohttp (1.1.3)
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Win10

@asvetlov
Copy link
Member

Well, sendfile should use aiofiles in fallback code.
sendfile already works in async manner.

@major12
Copy link
Author

major12 commented Nov 15, 2016

After small print-debugging session I found the issue
https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/file_sender.py#L132
Instead of

                count = count - chunk_size
                if count <= 0:
                    break
                chunk = fobj.read(count)

there should be

                count = count - chunk_size
                if count <= 0:
                    break
                chunk = fobj.read(chunk_size)

Could you please include the fix in next release.

@asvetlov
Copy link
Member

Excellent catch!
Fixed by aiohttp 1.1.5

@lock
Copy link

lock bot commented Oct 29, 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.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 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

2 participants