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

Remove infinity support for tcp listener backlogs #2842

Merged
merged 9 commits into from
Oct 31, 2023
1 change: 1 addition & 0 deletions newsfragments/2842.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed support for ``math.inf`` for the ``backlog`` argument in ``open_tcp_listeners``, making it's docstring correct in the fact that only ``TypeError``s are raised if invalid arguments are passed.
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 14 additions & 11 deletions trio/_highlevel_open_tcp_listeners.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import errno
import math
import sys
import warnings
from collections.abc import Awaitable, Callable
from math import inf

import trio
from trio import TaskStatus
Expand Down Expand Up @@ -41,16 +42,18 @@
# so this is unnecessary -- we can just pass in "infinity" and get the maximum
# that way. (Verified on Windows, Linux, macOS using
# notes-to-self/measure-listen-backlog.py)
def _compute_backlog(backlog: int | float | None) -> int: # noqa: PYI041
def _compute_backlog(backlog: int | None) -> int:
# Many systems (Linux, BSDs, ...) store the backlog in a uint16 and are
# missing overflow protection, so we apply our own overflow protection.
# https://github.com/golang/go/issues/5030
if isinstance(backlog, float):
# TODO: Remove when removing infinity support
# https://github.com/python-trio/trio/pull/2724#discussion_r1278541729
if backlog != inf:
raise ValueError(f"Only accepts infinity, not {backlog!r}")
if backlog == math.inf:
backlog = None
warnings.warn(

Check warning on line 51 in trio/_highlevel_open_tcp_listeners.py

View check run for this annotation

Codecov / codecov/patch

trio/_highlevel_open_tcp_listeners.py#L51

Added line #L51 was not covered by tests
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
"Accepting infinity for backlog for compatibility, please use `None` instead.",
stacklevel=2,
)
if not isinstance(backlog, int) and backlog is not None:
raise TypeError(f"backlog must be an int or None, not {backlog!r}")
if backlog is None:
return 0xFFFF
return min(backlog, 0xFFFF)
Expand All @@ -60,7 +63,7 @@
port: int,
*,
host: str | bytes | None = None,
backlog: int | float | None = None, # noqa: PYI041
backlog: int | None = None,
) -> list[trio.SocketListener]:
"""Create :class:`SocketListener` objects to listen for TCP connections.

Expand Down Expand Up @@ -93,8 +96,8 @@
all interfaces, pass the family-specific wildcard address:
``"0.0.0.0"`` for IPv4-only and ``"::"`` for IPv6-only.

backlog (int, math.inf, or None): The listen backlog to use. If you leave this as
``None`` or ``math.inf`` then Trio will pick a good default. (Currently: whatever
backlog (int or None): The listen backlog to use. If you leave this as
``None`` then Trio will pick a good default. (Currently: whatever
your system has configured as the maximum backlog.)

Returns:
Expand Down Expand Up @@ -172,7 +175,7 @@
port: int,
*,
host: str | bytes | None = None,
backlog: int | float | None = None, # noqa: PYI041
backlog: int | None = None,
handler_nursery: trio.Nursery | None = None,
task_status: TaskStatus[list[trio.SocketListener]] = trio.TASK_STATUS_IGNORED,
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions trio/_highlevel_ssl_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def open_ssl_over_tcp_listeners(
*,
host: str | bytes | None = None,
https_compatible: bool = False,
backlog: int | float | None = None, # noqa: PYI041
backlog: int | None = None,
) -> list[trio.SSLListener]:
"""Start listening for SSL/TLS-encrypted TCP connections to the given port.

Expand Down Expand Up @@ -101,7 +101,7 @@ async def serve_ssl_over_tcp(
*,
host: str | bytes | None = None,
https_compatible: bool = False,
backlog: int | float | None = None, # noqa: PYI041
backlog: int | None = None,
handler_nursery: trio.Nursery | None = None,
task_status: trio.TaskStatus[list[trio.SSLListener]] = trio.TASK_STATUS_IGNORED,
) -> NoReturn:
Expand Down
6 changes: 2 additions & 4 deletions trio/_tests/test_highlevel_open_tcp_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import errno
import socket as stdlib_socket
import sys
from math import inf
from socket import AddressFamily, SocketKind
from typing import TYPE_CHECKING, Any, Sequence, overload

Expand Down Expand Up @@ -368,7 +367,6 @@ async def test_open_tcp_listeners_backlog() -> None:
tsocket.set_custom_socket_factory(fsf)
for given, expected in [
(None, 0xFFFF),
(inf, 0xFFFF),
(99999999, 0xFFFF),
(10, 10),
(1, 1),
Expand All @@ -385,6 +383,6 @@ async def test_open_tcp_listeners_backlog_float_error() -> None:
tsocket.set_custom_socket_factory(fsf)
for should_fail in (0.0, 2.18, 3.14, 9.75):
with pytest.raises(
ValueError, match=f"Only accepts infinity, not {should_fail!r}"
TypeError, match=f"backlog must be an int or None, not {should_fail!r}"
):
await open_tcp_listeners(0, backlog=should_fail)
await open_tcp_listeners(0, backlog=should_fail) # type: ignore[arg-type]
Loading