Misc. bug: router mode forwards the child's Connection: close, so keep-alive clients fail with 'other side closed'

Open Beginner friendly
#32,666 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@huihoudantuiren is already working on this.

Since Nov 22, 2026.

  • #32814 by @huihoudantuiren — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Read should_strip_proxy_header() in tools/server/server-models.cpp and check how router mode forwards response headers. Add coverage for the child's Connection: close header if nearby tests exist, then run the relevant server tests. Done means the child connection header is stripped from proxied responses and sequential keep-alive client requests no longer fail.

Written by the indexing model from the issue text.

Description

Name and Version

llama-server built at a963f65 (Vulkan). The code is unchanged on current master (5f83714).

Operating systems

Linux

Which llama.cpp modules do you know to be affected?

llama-server (router mode)

Command line
llama-server --models-dir /models --models-max 1 --parallel 1 --host 0.0.0.0 --port 8080
Problem description & steps to reproduce

In router mode, every proxied response carries Connection: close (from the child) next to the router's own Keep-Alive:

$ curl -s -D - -o /dev/null localhost:8080/v1/chat/completions -H 'Content-Type: application/json' \
    -d '{"model":"<any>","messages":[{"role":"user","content":"hi"}],"max_tokens":5}'
HTTP/1.1 200 OK
Connection: close
Keep-Alive: timeout=5, max=100
...

The router then closes the client connection after each response. Node's fetch/undici, which the OpenAI JS SDK and agents built on it use, reuses the pooled socket anyway. A request sent right after the previous response finishes fails with UND_ERR_SOCKET: other side closed.

Repro (Node 22), sequential streaming requests with no gap:

for (let i = 0; i < 300; i++) {
  try {
    const r = await fetch("http://127.0.0.1:8080/v1/chat/completions", {
      method: "POST", headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ model: "<any>", stream: true, max_tokens: 8,
                             messages: [{ role: "user", content: "hi" }] }) });
    await r.text();
  } catch (e) { console.log(i, e.cause?.code); }
}

Measured results:

  • Directly against the router, about 6% of requests fail when sent within 0-1 ms of the previous response; none fail at gaps of 2 ms or more.
  • Through an SSH tunnel, the close reaches the client later, so the window widens. A coding agent (Pi) hit other side closed and retried up to 38 times in one run. A local proxy that delays the close by 10 ms reproduces this: 45% of requests fail.
  • Sending Connection: close from the client removes the errors (0/300).

Cause: should_strip_proxy_header() in tools/server/server-models.cpp strips the child's keep-alive header but not its connection header. The child's Connection: close only concerns the router's own connection to the child, but it is forwarded to the client. cpp-httplib then closes the client connection (honor_connection_close), while write_response still adds Keep-Alive, since the request itself did not ask to close.

Suggested fix:

     if (header_name == "server" ||
         header_name == "transfer-encoding" ||
         header_name == "content-length" || // quick fix for https://github.com/ggml-org/llama.cpp/issues/17710
-        header_name == "keep-alive") {
+        header_name == "keep-alive" ||
+        header_name == "connection") {

I have not built and tested this patch.

Dominant language
C++
Stars
136k
Forks
26.1k
Avg merge
1d 19h
Merged PRs (30d)
521

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 ggml-org/llama.cpp

All issues in ggml-org/llama.cpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.