This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-native-websocket-backpressure-drain-invariant

Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant

Enforces strict backpressure synchronization and bounded buffer thresholds on native Bun 1.2 WebSocket broadcast loops. Prevents catastrophic off-heap C++ buffer allocation and process-level RSS memory exhaustion caused by slow socket consumers during high-volume binary streaming.

Prohibited Anti-Pattern: Unchecked Fire-and-Forget ws.send() Calls Triggering Unbounded Off-Heap C++ Queue Bloat

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant - Enforces strict backpressure synchronization and bounded buffer thresholds on native Bun 1.2 WebSocket broadcast loops. Prevents catastrophic off-heap C++ buffer allocation and process-level RSS memory exhaustion caused by slow socket consumers during high-volume binary streaming.
globs: src/**/*.{ts,tsx}, server/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-native-websocket-backpressure-drain-invariant"
---

# Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unchecked Fire-and-Forget ws.send() Calls Triggering Unbounded Off-Heap C++ Queue Bloat

## System Constraints
You are a Principal Systems Engineer enforcing Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain Invariants. Under high-throughput broadcast flows, Bun queues native socket frames in an off-heap C++ ring buffer that is invisible to JavaScript garbage collection. You must forbid unmonitored ws.send() calls and enforce strict backpressure handling via getBufferedAmount(), the native drain(ws) callback, and bounded client disconnect thresholds.

## Strict Guidelines
- Engineers MUST ALWAYS implement the native `drain(ws)` socket handler on `Bun.serve` when streaming binary buffers across WebSockets.
- Engineers MUST NOT execute fire-and-forget `ws.send(buffer)` operations across socket sets without validating `ws.getBufferedAmount()` against a fixed high-water mark.
- Services MUST immediately terminate or shed slow consumers (`ws.close(1008, 'Buffer Overflow')`) when `ws.getBufferedAmount()` crosses the catastrophic threshold (e.g., 2MB).
- Engineers MUST NEVER mutate, re-slice, or detach an underlying `ArrayBuffer` passed to `ws.send()` while zero-copy transfers are queued in the native layer.
- Code SHOULD leverage immutable `Uint8Array` references for zero-copy binary dispatch and pause publisher streams when socket queues saturate.
- Publishers MUST defer re-enqueuing data to a backpressured socket until Bun's native `drain` hook verifies that the kernel transmit window has cleared.

## Bad vs Good Implementations
### Unbounded WebSocket Broadcast vs. Native Drain Backpressure Synchronization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Calling ws.send() blindly ignores slow consumers. Bun queues unsent frames in off-heap C++ buffers outside JSC garbage collection. In multi-gigabit workloads, these unmonitored queues consume gigabytes of RSS memory within seconds, triggering the OS OOM killer.
```typescript
import { serve, ServerWebSocket } from 'bun';

interface ClientData {
  id: string;
}

const clients = new Set<ServerWebSocket<ClientData>>();

// ANTI-PATTERN: Blindly sending multi-megabyte ArrayBuffers to all connected clients.
// If a single client throttles TCP ACK packets, Bun's internal uSockets layer
// queues data off-heap, causing silent Resident Set Size (RSS) explosion and OOM panic.
export function broadcastFrame(frame: Uint8Array) {
  for (const ws of clients) {
    ws.send(frame);
  }
}

serve<ClientData>({
  port: 8080,
  websocket: {
    open(ws) {
      clients.add(ws);
    },
    close(ws) {
      clients.delete(ws);
    },
    message(ws, message) {
      // No drain handler, no backpressure validation
    },
  },
  fetch(req, server) {
    server.upgrade(req);
    return new Response('Upgraded', { status: 101 });
  },
});
```

