[PR #7441] [MERGED] fix(server): compress API JSON synchronously so early sends keep their body #7734

Closed
opened 2026-10-24 04:06:59 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7441
Author: @SpicyMarinara
Created: 10/11/2026
Status: ✅ Merged
Merged: 10/11/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/compress-json-sync


📝 Commits (1)

  • 40e1e85 fix(server): compress API JSON synchronously so early sends keep their body

📊 Changes

3 files changed (+26 additions, -8 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/middleware/compress-json.ts (+16 -8)
📝 scripts/regressions/compress-json.regression.ts (+9 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7440

Why this change

  • A Discord report (staging 63006ab28, Windows 11) says Game mode can't create a new game with any connection or setup. The log shows FST_ERR_REP_ALREADY_SENT on POST /api/game/setup.
  • Reproduced over real HTTP against staging with a local mock provider. A 601 KB setup reply came back as an empty 200 whenever the request offered br or gzip, which every browser does. Without Accept-Encoding it came back as the full JSON. The server logged the reporter's exact three lines.
  • Cause: compressJsonHook (445512d4b, merged with #7312) awaits Brotli/gzip. An async handler that sends without return reply resolves during that wait, so Fastify sends a second, empty reply that wins the race. This affects Game setup, /game/setup/apply-json and other handlers with the same shape. Tests missed it because app.inject() replies are never compressed.
  • The reporter's proposed patch (adding return to three sends) doesn't apply: those sends are already followed by return;.

What changed

  • compressJsonHook is synchronous and callback-style (brotliCompressSync / gzipSync), with the same quality settings. That restores the timing from before 445512d4b for every route, not just Game setup. The cost is about 1–6 ms of event-loop time per MB of JSON, similar to the JSON.stringify already done for the same body.
  • Regression: compress-json now covers an async handler that sends without returning. It fails on staging's hook ("a handler that sends without returning reply gets its body") and passes with the fix.
  • Not changed: adding return reply to the handlers. With the hook synchronous it's no longer needed, and the hook fix covers every call site.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Commands run by the agent: pnpm check, plus the compress-json, capability-prompt-preview and prompt regressions (all pass). The real-HTTP reproduction (mock provider, browser Accept-Encoding, streaming and non-streaming, slow replies, <think> blocks) passed with this fix applied.

Manual verification notes

  • Manually verify that creating a new Game in the browser on staging finishes setup and opens the game.
  • Manually verify that a long chat still loads, and that its response is compressed (DevTools Network shows content-encoding: br).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

CHANGELOG [Unreleased] has an entry. No user docs change.


🔄 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/Pasta-Devs/Marinara-Engine/pull/7441 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/11/2026 **Status:** ✅ Merged **Merged:** 10/11/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/compress-json-sync` --- ### 📝 Commits (1) - [`40e1e85`](https://github.com/Pasta-Devs/Marinara-Engine/commit/40e1e8572d455cc20d39c52b1e1ef436e5f4240b) fix(server): compress API JSON synchronously so early sends keep their body ### 📊 Changes **3 files changed** (+26 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/middleware/compress-json.ts` (+16 -8) 📝 `scripts/regressions/compress-json.regression.ts` (+9 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7440 ## Why this change - A Discord report (staging 63006ab28, Windows 11) says Game mode can't create a new game with any connection or setup. The log shows `FST_ERR_REP_ALREADY_SENT` on `POST /api/game/setup`. - Reproduced over real HTTP against staging with a local mock provider. A 601 KB setup reply came back as an empty 200 whenever the request offered `br` or `gzip`, which every browser does. Without `Accept-Encoding` it came back as the full JSON. The server logged the reporter's exact three lines. - Cause: `compressJsonHook` (445512d4b, merged with #7312) awaits Brotli/gzip. An async handler that sends without `return reply` resolves during that wait, so Fastify sends a second, empty reply that wins the race. This affects Game setup, `/game/setup/apply-json` and other handlers with the same shape. Tests missed it because `app.inject()` replies are never compressed. - The reporter's proposed patch (adding `return` to three sends) doesn't apply: those sends are already followed by `return;`. ## What changed - `compressJsonHook` is synchronous and callback-style (`brotliCompressSync` / `gzipSync`), with the same quality settings. That restores the timing from before 445512d4b for every route, not just Game setup. The cost is about 1–6 ms of event-loop time per MB of JSON, similar to the `JSON.stringify` already done for the same body. - Regression: `compress-json` now covers an async handler that sends without returning. It fails on staging's hook ("a handler that sends without returning reply gets its body") and passes with the fix. - Not changed: adding `return reply` to the handlers. With the hook synchronous it's no longer needed, and the hook fix covers every call site. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Commands run by the agent: `pnpm check`, plus the `compress-json`, `capability-prompt-preview` and `prompt` regressions (all pass). The real-HTTP reproduction (mock provider, browser `Accept-Encoding`, streaming and non-streaming, slow replies, `<think>` blocks) passed with this fix applied. ### Manual verification notes - Manually verify that creating a new Game in the browser on staging finishes setup and opens the game. - Manually verify that a long chat still loads, and that its response is compressed (DevTools Network shows `content-encoding: br`). ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) CHANGELOG `[Unreleased]` has an entry. No user docs change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:06:59 +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
Pasta-Devs/Marinara-Engine#7734
No description provided.