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

Bun 1.2 Native WebSocket Backpressure & Recycled Direct Memory Architecture

Under high-throughput outbound streaming, unbounded invocations of ws.send() trigger socket buffer bloat, unmanaged kernel queue expansion, and severe memory exhaustion. Bun services must govern stream velocity using ws.send() backpressure feedback, aggregate writes with ws.cork(), resume throttled producers exclusively via the native drain(ws) callback, and reuse pre-allocated slab buffers for outbound payloads.

Prohibited Anti-Pattern: Unbounded Fire-and-Forget ws.send() Loops Inducing Kernel Buffer Bloat and Out-Of-Memory Crashes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native WebSocket Backpressure & Recycled Direct Memory Architecture - Under high-throughput outbound streaming, unbounded invocations of ws.send() trigger socket buffer bloat, unmanaged kernel queue expansion, and severe memory exhaustion. Bun services must govern stream velocity using ws.send() backpressure feedback, aggregate writes with ws.cork(), resume throttled producers exclusively via the native drain(ws) callback, and reuse pre-allocated slab buffers for outbound payloads.
globs: src/**/*.{ts,js}, server/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-1-2-websocket-kernel-backpressure-guardrail"
---

# Bun 1.2 Native WebSocket Backpressure & Recycled Direct Memory Architecture

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Fire-and-Forget ws.send() Loops Inducing Kernel Buffer Bloat and Out-Of-Memory Crashes

## System Constraints
You are a Principal Software Architect enforcing production-grade WebSocket throughput and memory safety in Bun 1.2. Mandate native backpressure flow control, zero per-message heap allocation in hot streaming paths, corked syscall aggregation, and deterministic client throttling using Bun's native uSockets lifecycle hooks.

## Strict Guidelines
- Engineers MUST ALWAYS check the return status of ws.send(data); when send() reports backpressure (returns -1, 0, or false indicating buffer exhaustion), the producer MUST immediately pause streaming.
- Engineers MUST implement the native drain(ws: ServerWebSocket<T>) callback on the Bun server configuration to resume paused data producers only when the kernel buffer clears.
- Engineers MUST aggregate multiple atomic messages inside ws.cork((ws) => { ... }) to bundle outbound frames into unified kernel TCP packets and minimize syscall overhead.
- Engineers MUST NOT allocate new Buffer or Uint8Array instances per message in hot outbound loops; services MUST write into recycled slab buffers or pre-allocated ArrayBuffer views.
- Engineers MUST enforce explicit per-socket byte queue high-water marks and proactively terminate or drop non-essential frames for perpetually stalled consumers.
- Engineers MUST NOT rely on high-level Node.js stream adapters (e.g., Readable.from) over Bun WebSockets without wrapping write calls in backpressure checks.

## Bad vs Good Implementations
### Naive Unbounded Broadcast vs. Backpressure-Aware Native Streaming with Bun 1.2

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The handler launches an unchecked async loop that fires one million allocations into `ws.send()` without inspecting kernel buffer saturation. The socket queue balloons in native memory, triggering buffer bloat, latency spikes, and eventual OOM termination under load.
```typescript
import { serve } from "bun";

interface ClientState {
  streamId: string;
}

serve<ClientState>({
  port: 8080,
  websocket: {
    open(ws) {
      console.log("Client connected");
    },
    message(ws, message) {
      // CRITICAL BUG: Unchecked streaming loop. Ignores kernel socket buffer saturation,
      // allocates a new Buffer on every tick, and causes unbounded heap growth leading to crash.
      const streamData = async () => {
        for (let i = 0; i < 1_000_000; i++) {
          const payload = Buffer.from(JSON.stringify({ index: i, timestamp: performance.now() }));
          ws.send(payload);
        }
      };
      streamData();
    },
  },
  fetch(req, server) {
    if (server.upgrade(req, { data: { streamId: crypto.randomUUID() } })) return;
    return new Response("Upgrade failed", { status: 400 });
  },
});
```

