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

Bun 1.2 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail

Mandates proactive backpressure mitigation using ws.bufferedAmount, the drain lifecycle handler, and explicit outbound drop/pause policies on Bun WebSockets. Prevents catastrophic event-loop heap exhaustion and process termination caused by unbounded TCP write buffer accumulation on slow consumer connections.

Prohibited Anti-Pattern: Unbounded ws.send() Fire-and-Forget Loops Saturating OS Kernel Buffers and Triggering OOM Crashing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail - Mandates proactive backpressure mitigation using ws.bufferedAmount, the drain lifecycle handler, and explicit outbound drop/pause policies on Bun WebSockets. Prevents catastrophic event-loop heap exhaustion and process termination caused by unbounded TCP write buffer accumulation on slow consumer connections.
globs: src/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-websocket-kernel-backpressure-guardrail-bun-1-2-websocke-844"
---

# Bun 1.2 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded ws.send() Fire-and-Forget Loops Saturating OS Kernel Buffers and Triggering OOM Crashing

## System Constraints
You are a Principal Software Architect enforcing Bun 1.2 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrails. Mandate bounded queue checks, the native Bun drain() handler, and deterministic load-shedding for slow consumers to prevent heap saturation.

## Strict Guidelines
- Engineers MUST ALWAYS evaluate ws.bufferedAmount against a defined high-water-mark (e.g., 512KB - 1MB) prior to invoking ws.send().
- Engineers MUST implement the drain(ws) callback inside Bun.serve({ websocket: { drain(ws) { ... } } }) to resume streaming when socket buffers flush.
- Services MUST NOT execute unthrottled fire-and-forget broadcasting loops over connected client sets without per-client backpressure evaluation.
- Code MUST define deterministic shedding policies for saturated sockets: drop non-critical frames, queue with strict capacity limits, or terminate toxic slow consumers via ws.close(1008, 'Policy Violation: Slow Consumer').
- Stateful publishers MUST maintain connection-specific paused/buffering states inside ws.data instead of holding unbounded memory in external global maps.

## Bad vs Good Implementations
### Bun WebSocket Backpressure Handling with Drain and Bounded Watermarks

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly sending messages to clients without evaluating ws.bufferedAmount or registering the drain lifecycle hook bypasses Bun's native uWebSockets backpressure signals. If a client has network degradation, pending bytes buffer in native heap memory until the runtime encounters an Out-Of-Memory (OOM) crash.
```typescript
import { serve } from "bun";

interface ClientData {
  id: string;
}

const server = serve<ClientData>({
  port: 3000,
  fetch(req, server) {
    if (server.upgrade(req, { data: { id: crypto.randomUUID() } })) return;
    return new Response("Upgrade failed", { status: 400 });
  },
  websocket: {
    message(ws, message) {},
    open(ws) {}
  }
});

// Anti-pattern: Unbounded broadcasting directly into TCP write buffers
export function broadcastMarketTick(data: string) {
  for (const client of server.publish ? [] : []) {
    // Blindly sends without checking kernel buffer size;
    // slow consumers cause Bun's C++ heap allocations to grow uncontrollably
    // client.send(data);
  }
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly verifies ws.bufferedAmount against a safe 512KB limit, leverages the Bun 1.2 drain(ws) callback to reset backpressure state when the OS drains the socket buffer, drops ephemeral frames when backpressured, and evicts persistently unresponsive consumers before heap exhaustion occurs.
```typescript
import { serve, type ServerWebSocket } from "bun";

const BUFFER_HIGH_WATER_MARK = 1024 * 512; // 512 KB

interface ClientData {
  id: string;
  isBackpressured: boolean;
  droppedFrames: number;
}

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

export const server = serve<ClientData>({
  port: 3000,
  fetch(req, server) {
    const id = crypto.randomUUID();
    if (server.upgrade(req, { data: { id, isBackpressured: false, droppedFrames: 0 } })) {
      return;
    }
    return new Response("Upgrade failed", { status: 400 });
  },
  websocket: {
    open(ws) {
      activeClients.add(ws);
    },
    close(ws) {
      activeClients.delete(ws);
    },
    message(ws, message) {
      // Client-to-server messaging
    },
    drain(ws) {
      // Bun calls drain when socket buffer drops below internal low-water mark
      if (ws.bufferedAmount < BUFFER_HIGH_WATER_MARK) {
        ws.data.isBackpressured = false;
      }
    }
  }
});

export function broadcastTick(payload: string): void {
  for (const ws of activeClients) {
    if (ws.bufferedAmount >= BUFFER_HIGH_WATER_MARK) {
      ws.data.isBackpressured = true;
      ws.data.droppedFrames++;

      // Terminate toxic slow consumers exceeding drop tolerance
      if (ws.data.droppedFrames > 100) {
        ws.close(1008, "Slow consumer limit exceeded");
        activeClients.delete(ws);
      }
      continue; // Drop ephemeral real-time tick to protect host memory
    }

    const sentBytes = ws.send(payload);
    if (sentBytes === 0 || ws.bufferedAmount >= BUFFER_HIGH_WATER_MARK) {
      ws.data.isBackpressured = true;
    }
  }
}
```

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 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail

Bun 1.2 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail defines an industrial-grade behavioral standard for backend engineering. Mandates proactive backpressure mitigation using ws.bufferedAmount, the drain lifecycle handler, and explicit outbound drop/pause policies on Bun WebSockets. Prevents catastrophic event-loop heap exhaustion and process termination caused by unbounded TCP write buffer accumulation on slow consumer connections. 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 ws.send() Fire-and-Forget Loops Saturating OS Kernel Buffers and Triggering OOM Crashing 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 ws.send() Fire-and-Forget Loops Saturating OS Kernel Buffers and Triggering OOM CrashingCritical
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 evaluate ws.bufferedAmount against a defined high-water-mark (e.g., 512KB - 1MB) prior to invoking ws.send().

Engineers MUST ALWAYS evaluate ws.bufferedAmount against a defined high-water-mark (e.g., 512KB - 1MB) prior to invoking ws.send().

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 drain(ws) callback inside Bun.serve({ websocket

Engineers MUST implement the drain(ws) callback inside Bun.serve({ websocket: { drain(ws) { ... } } }) to resume streaming when socket buffers flush.

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 NOT execute unthrottled fire-and-forget broadcasting loops over connected client sets without per-client backpressure evaluation.

Services MUST NOT execute unthrottled fire-and-forget broadcasting loops over connected client sets without per-client backpressure evaluation.

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

Code MUST define deterministic shedding policies for saturated sockets

Code MUST define deterministic shedding policies for saturated sockets: drop non-critical frames, queue with strict capacity limits, or terminate toxic slow consumers via ws.close(1008, 'Policy Violation: Slow Consumer').

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

Stateful publishers MUST maintain connection-specific paused/buffering states inside ws.data instead of holding unbounded memory in external global maps.

Stateful publishers MUST maintain connection-specific paused/buffering states inside ws.data instead of holding unbounded memory in external global maps.

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 WebSocket Kernel-Buffer Auto-Pipelining Backpressure Guardrail critical for AI coding assistants?

Mandates proactive backpressure mitigation using ws.bufferedAmount, the drain lifecycle handler, and explicit outbound drop/pause policies on Bun WebSockets. Prevents catastrophic event-loop heap exhaustion and process termination caused by unbounded TCP write buffer accumulation on slow consumer connections. 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 ws.send() Fire-and-Forget Loops Saturating OS Kernel Buffers and Triggering OOM Crashing) 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