[PR #4517] [MERGED] Save: keep at most 256 region files open, like vanilla (fixes "Too many open files" crashes) #5668

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

📋 Pull Request Information

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

Base: main ← Head: fix/region-cache-limit


📝 Commits (1)

  • e0cb05b Save: keep at most 256 region files open, like vanilla

📊 Changes

3 files changed (+144 additions, -9 deletions)

View changed files

📝 crates/horizon-client/Cargo.toml (+1 -1)
📝 crates/horizon-client/src/main.rs (+37 -0)
📝 crates/horizon-client/src/save.rs (+106 -8)

📄 Description

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

Why

On 2026-10-30, the official server (v0.2.97, ~55 players) crashed four times (04:33, 05:08, 09:16, 16:01), each time after Too many open files (os error 24). The order was always the same:

  1. horizon-region-writer fails to open region, entities and poi files (in one session: 335 region, 129 entities and 42 poi failures, 35 minutes after the start).
  2. A save fails.
  3. A tick error kills the server.

The cause is in save.rs. Its region cache opened every region, entities and poi file that a dimension ever touched, and never closed any of them. Players who travel (teleports, elytra, exploration) add new files without end, until the process reaches its descriptor limit. Nothing between v0.2.97 and main had changed this.

What

  • Cache cap. Each region cache keeps at most 256 files open. This is RegionFileStorage.MAX_CACHE_SIZE: vanilla closes the least recently used file when it opens the 257th. Here, the least recently used file that no caller still holds is dropped, and it is reopened if needed. Like vanilla, the cap applies to each storage (region, entities, poi) of each dimension.
  • Open-file limit. At startup, the soft open-file limit is raised to the hard limit (capped at 10240 on macOS). The JVM does this with -XX:+MaxFDLimit, so a vanilla server never runs at a container's 1024. The server logs one line when it raises the limit.

Proof

  • New test. save::tests::region_files_kept_open_stay_under_vanillas_cache_size opens 600 regions and counts /dev/fd, checks that a held region stays cached, then saves entities across 300 regions and reads them back.
    • On main it fails with "601 descriptors more".
    • With the fix, the open count stays at or under 256, and every region reads back.
  • Bots, main build. A dedicated server on the Mac mini, cycles of 20 explorer bots that join, explore for 15 s and leave (80 joins). It counted with lsof:
    • descriptors: 17 → 48 → 76 → 106 → 137;
    • .mca files: 2 → 122;
    • sockets: stayed at 1 the whole time, so the leak is region files, not connections.
  • Bots, fixed build. The same growth while under the cap (2 → 127 .mca). The bots didn't reach 256 distinct regions per storage in that run; the unit test covers the cap itself.

Follow-ups, not in this PR:

  • A failed save is fatal here, while vanilla only logs it.
  • On a dedicated server, the crash report's head says "Render thread".

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

Check Result
Scope ✅ 3 files, +144 −9
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 481 passed (59 s)
rustfmt ✅ (5 s)
clippy ✅ (2 min 43 s)
tests: horizon-client ✅ 1065 passed (6 min 58 s)
doc tests ✅ 0 passed (3 s)
content root tests ➖ content root untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 92 affected, to run in CI (--quick): ambient_sound, arrow, audio_attributes, block_removal, block_tint, camera, cloud_render, command_exec, composter, container_screen, container_title, crafter and 80 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 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/4517 **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/region-cache-limit` --- ### 📝 Commits (1) - [`e0cb05b`](https://github.com/horizon-rust/horizon/commit/e0cb05bfcedee4654681d1e813f1078ce047a8c0) Save: keep at most 256 region files open, like vanilla ### 📊 Changes **3 files changed** (+144 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/Cargo.toml` (+1 -1) 📝 `crates/horizon-client/src/main.rs` (+37 -0) 📝 `crates/horizon-client/src/save.rs` (+106 -8) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## Why On 2026-10-30, the official server (v0.2.97, ~55 players) crashed four times (04:33, 05:08, 09:16, 16:01), each time after `Too many open files (os error 24)`. The order was always the same: 1. `horizon-region-writer` fails to open region, entities and poi files (in one session: 335 region, 129 entities and 42 poi failures, 35 minutes after the start). 2. A save fails. 3. A tick error kills the server. The cause is in `save.rs`. Its region cache opened every region, entities and poi file that a dimension ever touched, and never closed any of them. Players who travel (teleports, elytra, exploration) add new files without end, until the process reaches its descriptor limit. Nothing between v0.2.97 and main had changed this. ## What - **Cache cap.** Each region cache keeps at most 256 files open. This is `RegionFileStorage.MAX_CACHE_SIZE`: vanilla closes the least recently used file when it opens the 257th. Here, the least recently used file that no caller still holds is dropped, and it is reopened if needed. Like vanilla, the cap applies to each storage (region, entities, poi) of each dimension. - **Open-file limit.** At startup, the soft open-file limit is raised to the hard limit (capped at 10240 on macOS). The JVM does this with `-XX:+MaxFDLimit`, so a vanilla server never runs at a container's 1024. The server logs one line when it raises the limit. ## Proof - **New test.** `save::tests::region_files_kept_open_stay_under_vanillas_cache_size` opens 600 regions and counts `/dev/fd`, checks that a held region stays cached, then saves entities across 300 regions and reads them back. - On main it fails with "601 descriptors more". - With the fix, the open count stays at or under 256, and every region reads back. - **Bots, main build.** A dedicated server on the Mac mini, cycles of 20 explorer bots that join, explore for 15 s and leave (80 joins). It counted with lsof: - descriptors: 17 → 48 → 76 → 106 → 137; - `.mca` files: 2 → 122; - sockets: stayed at 1 the whole time, so the leak is region files, not connections. - **Bots, fixed build.** The same growth while under the cap (2 → 127 `.mca`). The bots didn't reach 256 distinct regions per storage in that run; the unit test covers the cap itself. Follow-ups, not in this PR: - A failed save is fatal here, while vanilla only logs it. - On a dedicated server, the crash report's head says "Render thread". **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 8f376a471, 11 min 19 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +144 −9 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 481 passed (59 s) | | rustfmt | ✅ (5 s) | | clippy | ✅ (2 min 43 s) | | tests: horizon-client | ✅ 1065 passed (6 min 58 s) | | doc tests | ✅ 0 passed (3 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 92 affected, to run in CI (--quick): ambient_sound, arrow, audio_attributes, block_removal, block_tint, camera, cloud_render, command_exec, composter, container_screen, container_title, crafter and 80 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 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:14:12 +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#5668
No description provided.