This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-cluster-ipc-socket-handoff

Bun 1.2 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard

Enforces deterministic TCP connection handoffs and zero-downtime worker recycling under Bun 1.2 cluster topologies using non-blocking IPC handle transfers. Guarantees starvation-free connection distribution and prevents kernel file descriptor leaks (EMFILE) during high-throughput rolling process upgrades.

Prohibited Anti-Pattern: Leaking Kernel FDs and Dropping Inflight Connections via Blind Worker Termination and Uncoordinated IPC Socket Transfers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard - Enforces deterministic TCP connection handoffs and zero-downtime worker recycling under Bun 1.2 cluster topologies using non-blocking IPC handle transfers. Guarantees starvation-free connection distribution and prevents kernel file descriptor leaks (EMFILE) during high-throughput rolling process upgrades.
globs: src/cluster/**/*.{ts,tsx}, src/server/**/*.{ts,tsx}, src/workers/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-cluster-ipc-socket-handoff"
---

# Bun 1.2 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Leaking Kernel FDs and Dropping Inflight Connections via Blind Worker Termination and Uncoordinated IPC Socket Transfers

## System Constraints
You are a Principal Engineer enforcing the Bun 1.2 Cluster IPC Non-Blocking Socket Handoff Standard. You mandate zero-downtime process recycling, atomic socket handle passing via IPC, and strict lifecycle accounting to eliminate EMFILE errors and connection starvation.

## Strict Guidelines
- Engineers MUST ALWAYS transfer TCP socket handles via structured IPC messages with bidirectional ACK protocols before closing sender-side descriptors.
- Engineers MUST immediately pause or detach local socket event loops on the dispatcher before dispatching the socket handle to a target worker.
- Services MUST implement dual-phase graceful shutdown: cease accepting new sockets, notify the master/cluster supervisor, drain active connections within a strict timeout, and then exit cleanly.
- Workers MUST report in-flight socket gauge metrics over IPC to enable load-aware socket handoff and prevent single-worker starvation.
- Components NEVER execute raw process.exit() or worker.terminate() without verifying that the socket migration queue and IPC ring-buffers are completely flushed.

## Bad vs Good Implementations
### IPC Socket Migration and Worker Drain in Bun

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Passing socket references without relinquishing ownership or pausing read events causes duplicate reads and race conditions between parent and child event loops. Abrupt termination via `worker.terminate()` discards inflight connections and bypasses kernel descriptor cleanup.
```typescript
import { Worker } from 'bun';

// ANTI-PATTERN: Blindly spawning workers and passing raw sockets without ownership transfer,
// dropping inflight requests on reload and causing epoll descriptor leaks (EMFILE).
const worker = new Worker('./worker.ts');

Bun.listen({
  port: 8080,
  hostname: '0.0.0.0',
  socket: {
    data(socket) {
      // Race condition: passing socket reference without detaching listener or awaiting ACK
      worker.postMessage({ type: 'SOCKET', socket });
    },
    close() {
      // Worker might still be using the handle
    }
  }
});

process.on('SIGUSR2', () => {
  // Abrupt termination drops active connections and leaks FDs
  worker.terminate();
  process.exit(0);
});
```

