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

Bun 1.2 Native Connection Pool Backpressure Isolation & Thread Starvation Guardrail

Bun 1.2 leverages native Zig/C++ async I/O primitives and uSockets underneath the JavaScript runtime. Unbounded asynchronous native operations—such as unthrottled fetch dispatches, unmetered native socket allocations, and limitless database pool acquisitions—exhaust native file descriptors and starve worker threads during traffic bursts. This standard mandates deterministic semaphore bounds, acquisition deadlines, and native backpressure isolation across all outbound I/O layers.

Prohibited Anti-Pattern: Unbounded Asynchronous Native I/O Dispatch Inducing Zig Worker Pool Starvation and FD Exhaustion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native Connection Pool Backpressure Isolation & Thread Starvation Guardrail - Bun 1.2 leverages native Zig/C++ async I/O primitives and uSockets underneath the JavaScript runtime. Unbounded asynchronous native operations—such as unthrottled fetch dispatches, unmetered native socket allocations, and limitless database pool acquisitions—exhaust native file descriptors and starve worker threads during traffic bursts. This standard mandates deterministic semaphore bounds, acquisition deadlines, and native backpressure isolation across all outbound I/O layers.
globs: src/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-native-connection-pool-backpressure-isolation"
---

# Bun 1.2 Native Connection Pool Backpressure Isolation & Thread Starvation Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Asynchronous Native I/O Dispatch Inducing Zig Worker Pool Starvation and FD Exhaustion

## System Constraints
You are a Principal Software Architect enforcing Bun 1.2 Native Connection Pool Backpressure Isolation. Mandate explicit concurrency gates, bounded acquisition queues, and fast-failing timeouts on all native I/O boundaries to eliminate worker thread starvation and event loop delays under bursty workloads.

## Strict Guidelines
- Engineers MUST ALWAYS bound concurrent native operations (fetch, native SQL, and TCP socket handles) using an explicit bounded semaphore or token-bucket controller.
- Engineers MUST NOT dispatch unbounded parallel arrays directly through Promise.all or Promise.allSettled without bounded concurrency slicing.
- Code SHOULD enforce strict acquisition timeouts on native connection pool checkout requests, immediately rejecting callers with a 503 or custom backpressure error when wait queues exceed capacity.
- Services MUST monitor and expose queue depth metrics and reject ingress traffic before backpressure cascades into file descriptor exhaustion.
- Components NEVER hold open native connection handles across tick-spanning asynchronous gaps without dedicated lease-abort hooks.

## Bad vs Good Implementations
### Outbound Native HTTP Fetch Dispatcher with Strict Backpressure Semaphore

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing hundreds or thousands of unmetered native fetch calls concurrently bypasses JavaScript queue discipline and overwhelms Bun's underlying Zig network thread pool. This leads to socket allocation stalls, delayed microtask resolution, and unrecoverable FD exhaustion.
```typescript
import { serve } from "bun";

// ANTI-PATTERN: Unbounded concurrent native fetch dispatches exhaust Bun's
// internal Zig worker queues and native epoll/kqueue file descriptors during spikes.
export async function handleBurstOutbound(requests: Array<{ id: string; url: string }>) {
  return Promise.all(
    requests.map(async (req) => {
      const response = await fetch(req.url, {
        headers: { "x-request-id": req.id }
      });
      return response.json();
    })
  );
}
```

