Repository navigation
Description
Part of #649 (ruling 1). Lands in the same change as #650.
Problem
Up to 1.4.3, Bun's built-in ws refused any inbound message over 16 MiB on the server side, whatever maxPayload said. Bun 1.4.4 enforces maxPayload, with a 100 MiB default. That comes from the 1.4.4 release notes; we measured 1.4.1 and 1.4.4.
Hocuspocus 3.4.4 builds its socket server from that package and passes our websocketOptions through. We pass none, so on Bun 1.4.4 the limit is the 100 MiB default.
The 64 KiB stateless and awareness budgets in apps/hocuspocus.server/CLAUDE.md §Runtime limits run after a message arrives. They do not bound its size. That section also says maxPayload "is not the answer" because Bun ignores it. That is false on 1.4.4.
Evidence
Measured 2026-10-31 on macOS arm64, with the real @hocuspocus/server 3.4.4 Server and HocuspocusProvider. Only the Bun binary changed. With maxPayload: 16 * 1024 * 1024:
| Message | Bun 1.4.1 | Bun 1.4.4 |
|---|---|---|
| 15 MiB | accepted | accepted |
| 17 MiB | closed 1006 |
closed 1009 |
| 24 MiB as 3 × 8 MiB fragments | not run | closed 1009 |
| a new connection afterwards | not run | syncs |
- A run with the server and the client on different binaries showed the 1.4.1 limit is on the server side, at exactly 16 MiB.
- The cap counts the whole message, so fragments do not get past it.
- No other backend listener needs the cap. The three
Bun.servelisteners (src/index.ts:227,src/hocuspocus.server.ts:661,src/hocuspocus.worker.ts:248) have nowebsockethandler.
Fix
-
In
apps/hocuspocus.server/src/hocuspocus.server.ts:604, pass the cap as the constructor's second argument:const server = new Server(serverConfig, { maxPayload: MAX_WS_MESSAGE_BYTES })
The constructor is
constructor(configuration, websocketOptions = {}). It spreadswebsocketOptionsintonew WebSocketServer({ noServer: true, ...websocketOptions }). It is not a key insideserverConfig. -
Declare
const MAX_WS_MESSAGE_BYTES = 16 * 1024 * 1024next toMAX_AWARENESS_FRAME_BYTES(src/hocuspocus.server.ts:62). -
Add
maxPayload: server.webSocketServer.options.maxPayloadandbun: Bun.versionto the'🚀 WebSocket Server started successfully'log line (src/hocuspocus.server.ts:617). Each replica then states its cap at start. -
Replace the
maxPayloadbullet inapps/hocuspocus.server/CLAUDE.md§Runtime limits with:MAX_WS_MESSAGE_BYTES(16 MiB) caps each inbound WebSocket message. It is theServerconstructor's second argument, not a config key.- Bun 1.4.4 enforces it and closes with
1009. Bun 1.4.1 refused anything over 16 MiB with1006, whatever the option said. - It bounds one message. It is not a memory bound, and the 64 KiB budgets still stop the relay amplifier.
- Keep it above the largest client sync message, which is about one document's size.
- Before a Hocuspocus bump, confirm
Serverstill attachesincoming.on('error')to each socket. Without it, a refused message reaches theuncaughtExceptionhandler, which shuts the process down.
Each refusal makes two console.error calls from Hocuspocus, about 12 stderr lines with Bun's code frame. That is outside pino, and no metric counts it. That is acceptable for this fix.
Out of scope: this restores the old bound on one message. It does not bound document size.
Verification
- Add three checks to section
[8]ofscripts/e2e-versions.ts, whichbackend-ci.yml:135runs:- Control: a stateless
{type:'docTitle', pad:'x'.repeat(15 * 1024 * 1024)}movesstateless_relay_dropped_total{reason="oversized"}by 1. That proves the server received and parsed it. - Send one 17 MiB binary message on a raw
WebSocketwith no provider. Assert close code1009within 10 s. Use a new socket, not one a later check in section[8]needs. - Then a new provider on the same room still syncs.
- Control: a stateless
- Run the new checks once on 1.4.4 without the cap. The
1009assertion must fail. Treat no close within 10 s as a failure. - The checks need Bun 1.4.4. On 1.4.1 the 17 MiB message closes with
1006, so the1009assertion fails there even with the cap. - After the deploy, read the start log line in each replica with
docker logs. It must show the 16 MiB cap.
Decision
- Which cap value? Recommended: 16 MiB. The old limit was server-side and exactly 16 MiB, so no client loses anything it had. It also sits above the largest stored head, 5,357,733 B on 2026-09-05, which a client can send back in one sync message. Never set it near 64 KiB.
Activity
[-]Cap collaboration WebSocket frames at 16 MiB again on Bun 1.4.4[/-][+]Cap collaboration WebSocket messages at 16 MiB again on Bun 1.4.4[/+]HMarzban commentedon Nov 3, 2026
Status, 2026-11-03. Nothing for this issue is pushed.
Serveris still built with nomaxPayload, so production on Bun 1.4.4 uses the 100 MiB default. What is left:CLAUDE.mdtext;[8]checks, proved by sabotage on Bun 1.4.4;docs: record the WebSocket cap and the CI Bun check