#### Production Standard (Correct)
> **Standard**: The stream checks the return value of `ws.send()`, suspends pumping immediately when TCP backpressure occurs (`sendResult <= 0`), aggregates frames into single kernel syscalls using `ws.cork()`, reuses a static slab `Uint8Array`, and guarantees resumed pumping only through the engine-driven `drain()` hook.
```typescript
import { serve, type ServerWebSocket } from "bun";

interface ClientData {
  streamId: string;
  isPaused: boolean;
  currentTick: number;
  targetTick: number;
}

const SLAB_BUFFER = new Uint8Array(64);
const encoder = new TextEncoder();

function writeTickToSlab(tick: number, timestamp: number): Uint8Array {
  const payload = `{"tick":${tick},"ts":${timestamp.toFixed(2)}}`;
  const { written } = encoder.encodeInto(payload, SLAB_BUFFER);
  return SLAB_BUFFER.subarray(0, written);
}

function pumpStream(ws: ServerWebSocket<ClientData>): void {
  while (ws.data.currentTick < ws.data.targetTick) {
    const view = writeTickToSlab(ws.data.currentTick, performance.now());
    
    // ws.send returns the number of bytes sent or negative/zero code when backpressured
    const sendResult = ws.send(view, true);
    
    if (sendResult <= 0) {
      // Socket buffer is saturated; pause generation until native drain callback fires
      ws.data.isPaused = true;
      return;
    }
    
    ws.data.currentTick++;
  }
}

serve<ClientData>({
  port: 8080,
  websocket: {
    open(ws) {
      ws.data = {
        streamId: crypto.randomUUID(),
        isPaused: false,
        currentTick: 0,
        targetTick: 1_000_000,
      };
    },
    message(ws) {
      if (!ws.data.isPaused) {
        ws.cork(() => pumpStream(ws));
      }
    },
    drain(ws) {
      // Native kernel socket buffer drained; safely resume data pump
      if (ws.data.isPaused) {
        ws.data.isPaused = false;
        ws.cork(() => pumpStream(ws));
      }
    },
    close(ws) {
      ws.data.isPaused = true;
    }
  },
  fetch(req, server) {
    if (server.upgrade(req, { data: {} as ClientData })) return;
    return new Response("Upgrade failed", { status: 400 });
  },
});
```

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 WebSocket Backpressure & Recycled Direct Memory Architecture

Bun 1.2 Native WebSocket Backpressure & Recycled Direct Memory Architecture defines an industrial-grade behavioral standard for backend engineering. Under high-throughput outbound streaming, unbounded invocations of ws.send() trigger socket buffer bloat, unmanaged kernel queue expansion, and severe memory exhaustion. Bun services must govern stream velocity using ws.send() backpressure feedback, aggregate writes with ws.cork(), resume throttled producers exclusively via the native drain(ws) callback, and reuse pre-allocated slab buffers for outbound payloads. 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: Unbounded Fire-and-Forget ws.send() Loops Inducing Kernel Buffer Bloat and Out-Of-Memory Crashes 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 DriftUnbounded Fire-and-Forget ws.send() Loops Inducing Kernel Buffer Bloat and Out-Of-Memory CrashesCritical
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 check the return status of ws.send(data)

Engineers MUST ALWAYS check the return status of ws.send(data); when send() reports backpressure (returns -1, 0, or false indicating buffer exhaustion), the producer MUST immediately pause streaming.

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 implement the native drain(ws

Engineers MUST implement the native drain(ws: ServerWebSocket<T>) callback on the Bun server configuration to resume paused data producers only when the kernel buffer clears.

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

Engineers MUST aggregate multiple atomic messages inside ws.cork((ws) => { ... }) to bundle outbound frames into unified kernel TCP packets and minimize syscall overhead.

Engineers MUST aggregate multiple atomic messages inside ws.cork((ws) => { ... }) to bundle outbound frames into unified kernel TCP packets and minimize syscall overhead.

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 NOT allocate new Buffer or Uint8Array instances per message in hot outbound loops

Engineers MUST NOT allocate new Buffer or Uint8Array instances per message in hot outbound loops; services MUST write into recycled slab buffers or pre-allocated ArrayBuffer views.

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

Engineers MUST enforce explicit per-socket byte queue high-water marks and proactively terminate or drop non-essential frames for perpetually stalled consumers.

Engineers MUST enforce explicit per-socket byte queue high-water marks and proactively terminate or drop non-essential frames for perpetually stalled consumers.

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

Engineers MUST NOT rely on high-level Node.js stream adapters (e.g., Readable.from) over Bun WebSockets without wrapping write calls in backpressure checks.

Engineers MUST NOT rely on high-level Node.js stream adapters (e.g., Readable.from) over Bun WebSockets without wrapping write calls in backpressure checks.

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 WebSocket Backpressure & Recycled Direct Memory Architecture critical for AI coding assistants?

Under high-throughput outbound streaming, unbounded invocations of ws.send() trigger socket buffer bloat, unmanaged kernel queue expansion, and severe memory exhaustion. Bun services must govern stream velocity using ws.send() backpressure feedback, aggregate writes with ws.cork(), resume throttled producers exclusively via the native drain(ws) callback, and reuse pre-allocated slab buffers for outbound payloads. 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 (Unbounded Fire-and-Forget ws.send() Loops Inducing Kernel Buffer Bloat and Out-Of-Memory Crashes) 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