`TCP.Socket.noDelay` docstring says it "Enables the Nagle algorithm", but it sets `TCP_NODELAY` (disables Nagle)

Open Beginner friendly
#16,816 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
90/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Domain
documentation

Research direction

Read the docstrings in src/Std/Internal/UV/TCP.lean and src/Std/Async/TCP.lean, then locate the reference manual entry mentioned in the issue. Update the descriptions to say that noDelay disables the Nagle algorithm by setting TCP_NODELAY. Done when all three docstrings and the manual describe the behavior consistently.

Written by the indexing model from the issue text.

Description

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
    (Not a soundness bug.)
Description

src/runtime/uv/tcp.cpp:765 calls uv_tcp_nodelay(..., 1), which sets TCP_NODELAY and so disables Nagle. This is what a function named noDelay is meant to do (libuv: "Enable TCP_NODELAY, which disables Nagle's algorithm"), and what its in-tree caller Std.Http.Server (src/Std/Http/Server.lean:156) wants. The docstring at src/Std/Internal/UV/TCP.lean:149, also copied into the Std.Async.TCP wrappers (src/Std/Async/TCP.lean:130 and :267) and the reference manual, says the opposite ("Enables the Nagle algorithm"). The mismatch is real, but the docstring is the wrong side, not the code: passing 0 would break the function's purpose. A one-line docstring fix (e.g. "Disables the Nagle algorithm (sets TCP_NODELAY)") resolves it.

Documented behavior it contradicts:

  • Std.Internal.UV.TCP.Socket.noDelay (src/Std/Internal/UV/TCP.lean:149):

    Enables the Nagle algorithm for a TCP socket.

  • Std.Async.TCP.Socket.Server.noDelay (src/Std/Async/TCP.lean:130):

    Enables the Nagle algorithm for all client sockets accepted by this server socket.

  • Std.Async.TCP.Socket.Client.noDelay (src/Std/Async/TCP.lean:267):

    Enables the Nagle algorithm for the client socket.

Context
  • Affected function: lean_uv_tcp_nodelay (src/runtime/uv/tcp.cpp) and the docstrings above
  • Upstream: no existing issue found; the cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce

The repro reads the socket option back with getsockopt from a tiny C helper, so it does not depend on timing.

  1. Save the following as helper.c:
#include <lean/lean.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
/* Prints TCP_NODELAY for every open TCP socket fd of this process. */
LEAN_EXPORT lean_obj_res dump_nodelay(lean_obj_arg w) {
  for (int fd = 3; fd < 256; fd++) {
    int type; socklen_t len = sizeof(type);
    if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) != 0 || type != SOCK_STREAM) continue;
    int v = -1; len = sizeof(v);
    if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, &len) == 0)
      printf("  fd %d: TCP_NODELAY=%d\n", fd, v);
    else
      printf("  fd %d: getsockopt(TCP_NODELAY) failed: %s\n", fd, strerror(errno));
  }
  fflush(stdout);
  return lean_io_result_mk_ok(lean_box(0));
}
  1. Save the following as input.lean (imports only Init/Std):
import Std.Internal.UV.TCP
open Std.Net Std.Internal.UV

/-- Prints `TCP_NODELAY` (via `getsockopt`) for every open TCP socket of the process. -/
@[extern "dump_nodelay"] opaque dumpNoDelay : IO Unit

def main : IO Unit := do
  let server ← TCP.Socket.new
  server.bind (SocketAddressV4.mk (.ofParts 127 0 0 1) 0)
  server.listen 4
  let ap ← server.accept
  let client ← TCP.Socket.new
  let p ← client.connect (← server.getSockName)
  IO.ofExcept (← IO.wait p.result!)
  let conn ← IO.ofExcept (← IO.wait ap.result!)
  IO.println "before noDelay:"
  dumpNoDelay
  client.noDelay
  conn.noDelay
  IO.println "after noDelay on both connected sockets:"
  dumpNoDelay
  -- keep all three sockets alive until here
  client.close; conn.close; server.close
  1. Compile: lean -c main.c input.lean && cc -c -I$(lean --print-prefix)/include helper.c -o helper.o && leanc -o prog main.c helper.o
    (helper.c is compiled with the system C compiler because it needs the system socket headers.)
  2. Run: ./prog

Expected behavior (per the docstring, Nagle stays enabled, i.e. TCP_NODELAY stays 0):

before noDelay:
  fd 8: TCP_NODELAY=0
  fd 9: TCP_NODELAY=0
  fd 10: TCP_NODELAY=0
after noDelay on both connected sockets:
  fd 8: TCP_NODELAY=0
  fd 9: TCP_NODELAY=0
  fd 10: TCP_NODELAY=0

Actual behavior (fd 8 is the listening socket; macOS reports a set flag as 4):

before noDelay:
  fd 8: TCP_NODELAY=0
  fd 9: TCP_NODELAY=0
  fd 10: TCP_NODELAY=0
after noDelay on both connected sockets:
  fd 8: TCP_NODELAY=0
  fd 9: TCP_NODELAY=4
  fd 10: TCP_NODELAY=4

i.e. noDelay turns TCP_NODELAY on, which disables the Nagle algorithm.

Versions

Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)

Additional Information

Root cause analysis. The whole deviation is the literal 1 passed to uv_tcp_nodelay at src/runtime/uv/tcp.cpp:765 (TCP_NODELAY on = Nagle off). The closed-socket check, the event_loop_guard and the result construction are fine, and the return value is an OK unit either way, so you cannot see the deviation from the return value alone.

Suggested fix: keep the code and correct the three docstrings (and the reference manual entry) to say that noDelay disables the Nagle algorithm by setting TCP_NODELAY.

Impact

A reader who believes the docstring is misled about what noDelay does. The runtime behavior is the intended low-latency one.

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.