#### Production Standard (Correct)
> **Standard**: Enforces deterministic concurrency limits (64 active native sockets) with bounded waiting queues (max 512) and explicit acquisition deadlines (1500ms). Prevents native worker thread starvation and rejects excess bursts gracefully before resource exhaustion occurs.
```typescript
export class NativeBackpressurePool {
  private running = 0;
  private queue: Array<{ resolve: () => void; reject: (err: Error) => void }> = [];

  constructor(
    private readonly maxConcurrent: number,
    private readonly maxQueueDepth: number,
    private readonly acquireTimeoutMs: number
  ) {}

  async acquire(signal?: AbortSignal): Promise<() => void> {
    if (signal?.aborted) {
      throw new Error("Operation aborted prior to pool acquisition");
    }

    if (this.running < this.maxConcurrent) {
      this.running++;
      return () => this.release();
    }

    if (this.queue.length >= this.maxQueueDepth) {
      throw new Error(`Connection pool backpressure limit reached: queue depth ${this.queue.length}`);
    }

    return new Promise<() => void>((resolve, reject) => {
      const timer = setTimeout(() => {
        const idx = this.queue.findIndex(item => item.reject === reject);
        if (idx !== -1) {
          this.queue.splice(idx, 1);
          reject(new Error(`Native pool acquisition timed out after ${this.acquireTimeoutMs}ms`));
        }
      }, this.acquireTimeoutMs);

      const abortHandler = () => {
        clearTimeout(timer);
        const idx = this.queue.findIndex(item => item.reject === reject);
        if (idx !== -1) {
          this.queue.splice(idx, 1);
          reject(new Error("Native pool acquisition aborted"));
        }
      };

      signal?.addEventListener("abort", abortHandler, { once: true });

      this.queue.push({
        resolve: () => {
          clearTimeout(timer);
          signal?.removeEventListener("abort", abortHandler);
          resolve(() => this.release());
        },
        reject
      });
    });
  }

  private release(): void {
    this.running--;
    const next = this.queue.shift();
    if (next) {
      this.running++;
      next.resolve();
    }
  }
}

const outboundPool = new NativeBackpressurePool(64, 512, 1500);

export async function handleBurstOutbound(requests: Array<{ id: string; url: string }>, parentSignal?: AbortSignal) {
  return Promise.all(
    requests.map(async (req) => {
      const release = await outboundPool.acquire(parentSignal);
      try {
        const controller = new AbortController();
        const timer = setTimeout(() => controller.abort(), 5000);
        const response = await fetch(req.url, {
          headers: { "x-request-id": req.id },
          signal: controller.signal
        });
        clearTimeout(timer);
        return await response.json();
      } finally {
        release();
      }
    })
  );
}
```

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 Connection Pool Backpressure Isolation & Thread Starvation Guardrail

Bun 1.2 Native Connection Pool Backpressure Isolation & Thread Starvation Guardrail defines an industrial-grade behavioral standard for backend engineering. Bun 1.2 leverages native Zig/C++ async I/O primitives and uSockets underneath the JavaScript runtime. Unbounded asynchronous native operations—such as unthrottled fetch dispatches, unmetered native socket allocations, and limitless database pool acquisitions—exhaust native file descriptors and starve worker threads during traffic bursts. This standard mandates deterministic semaphore bounds, acquisition deadlines, and native backpressure isolation across all outbound I/O layers. 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 Asynchronous Native I/O Dispatch Inducing Zig Worker Pool Starvation and FD Exhaustion 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 Asynchronous Native I/O Dispatch Inducing Zig Worker Pool Starvation and FD ExhaustionCritical
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 bound concurrent native operations (fetch, native SQL, and TCP socket handles) using an explicit bounded semaphore or token-bucket controller.

Engineers MUST ALWAYS bound concurrent native operations (fetch, native SQL, and TCP socket handles) using an explicit bounded semaphore or token-bucket controller.

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 dispatch unbounded parallel arrays directly through Promise.all or Promise.allSettled without bounded concurrency slicing.

Engineers MUST NOT dispatch unbounded parallel arrays directly through Promise.all or Promise.allSettled without bounded concurrency slicing.

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

Code SHOULD enforce strict acquisition timeouts on native connection pool checkout requests, immediately rejecting callers with a 503 or custom backpressure error when wait queues exceed capacity.

Code SHOULD enforce strict acquisition timeouts on native connection pool checkout requests, immediately rejecting callers with a 503 or custom backpressure error when wait queues exceed capacity.

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

Services MUST monitor and expose queue depth metrics and reject ingress traffic before backpressure cascades into file descriptor exhaustion.

Services MUST monitor and expose queue depth metrics and reject ingress traffic before backpressure cascades into file descriptor exhaustion.

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

Components NEVER hold open native connection handles across tick-spanning asynchronous gaps without dedicated lease-abort hooks.

Components NEVER hold open native connection handles across tick-spanning asynchronous gaps without dedicated lease-abort hooks.

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 Connection Pool Backpressure Isolation & Thread Starvation Guardrail critical for AI coding assistants?

Bun 1.2 leverages native Zig/C++ async I/O primitives and uSockets underneath the JavaScript runtime. Unbounded asynchronous native operations—such as unthrottled fetch dispatches, unmetered native socket allocations, and limitless database pool acquisitions—exhaust native file descriptors and starve worker threads during traffic bursts. This standard mandates deterministic semaphore bounds, acquisition deadlines, and native backpressure isolation across all outbound I/O layers. 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 Asynchronous Native I/O Dispatch Inducing Zig Worker Pool Starvation and FD Exhaustion) 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