#### Production Standard (Correct)
> **Standard**: Implements deterministic non-blocking socket handoff using transfer lists, pauses socket reads before dispatching to prevent dual-owner race conditions, routes traffic to the least-loaded worker, and enforces a strict two-phase graceful drain timeout.
```typescript
import { Worker } from 'bun';

interface WorkerState {
  worker: Worker;
  activeConnections: number;
  draining: boolean;
}

const WORKER_COUNT = navigator.hardwareConcurrency || 4;
const DRAIN_TIMEOUT_MS = 10_000;
const workers: WorkerState[] = [];

for (let i = 0; i < WORKER_COUNT; i++) {
  spawnWorker(i);
}

function spawnWorker(index: number) {
  const worker = new Worker(new URL('./worker.ts', import.meta.url).href);
  const state: WorkerState = { worker, activeConnections: 0, draining: false };

  worker.onmessage = (event: MessageEvent) => {
    const { type, count } = event.data;
    if (type === 'METRICS_UPDATE') {
      state.activeConnections = count;
    } else if (type === 'DRAIN_COMPLETE') {
      state.worker.terminate();
      workers[index] = spawnWorker(index);
    }
  };

  workers[index] = state;
  return state;
}

function getLeastLoadedWorker(): WorkerState | null {
  const available = workers.filter(w => !w.draining);
  if (available.length === 0) return null;
  return available.reduce((prev, curr) => 
    curr.activeConnections < prev.activeConnections ? curr : prev
  );
}

const server = Bun.listen({
  port: 8080,
  hostname: '0.0.0.0',
  socket: {
    open(socket) {
      const target = getLeastLoadedWorker();
      if (!target) {
        socket.end('HTTP/1.1 503 Service Unavailable\r\nRetry-After: 5\r\n\r\n');
        return;
      }
      // Detach/pause local interest before handoff to eliminate dual-owner race conditions
      socket.pause();
      target.worker.postMessage({ type: 'HANDOFF_SOCKET' }, [socket]);
    },
    data() {},
    error(socket, error) {
      console.error('Master socket error:', error);
      socket.end();
    }
  }
});

async function reloadWorkers(): Promise<void> {
  for (const state of workers) {
    state.draining = true;
    state.worker.postMessage({ type: 'INITIATE_DRAIN', timeout: DRAIN_TIMEOUT_MS });
    
    // Guardrail against hung workers preventing deployment
    setTimeout(() => {
      if (state.activeConnections > 0) {
        console.warn('Forcing termination of stalled worker during drain');
        state.worker.terminate();
      }
    }, DRAIN_TIMEOUT_MS + 1000).unref();
  }
}

process.on('SIGUSR2', () => reloadWorkers());
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Bun 1.2 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard

Bun 1.2 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic TCP connection handoffs and zero-downtime worker recycling under Bun 1.2 cluster topologies using non-blocking IPC handle transfers. Guarantees starvation-free connection distribution and prevents kernel file descriptor leaks (EMFILE) during high-throughput rolling process upgrades. 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: Leaking Kernel FDs and Dropping Inflight Connections via Blind Worker Termination and Uncoordinated IPC Socket Transfers 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 DriftLeaking Kernel FDs and Dropping Inflight Connections via Blind Worker Termination and Uncoordinated IPC Socket TransfersCritical
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 transfer TCP socket handles via structured IPC messages with bidirectional ACK protocols before closing sender-side descriptors.

Engineers MUST ALWAYS transfer TCP socket handles via structured IPC messages with bidirectional ACK protocols before closing sender-side descriptors.

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 immediately pause or detach local socket event loops on the dispatcher before dispatching the socket handle to a target worker.

Engineers MUST immediately pause or detach local socket event loops on the dispatcher before dispatching the socket handle to a target worker.

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 implement dual-phase graceful shutdown

Services MUST implement dual-phase graceful shutdown: cease accepting new sockets, notify the master/cluster supervisor, drain active connections within a strict timeout, and then exit cleanly.

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

Workers MUST report in-flight socket gauge metrics over IPC to enable load-aware socket handoff and prevent single-worker starvation.

Workers MUST report in-flight socket gauge metrics over IPC to enable load-aware socket handoff and prevent single-worker starvation.

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 execute raw process.exit() or worker.terminate() without verifying that the socket migration queue and IPC ring-buffers are completely flushed.

Components NEVER execute raw process.exit() or worker.terminate() without verifying that the socket migration queue and IPC ring-buffers are completely flushed.

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 Cluster IPC Non-Blocking Socket Handoff & Graceful Drain Standard critical for AI coding assistants?

Enforces deterministic TCP connection handoffs and zero-downtime worker recycling under Bun 1.2 cluster topologies using non-blocking IPC handle transfers. Guarantees starvation-free connection distribution and prevents kernel file descriptor leaks (EMFILE) during high-throughput rolling process upgrades. 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 (Leaking Kernel FDs and Dropping Inflight Connections via Blind Worker Termination and Uncoordinated IPC Socket Transfers) 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