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

[Documentation and/or web.run_app] Default host is 0.0.0.0 #3453

Closed
coxley opened this issue Dec 18, 2018 · 5 comments
Closed

[Documentation and/or web.run_app] Default host is 0.0.0.0 #3453

coxley opened this issue Dec 18, 2018 · 5 comments

Comments

@coxley
Copy link

coxley commented Dec 18, 2018

Long story short

  • Warnings for moving away from make_handler suggest Application Runners as the replacement

  • make_handler returned a protocol factory. This factory could be provided to loop.create_server

  • loop.create_server when host=None assumes all interfaces and will return multiple sockets (usually one for each address family)

  • Both web.run_app and web.TCPSite when host=None assume 0.0.0.0

    • Confusingly, web.run_app states:

The server will listen on any host or Unix domain socket path you supply. If no hosts or paths are supplied, or only a port is supplied, a TCP server listening on 0.0.0.0 (all hosts) will be launched.

  • 0.0.0.0 is not inclusive of "all hosts" unless in an IPv4-only environment

Expected behaviour

Two sockets created per port/TCPSite, matching (or at least getting close) the event loop behavior. Using host=localhost does the correct thing in my environments

I1217 165618 base_events.py:1068] <Server sockets=[<socket.socket fd=25, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('127.0.0.1', 8888)>, <socket.socket fd=27, family=AddressFamily.AF_INET6, type=2049, proto=6, laddr=('::1', 8888, 0, 0)>]> is serving

Actual behaviour

One socket is created per TCPSite for 0.0.0.0

Steps to reproduce

import asyncio
import logging
from aiohttp import web


logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


async def sup():
    return web.Response(text="Sup")


async def setup():
    app = web.Application()
    app.add_routes([web.get("/", sup)])

    logging.debug("setting up test servers")
    server_fs = []
    single = web.AppRunner(app)
    await single.setup()
    server_fs.append(web.TCPSite(single, port=8080).start())
    logging.debug("AF_INET to start on: 8080")

    double = web.AppRunner(app)
    await double.setup()
    server_fs.append(web.TCPSite(double, host="localhost", port=8081).start())
    logging.debug("AF_INET + AF_INET6 to start on: 8081")
    return asyncio.ensure_future(asyncio.gather(*server_fs))


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.set_debug(True)
    srv = loop.run_until_complete(setup())
    loop.run_forever()

Output:


$ python  /tmp/whatever.py
DEBUG:root:setting up test servers
DEBUG:root:AF_INET to start on: 8080
DEBUG:root:AF_INET + AF_INET6 to start on: 8081
DEBUG:asyncio:Get address info localhost:8081, type=<SocketKind.SOCK_STREAM: 1>, flags=<AddressInfo.AI_PASSIVE: 1>
INFO:asyncio:<Server sockets=[<socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 8080)>]> is serving
DEBUG:asyncio:Getting address info localhost:8081, type=<SocketKind.SOCK_STREAM: 1>, flags=<AddressInfo.AI_PASSIVE: 1> took 1.300ms: [(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 8081, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 8081))]
DEBUG:asyncio:poll took 0.033 ms: 1 events
INFO:asyncio:<Server sockets=[<socket.socket fd=6, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=6, laddr=('::1', 8081, 0, 0)>, <socket.socket fd=8, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 8081)>]> is serving

Your environment

Linux, 4.11

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #120 (Documentation), #2591 (Documentation for server BaseRequest.host() is wrong), #2739 (Accept a coroutine in web.run_app), #1940 (Content length is 0 by default in aiohttp.web.Response), and #3243 (Enable stdout logging in run_app by default).

@asvetlov
Copy link
Member

I believe one socket per one runner's site is a good idea.
If you want to enable IPv6 please make a second TCPSite for serving ::.
I hope it helps.

@coxley
Copy link
Author

coxley commented Dec 18, 2018

I'd at least document that change of behavior, while subtle, where suggesting to use Application Runners instead. Figured that'd be the want though

@coxley coxley closed this as completed Dec 18, 2018
@asvetlov
Copy link
Member

@coxley pull request for docs update is welcome!

@lock
Copy link

lock bot commented Dec 18, 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 Dec 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants