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

fixed ValueError for AF_INET6 sockets #2431

Merged
merged 4 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2431.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed ValueError for AF_INET6 sockets if a preexisting INET6 socket to the `aiohttp.web.run_app` function.
2 changes: 1 addition & 1 deletion aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _make_server_creators(handler, *, loop, ssl_context,
if hasattr(socket, 'AF_UNIX') and sock.family == socket.AF_UNIX:
uris.append('{}://unix:{}:'.format(scheme, sock.getsockname()))
else:
host, port = sock.getsockname()
host, port = sock.getsockname()[:2]
uris.append(str(base_url.with_host(host).with_port(port)))
return server_creations, uris

Expand Down
35 changes: 35 additions & 0 deletions tests/test_run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
)
del _has_unix_domain_socks, _abstract_path_failed

HAS_IPV6 = socket.has_ipv6
if HAS_IPV6:
# The socket.has_ipv6 flag may be True if Python was built with IPv6
# support, but the target system still may not have it.
# So let's ensure that we really have IPv6 support.
try:
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
except OSError:
HAS_IPV6 = False


# tokio event loop does not allow to override attributes
def skip_if_no_dict(loop):
Expand Down Expand Up @@ -451,6 +461,31 @@ def test_run_app_preexisting_inet_socket(loop, mocker):
assert "http://0.0.0.0:{}".format(port) in printer.call_args[0][0]


@pytest.mark.skipif(not HAS_IPV6, reason="IPv6 is not available")
def test_run_app_preexisting_inet6_socket(loop, mocker):
skip_if_no_dict(loop)

mocker.spy(loop, 'create_server')

app = web.Application()
mocker.spy(app, 'startup')

sock = socket.socket(socket.AF_INET6)
with contextlib.closing(sock):
sock.bind(('::', 0))
port = sock.getsockname()[1]

printer = mock.Mock(wraps=stopper(loop))
web.run_app(app, loop=loop, sock=sock, print=printer)

assert not loop.is_closed()
loop.create_server.assert_called_with(
mock.ANY, sock=sock, backlog=128, ssl=None
)
app.startup.assert_called_once_with()
assert "http://:::{}".format(port) in printer.call_args[0][0]


@skip_if_no_unix_socks
def test_run_app_preexisting_unix_socket(loop, mocker):
skip_if_no_dict(loop)
Expand Down