Skip to content

Enable TCP_NODELAY by default for TCP sockets (Disable Nagle Algorithm) #144254

Description

@morotti

Bug report

Bug description:

Hello,

Opening a bug ticket before I send a PR.

I just spent yet another day debugging a slow Python application. A task that took 8 seconds out of the box, took 0.3 seconds after adding TCP_NODELAY.
Nothing special, just two processes talking over a localhost TCP socket.

For people not familiar with the matter, nagle algorithm adds a small delay (up to 500ms depending on OS) before sending a packet in some circumstances, this can have a catastrophic effect on network applications depending on their read/write patterns.

This has been a long-standing recurring issue across many applications and libraries. I'd like to set TCP_NODELAY by default on all TCP sockets created by python (socket module), to effectively disables Nagle's algorithm by default.

The last python discussion on this topic was in 2016 https://bugs.python.org/issue27456
There was clear consensus that nagle algorithm should be disabled by default.
This lead to a few patches in built-in modules and third party packages (asyncio and uvloop), some related tickets: python/asyncio#286 python/asyncio#373

10 year have passed since that last discussion.
The bug is still showing up regularly both in the python interpreter and in popular libraries. ( a quick search immediately shows that http.server is affected #108736 )
We should fix it.

For python, I think we can set TCP_NODELAY when a socket is created in the socket module. It's just a few lines of code to change.
Thoughts?

The rest of the world has been actively setting TCP_NODELAY to mitigate the issue:

Regards

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Jan 26, 2026
  2. ericvsmith commented on Jan 27, 2026

    @ericvsmith
    Member

    This is a feature request, and should be discussed on discuss.python.org

  3. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jan 27, 2026
  4. ericvsmith commented on Jan 28, 2026

    @ericvsmith
    Member

    I'm going to close this until a discussion on discuss.python.org is held, and consensus is that the feature should be implemented. I think the 9+ year old issue on b.p.o. is too old to rely on.

    I'll note this isn't a bug because it doesn't disagree with the documentation, to my knowledge. If the documentation somewhere says that the Nagle algorithm is disabled by default, then please point out where.

    Personally, I'm +0.5 on the change.

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

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions