Skip to content

Cap collaboration WebSocket messages at 16 MiB again on Bun 1.4.4 #651

Closed

Description

@HMarzban

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.serve listeners (src/index.ts:227, src/hocuspocus.server.ts:661, src/hocuspocus.worker.ts:248) have no websocket handler.

Fix

  1. 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 spreads websocketOptions into new WebSocketServer({ noServer: true, ...websocketOptions }). It is not a key inside serverConfig.

  2. Declare const MAX_WS_MESSAGE_BYTES = 16 * 1024 * 1024 next to MAX_AWARENESS_FRAME_BYTES (src/hocuspocus.server.ts:62).

  3. Add maxPayload: server.webSocketServer.options.maxPayload and bun: Bun.version to the '🚀 WebSocket Server started successfully' log line (src/hocuspocus.server.ts:617). Each replica then states its cap at start.

  4. Replace the maxPayload bullet in apps/hocuspocus.server/CLAUDE.md §Runtime limits with:

    • MAX_WS_MESSAGE_BYTES (16 MiB) caps each inbound WebSocket message. It is the Server constructor'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 with 1006, 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 Server still attaches incoming.on('error') to each socket. Without it, a refused message reaches the uncaughtException handler, 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] of scripts/e2e-versions.ts, which backend-ci.yml:135 runs:
    • Control: a stateless {type:'docTitle', pad:'x'.repeat(15 * 1024 * 1024)} moves stateless_relay_dropped_total{reason="oversized"} by 1. That proves the server received and parsed it.
    • Send one 17 MiB binary message on a raw WebSocket with no provider. Assert close code 1009 within 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.
  • Run the new checks once on 1.4.4 without the cap. The 1009 assertion 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 the 1009 assertion 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

  1. 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

  1. added this to the v2.4.0 milestone on Oct 31, 2026
  2. added
    Security
    on Oct 31, 2026
  3. changed the title [-]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[/+] on Oct 31, 2026
  4. modified the milestones: v2.4.0, v2.3.0 on Oct 31, 2026
  5. HMarzban commented on Nov 3, 2026

    @HMarzban
    CollaboratorAuthor

    Status, 2026-11-03. Nothing for this issue is pushed. Server is still built with no maxPayload, so production on Bun 1.4.4 uses the 100 MiB default. What is left:

  6. modified the milestones: v2.3.0, v2.3.6 on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DevOpsSecurity

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions