[PR #4217] [MERGED] Server: time out clients that stop reading (keep-alive checked on the game thread) #5491

Closed
opened 2026-11-03 05:12:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4217
Author: @ludovic111
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @ludovic111

Base: main ← Head: fix/keepalive-timeout


📝 Commits (2)

  • 5fb90b4 Server: time out a client that stops answering keep-alives, even while its writer is blocked
  • c3601b7 Server: the game thread is the keep-alive timeout's only judge; kick, then close a second later

📊 Changes

3 files changed (+112 additions, -19 deletions)

View changed files

📝 crates/horizon-client/src/lan/conn.rs (+80 -19)
📝 crates/horizon-client/src/lan/hub.rs (+20 -0)
📝 crates/horizon-protocol/src/connection.rs (+12 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Part of #4179: a client that stops reading kept its player slot, its threads and its connection for as long as it liked.

Cause. The play connection's writer thread both sent the keep-alives and timed the client out. When a client stops reading while the server keeps writing to it, the socket fills and that thread blocks inside a write. It never reaches its keep-alive check. Before this PR, such a client stayed connected until it closed the connection itself.

Fix. The keep-alive clock (the id of the last answered keep-alive, in ms since the connection started) is shared with the game thread, which checks every player once a second:

  • No keep-alive answered of what was sent in the last 30 s (vanilla's keepConnectionAlive: a keep-alive 15 s after the last answered one, then 15 s to answer it): kick with disconnect.timeout. The log says "lost connection: Timed out", as vanilla's does.
  • Still there a second later: the socket is closed under the writer (new horizon_protocol::connection::Closer, a shutdown from another thread). The blocked write fails, both connection threads end, and the slot is released.

The writer thread still sends the keep-alives and measures latency. It no longer times out by itself; it used to race the game thread and log "Disconnected".

A/B (playhorizon, Linux, CPUs 0-3 exclusive)

Setup: 20 mixed horizon-bots plus 5 --no-read bots that each ask for 20 command suggestions a tick, so the server keeps writing to sockets nobody reads (#4181). 30 s warm-up, 60 s window, max-players=100, dedicated build --profile fast --no-default-features. Main is bd9b32587, without #4200, so the answers are many.

Non-reading bots dropped after Log reason TPS MSPT avg tick_lan ms
main, round 1 61 s (only when the bots quit at the end of the run) Disconnected 18.0 54.9 21.8
main, round 2 61 s (only when the bots quit at the end of the run) Disconnected 17.9 55.5 21.6
this PR, round 1 31–32 s Timed out 19.3 40.6 12.4
this PR, round 2 32 s Timed out 18.7 48.7 14.0

Two more cases:

  • Quiet clients: with 30 bots that stop reading and send nothing that needs an answer, main already dropped them at 30 s; its writer wasn't blocked. This PR drops them at 31 s with "Timed out" (30 of 30), against "Disconnected" on main.
  • Memory: server RSS stayed within the run-to-run noise in both builds (971–1057 MB), so these clients cost slots and threads, not memory.

Unit test a_silent_client_times_out_and_closes: the 30 s rule, and a read blocked on a connection ends once it is closed.

pr_ready

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at bd9b32587, 3 min 8 s)

Check Result
Scope ✅ 3 files, +112 −19
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 480 passed (49 s)
rustfmt ✅ (7 s)
clippy ✅ (12 s)
tests: horizon-client, horizon-protocol ✅ 1081 passed (1 min 36 s)
doc tests ✅ 0 passed (1 s)
Parity ➖ 33 affected, to run in CI (--quick): block_removal, command_exec, composter, crafter, cursor_death, dedicated_server, dispenser, forced_end, friends_integration, hardcore_respawn, lan_integration, locator_bar and 21 more

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/4217 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/keepalive-timeout` --- ### 📝 Commits (2) - [`5fb90b4`](https://github.com/horizon-rust/horizon/commit/5fb90b440cf2376ad1c48592bff236091072138b) Server: time out a client that stops answering keep-alives, even while its writer is blocked - [`c3601b7`](https://github.com/horizon-rust/horizon/commit/c3601b74f3d02ae26b5699a4e0c287115d51c057) Server: the game thread is the keep-alive timeout's only judge; kick, then close a second later ### 📊 Changes **3 files changed** (+112 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/conn.rs` (+80 -19) 📝 `crates/horizon-client/src/lan/hub.rs` (+20 -0) 📝 `crates/horizon-protocol/src/connection.rs` (+12 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Part of #4179: a client that stops reading kept its player slot, its threads and its connection for as long as it liked. **Cause.** The play connection's writer thread both sent the keep-alives and timed the client out. When a client stops reading while the server keeps writing to it, the socket fills and that thread blocks inside a write. It never reaches its keep-alive check. Before this PR, such a client stayed connected until it closed the connection itself. **Fix.** The keep-alive clock (the id of the last answered keep-alive, in ms since the connection started) is shared with the game thread, which checks every player once a second: - No keep-alive answered of what was sent in the last 30 s (vanilla's `keepConnectionAlive`: a keep-alive 15 s after the last answered one, then 15 s to answer it): kick with `disconnect.timeout`. The log says "lost connection: Timed out", as vanilla's does. - Still there a second later: the socket is closed under the writer (new `horizon_protocol::connection::Closer`, a shutdown from another thread). The blocked write fails, both connection threads end, and the slot is released. The writer thread still sends the keep-alives and measures latency. It no longer times out by itself; it used to race the game thread and log "Disconnected". ## A/B (playhorizon, Linux, CPUs 0-3 exclusive) Setup: 20 `mixed` horizon-bots plus 5 `--no-read` bots that each ask for 20 command suggestions a tick, so the server keeps writing to sockets nobody reads (#4181). 30 s warm-up, 60 s window, `max-players=100`, dedicated build `--profile fast --no-default-features`. Main is bd9b32587, without #4200, so the answers are many. | | Non-reading bots dropped after | Log reason | TPS | MSPT avg | `tick_lan` ms | |---|---|---|---|---|---| | main, round 1 | 61 s (only when the bots quit at the end of the run) | Disconnected | 18.0 | 54.9 | 21.8 | | main, round 2 | 61 s (only when the bots quit at the end of the run) | Disconnected | 17.9 | 55.5 | 21.6 | | this PR, round 1 | **31–32 s** | **Timed out** | 19.3 | 40.6 | 12.4 | | this PR, round 2 | **32 s** | **Timed out** | 18.7 | 48.7 | 14.0 | Two more cases: - **Quiet clients:** with 30 bots that stop reading and send nothing that needs an answer, main already dropped them at 30 s; its writer wasn't blocked. This PR drops them at 31 s with "Timed out" (30 of 30), against "Disconnected" on main. - **Memory:** server RSS stayed within the run-to-run noise in both builds (971–1057 MB), so these clients cost slots and threads, not memory. Unit test `a_silent_client_times_out_and_closes`: the 30 s rule, and a read blocked on a connection ends once it is closed. <details><summary>pr_ready</summary> **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at bd9b32587, 3 min 8 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +112 −19 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 480 passed (49 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (12 s) | | tests: horizon-client, horizon-protocol | ✅ 1081 passed (1 min 36 s) | | doc tests | ✅ 0 passed (1 s) | | Parity | ➖ 33 affected, to run in CI (--quick): block_removal, command_exec, composter, crafter, cursor_death, dedicated_server, dispenser, forced_end, friends_integration, hardcore_respawn, lan_integration, locator_bar and 21 more | </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 05:12:57 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#5491
No description provided.