#### Production Standard (Correct)
> **Standard**: Implements the invariant: tracks ws.getBufferedAmount(), enforces an upper threshold (512KB) before deferring packets to an application queue, terminates rogue slow consumers exceeding 2MB, and utilizes Bun's native drain(ws) callback to flush queued ArrayBuffers only when the kernel socket buffer has emptied.
```typescript
import { serve, ServerWebSocket } from 'bun';

interface SocketState {
  id: string;
  isBackpressured: boolean;
  pendingQueue: Uint8Array[];
}

const HIGH_WATER_MARK = 1024 * 512; // 512 KB max off-heap queue
const HARD_DROP_LIMIT = 1024 * 1024 * 2; // 2 MB drop threshold
const clients = new Set<ServerWebSocket<SocketState>>();

export function broadcastFrame(frame: Uint8Array) {
  // Pass immutable zero-copy view
  const immutableFrame = new Uint8Array(frame.buffer, frame.byteOffset, frame.byteLength);

  for (const ws of clients) {
    const buffered = ws.getBufferedAmount();

    if (buffered >= HARD_DROP_LIMIT) {
      // Client is fatally lagging; terminate immediately to preserve server RSS stability
      ws.close(1008, 'Backpressure threshold exceeded');
      clients.delete(ws);
      continue;
    }

    if (buffered >= HIGH_WATER_MARK || ws.data.isBackpressured) {
      // Apply backpressure: queue application frame or shed non-critical payload
      ws.data.isBackpressured = true;
      ws.data.pendingQueue.push(immutableFrame);
      continue;
    }

    // Send returns 0 or byte count transferred; verify backpressure after dispatch
    ws.send(immutableFrame);
    if (ws.getBufferedAmount() > HIGH_WATER_MARK) {
      ws.data.isBackpressured = true;
    }
  }
}

serve<SocketState>({
  port: 8080,
  websocket: {
    open(ws) {
      ws.data = { id: crypto.randomUUID(), isBackpressured: false, pendingQueue: [] };
      clients.add(ws);
    },
    close(ws) {
      clients.delete(ws);
    },
    drain(ws) {
      // INVARIANT: Native drain handler signals kernel buffer availability
      const state = ws.data;
      while (state.pendingQueue.length > 0) {
        if (ws.getBufferedAmount() >= HIGH_WATER_MARK) {
          return; // Remain in backpressured state
        }
        const nextFrame = state.pendingQueue.shift();
        if (nextFrame) {
          ws.send(nextFrame);
        }
      }
      state.isBackpressured = false;
    },
    message(ws, message) {
      // Handle client messages
    },
  },
  fetch(req, server) {
    server.upgrade(req, {
      data: { id: '', isBackpressured: false, pendingQueue: [] },
    });
    return new Response('Upgraded', { status: 101 });
  },
});
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant

Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces strict backpressure synchronization and bounded buffer thresholds on native Bun 1.2 WebSocket broadcast loops. Prevents catastrophic off-heap C++ buffer allocation and process-level RSS memory exhaustion caused by slow socket consumers during high-volume binary streaming. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unchecked Fire-and-Forget ws.send() Calls Triggering Unbounded Off-Heap C++ Queue Bloat Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnchecked Fire-and-Forget ws.send() Calls Triggering Unbounded Off-Heap C++ Queue BloatCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS implement the native `drain(ws)` socket handler on `Bun.serve` when streaming binary buffers across WebSockets.

Engineers MUST ALWAYS implement the native `drain(ws)` socket handler on `Bun.serve` when streaming binary buffers across WebSockets.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT execute fire-and-forget `ws.send(buffer)` operations across socket sets without validating `ws.getBufferedAmount()` against a fixed high-water mark.

Engineers MUST NOT execute fire-and-forget `ws.send(buffer)` operations across socket sets without validating `ws.getBufferedAmount()` against a fixed high-water mark.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Services MUST immediately terminate or shed slow consumers (`ws.close(1008, 'Buffer Overflow')`) when `ws.getBufferedAmount()` crosses the catastrophic threshold (e.g., 2MB).

Services MUST immediately terminate or shed slow consumers (`ws.close(1008, 'Buffer Overflow')`) when `ws.getBufferedAmount()` crosses the catastrophic threshold (e.g., 2MB).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST NEVER mutate, re-slice, or detach an underlying `ArrayBuffer` passed to `ws.send()` while zero-copy transfers are queued in the native layer.

Engineers MUST NEVER mutate, re-slice, or detach an underlying `ArrayBuffer` passed to `ws.send()` while zero-copy transfers are queued in the native layer.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Code SHOULD leverage immutable `Uint8Array` references for zero-copy binary dispatch and pause publisher streams when socket queues saturate.

Code SHOULD leverage immutable `Uint8Array` references for zero-copy binary dispatch and pause publisher streams when socket queues saturate.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Publishers MUST defer re-enqueuing data to a backpressured socket until Bun's native `drain` hook verifies that the kernel transmit window has cleared.

Publishers MUST defer re-enqueuing data to a backpressured socket until Bun's native `drain` hook verifies that the kernel transmit window has cleared.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Bun 1.2 Native Socket Zero-Copy ArrayBuffer Drain & Backpressure Invariant critical for AI coding assistants?

Enforces strict backpressure synchronization and bounded buffer thresholds on native Bun 1.2 WebSocket broadcast loops. Prevents catastrophic off-heap C++ buffer allocation and process-level RSS memory exhaustion caused by slow socket consumers during high-volume binary streaming. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unchecked Fire-and-Forget ws.send() Calls Triggering Unbounded Off-Heap C++ Queue Bloat) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Bun