Skip to content

Add TCP keepalive option for asyncio.start_server() #101336

Closed

Description

@beavailable

By default, this asyncio.start_server() method will create a list of socket objects usually for both IPv4 and IPv6 (if possible) and do not set keepalive option. if I want I can create a socket object by myself and set keepalive option for it, then pass it in start_server(), but if so, I will not be able to start a server which listens on both IPv4 and IPv6 addresses.
I hope we can add a keepalive parameter for asyncio.start_server() or do something else to make things easier.

Linked PRs

Activity

  1. axoroll7 commented on Aug 15, 2023

    @axoroll7

    I don't have the same problem as you, but I understand your problem. It is a recurrent problem for me. For many projects, not only web servers, but others TCP servers too. The same problem appears with others programming languages. To alleviate this problem in Python, I thought about monkey-patching the library in question, or the socket library to use TCP keepalive. The mode being applied before bind and connect. Can TCP keepalive be applied after bind, connect or the first interaction ? In addition to this, it is difficult to access the internal sockets.

    For some software prototypes, (never in production), I patched some methods of the socket object to add TCP keepalive, for most OSes :

    Function signature :

    def tcp_keepalive(
            sock:socket.socket,
            idle:int,
            interval:int,
            max:Optional[None] = None,
        ) -> None:
        pass

    For linux :

    sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
    sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle)
    sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
    if max is not None:
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max)

    For BSD (macOS, FreeBSD, OpenBSD, NetBSD) :

    # BSD offers only one parameter
    TCP_KEEPALIVE = 0x10
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
    sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPALIVE, idle)

    For Windows :

    # max is not configurable on Windows
    sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, idle * 1000, interval * 1000))

    (English isn't my first language, so please excuse any mistakes.)

  2. beavailable commented on Aug 15, 2023

    @beavailable
    ContributorAuthor

    Can TCP keepalive be applied after bind, connect or the first interaction ?

    Seems that it can't.

  3. gvanrossum commented on Nov 27, 2023

    @gvanrossum
    Member

    Please just propose a pull request that adds a keyword parameter to the right API. We will take it from there.

  4. added a commit that references this issue on Dec 13, 2023
  5. moved this from Todo to Done in asyncioon Dec 13, 2023
  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions