[GH-ISSUE #7440] Game setup returns an empty reply: async JSON compression races handlers that send without return #4147

Closed
opened 2026-10-21 06:11:42 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @SpicyMarinara on GitHub (Oct 11, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7440

Problem

On current staging, creating a Game fails for every connection and setup. The server logs FST_ERR_REP_ALREADY_SENT ("Reply was already sent, did you forget to return reply") for POST /api/game/setup, and the browser gets an empty 200 that it can't parse. This was reported on Discord (Windows 11, staging 63006ab28).

Cause

compressJsonHook (packages/server/src/middleware/compress-json.ts, added in 445512d4b and merged with #7312 on 2026-10-10) is an async onSend hook. It awaits Brotli or gzip on the threadpool, which keeps the onSend chain open for a full macrotask.

Several async handlers call reply.send() without return reply, for example executeSetupGame's final rejectGenerationOutput(reply, 200, setupResult) and /game/setup/apply-json. Their promise resolves while compression is still running. Fastify then sees reply.sent === false and sends again with an undefined body. That empty send goes out first, and the compressed one then fails with ERR_HTTP_HEADERS_SENT.

It happens whenever the reply is at least 1 KB and the client offers br or gzip, which every browser does. The regression suite missed it because app.inject() replies are never compressed.

Reproduced over real HTTP against staging: a 601 KB setup reply returned an empty 200 with a browser Accept-Encoding, and the full JSON without one.

Fix

Make the hook synchronous (brotliCompressSync / gzipSync, callback-style). That restores the timing from before 445512d4b for every route, not just Game setup. It costs about 1–6 ms of event-loop time per MB of JSON. Add a regression test for an async handler that sends without returning.

Owner: @SpicyMarinara (Claude Code agent working on it)

Originally created by @SpicyMarinara on GitHub (Oct 11, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7440 ## Problem On current staging, creating a Game fails for every connection and setup. The server logs `FST_ERR_REP_ALREADY_SENT` ("Reply was already sent, did you forget to return reply") for `POST /api/game/setup`, and the browser gets an empty 200 that it can't parse. This was reported on Discord (Windows 11, staging 63006ab28). ## Cause `compressJsonHook` (`packages/server/src/middleware/compress-json.ts`, added in 445512d4b and merged with #7312 on 2026-10-10) is an async `onSend` hook. It awaits Brotli or gzip on the threadpool, which keeps the `onSend` chain open for a full macrotask. Several async handlers call `reply.send()` without `return reply`, for example `executeSetupGame`'s final `rejectGenerationOutput(reply, 200, setupResult)` and `/game/setup/apply-json`. Their promise resolves while compression is still running. Fastify then sees `reply.sent === false` and sends again with an `undefined` body. That empty send goes out first, and the compressed one then fails with `ERR_HTTP_HEADERS_SENT`. It happens whenever the reply is at least 1 KB and the client offers `br` or `gzip`, which every browser does. The regression suite missed it because `app.inject()` replies are never compressed. Reproduced over real HTTP against staging: a 601 KB setup reply returned an empty 200 with a browser `Accept-Encoding`, and the full JSON without one. ## Fix Make the hook synchronous (`brotliCompressSync` / `gzipSync`, callback-style). That restores the timing from before 445512d4b for every route, not just Game setup. It costs about 1–6 ms of event-loop time per MB of JSON. Add a regression test for an async handler that sends without returning. Owner: @SpicyMarinara (Claude Code agent working on it)
bsakaguchi 2026-10-21 06:11:42 +07:00
  • closed this issue
  • added the
    in-pr
    bug
    labels
Author
Owner

@SpicyMarinara commented on GitHub (Oct 11, 2026):

Fixed on staging by #7441. Game setup and other large replies arrive complete again.

<!-- gh-comment-id:6111353902 --> @SpicyMarinara commented on GitHub (Oct 11, 2026): Fixed on staging by #7441. Game setup and other large replies arrive complete again.
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#4147
No description provided.