[PR #5103] [MERGED] Rewrite the whole region header on a chunk write (#5064, point 8) #6075

Closed
opened 2026-11-03 06:11:25 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: fix/region-header-rewrite


📝 Commits (3)

  • 9f0d819 Rewrite the whole region header on a chunk write (#5064)
  • a9387ea Merge main: keep both region tests
  • 5a44e81 Merge origin/main into fix/region-header-rewrite

📊 Changes

2 files changed (+42 additions, -8 deletions)

View changed files

📝 crates/horizon-world/src/region.rs (+13 -8)
📝 crates/horizon-world/tests/world_io.rs (+29 -0)

📄 Description

🤖 Opened by k3yh's AI agent (Claude Code, Claude Opus 5.5), not by k3yh personally.

What and why

Refs #5064, point 8 only. Points 3, 5 and 6 are in #5090, #5085 and #5077. Points 1, 2, 4 and 7 are not touched.

When vanilla's RegionFile constructor opens a file, it drops header entries it finds invalid: a sector overlapping the header, no sectors, or past the end of the file. It logs a warning for each and zeroes the offset in memory. RegionFile.write and clear then end in writeHeader, which writes both 4096-byte header sectors from memory at position 0. So after the first write those entries are zero on disk as well. I checked this in the 26.3 bytecode with javap.

Horizon's RegionFile::from_file already zeroes those entries in memory. But write and clear wrote only the touched slot's offset and timestamp (write_header_entry), so the invalid entries stayed in the file for other tools to read.

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Change

region.rs: write_header replaces write_header_entry. It builds both header sectors from offsets and timestamps and writes them at position 0, as writeHeader does. write and clear call it. That is 8 KiB per chunk write instead of 16 bytes, the same as vanilla.

Vanilla reference

  • RegionFile.<init>: the "has invalid sector at index" / "size has to be > 0" / "is out of bounds" warnings, then offsets.put(i, 0).
  • RegionFile.write(ChunkPos, ByteBuffer) and RegionFile.clear(ChunkPos) → writeHeader(), which writes header with FileChannel.write(header, 0).

Test

horizon-world/tests/world_io.rs::a_chunk_write_rewrites_the_whole_region_header:

  • Setup: an 8192-byte region whose header has slot 5 = 00000101 (sector 1, the issue's repro), slot 50 = 00000500 (no sectors) and slot 100 = 00006401 (past the end), plus a timestamp on slot 5.
  • One chunk write.
  • After it, the three slots are zero and slot 5's timestamp is kept.
  • Fails on main ("slot 5: left [0, 0, 1, 1]") and passes here.

I wrote the same file with vanilla 26.3's own RegionFile (getChunkDataOutputStream + NbtIo.write, from a small Java program on the 26.3 jar) and with Horizon's RegionFile::write:

header bytes after one chunk write vanilla 26.3 main this branch
slot 0 (written) 00000201 00000201 00000201
slot 5 (overlaps the header) 00000000 00000101 00000000
slot 50 (no sectors) 00000000 00000500 00000000
slot 100 (past the end) 00000000 00006401 00000000
slot 5 timestamp 00003039 00003039 00003039

Vanilla also logged its three "invalid sector" warnings for that file. All horizon-world tests pass, and so do horizon-client's save:: and chunk_io tests (49).

Overlap with #5077 (point 6, also mine): both append a test to the end of tests/world_io.rs, so whichever merges second has a conflict there. The fix is to keep both tests, and I'll rebase whichever is left.

Self-check

pr_ready (tools/pr_ready.py --quick --base c3905131b on DESKTOP-S9O4938, against c3905131b at c3905131b, 2 min 24 s)

Check Result
Scope ✅ 2 files, +42 −8
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ❌ 495 run, FAILED (errors=2, skipped=1) (52 s)
rustfmt ✅ (7 s)
clippy ✅ (14 s)
tests: horizon-world ✅ 317 passed (1 min 7 s)
doc tests ✅ 0 passed (1 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 ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The two Python errors come from files this PR doesn't touch, and both also failed on main on this machine:

  • test_port_ledger cannot import tests.test_class_index.
  • test_perf_bench's windows_version() gets nothing from cmd /C ver on a Spanish Windows.

🤖 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/5103 **Author:** [@k3yh](https://github.com/k3yh) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/region-header-rewrite` --- ### 📝 Commits (3) - [`9f0d819`](https://github.com/horizon-rust/horizon/commit/9f0d819f6ba6392fcde3b6ec730be663a2114936) Rewrite the whole region header on a chunk write (#5064) - [`a9387ea`](https://github.com/horizon-rust/horizon/commit/a9387ea36720eadb1253a415b72b3f05773aa747) Merge main: keep both region tests - [`5a44e81`](https://github.com/horizon-rust/horizon/commit/5a44e816c63ca73e75f800102efdad65f92c384f) Merge origin/main into fix/region-header-rewrite ### 📊 Changes **2 files changed** (+42 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-world/src/region.rs` (+13 -8) 📝 `crates/horizon-world/tests/world_io.rs` (+29 -0) </details> ### 📄 Description > 🤖 Opened by k3yh's AI agent (Claude Code, Claude Opus 5.5), not by k3yh personally. ## What and why Refs #5064, point 8 only. Points 3, 5 and 6 are in #5090, #5085 and #5077. Points 1, 2, 4 and 7 are not touched. When vanilla's `RegionFile` constructor opens a file, it drops header entries it finds invalid: a sector overlapping the header, no sectors, or past the end of the file. It logs a warning for each and zeroes the offset in memory. `RegionFile.write` and `clear` then end in `writeHeader`, which writes both 4096-byte header sectors from memory at position 0. So after the first write those entries are zero on disk as well. I checked this in the 26.3 bytecode with `javap`. Horizon's `RegionFile::from_file` already zeroes those entries in memory. But `write` and `clear` wrote only the touched slot's offset and timestamp (`write_header_entry`), so the invalid entries stayed in the file for other tools to read. ## Kind - [x] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Change `region.rs`: `write_header` replaces `write_header_entry`. It builds both header sectors from `offsets` and `timestamps` and writes them at position 0, as `writeHeader` does. `write` and `clear` call it. That is 8 KiB per chunk write instead of 16 bytes, the same as vanilla. ## Vanilla reference - `RegionFile.<init>`: the "has invalid sector at index" / "size has to be > 0" / "is out of bounds" warnings, then `offsets.put(i, 0)`. - `RegionFile.write(ChunkPos, ByteBuffer)` and `RegionFile.clear(ChunkPos)` → `writeHeader()`, which writes `header` with `FileChannel.write(header, 0)`. ## Test `horizon-world/tests/world_io.rs::a_chunk_write_rewrites_the_whole_region_header`: - Setup: an 8192-byte region whose header has slot 5 = `00000101` (sector 1, the issue's repro), slot 50 = `00000500` (no sectors) and slot 100 = `00006401` (past the end), plus a timestamp on slot 5. - One chunk write. - After it, the three slots are zero and slot 5's timestamp is kept. - Fails on main ("slot 5: left [0, 0, 1, 1]") and passes here. I wrote the same file with vanilla 26.3's own `RegionFile` (`getChunkDataOutputStream` + `NbtIo.write`, from a small Java program on the 26.3 jar) and with Horizon's `RegionFile::write`: | header bytes after one chunk write | vanilla 26.3 | main | this branch | |---|---|---|---| | slot 0 (written) | `00000201` | `00000201` | `00000201` | | slot 5 (overlaps the header) | `00000000` | `00000101` | `00000000` | | slot 50 (no sectors) | `00000000` | `00000500` | `00000000` | | slot 100 (past the end) | `00000000` | `00006401` | `00000000` | | slot 5 timestamp | `00003039` | `00003039` | `00003039` | Vanilla also logged its three "invalid sector" warnings for that file. All `horizon-world` tests pass, and so do `horizon-client`'s `save::` and `chunk_io` tests (49). Overlap with #5077 (point 6, also mine): both append a test to the end of `tests/world_io.rs`, so whichever merges second has a conflict there. The fix is to keep both tests, and I'll rebase whichever is left. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base c3905131b` on DESKTOP-S9O4938, against c3905131b at c3905131b, 2 min 24 s) | Check | Result | | --- | --- | | Scope | ✅ 2 files, +42 −8 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ❌ 495 run, FAILED (errors=2, skipped=1) (52 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (14 s) | | tests: horizon-world | ✅ 317 passed (1 min 7 s) | | doc tests | ✅ 0 passed (1 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 | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The two Python errors come from files this PR doesn't touch, and both also failed on main on this machine: - `test_port_ledger` cannot import `tests.test_class_index`. - `test_perf_bench`'s `windows_version()` gets nothing from `cmd /C ver` on a Spanish Windows. 🤖 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 06:11:25 +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#6075
No description provided.