This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-sqlite-multi-worker-concurrency-wal-lease

Bun 1.2 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol

Enforces thread-isolated native SQLite connection handles across Bun Web Workers and mandates deterministic WAL write-lease arbitration. Prevents native pointer corruption, segfaults from cross-thread handle transfers, and unrecoverable SQLITE_BUSY deadlocks during concurrent multi-worker writes.

Prohibited Anti-Pattern: Sharing Native bun:sqlite Instances Across Workers or Executing Uncoordinated Concurrent Transactions

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol - Enforces thread-isolated native SQLite connection handles across Bun Web Workers and mandates deterministic WAL write-lease arbitration. Prevents native pointer corruption, segfaults from cross-thread handle transfers, and unrecoverable SQLITE_BUSY deadlocks during concurrent multi-worker writes.
globs: src/**/*.worker.ts, src/**/db.ts, src/**/database.ts, src/workers/**/*.ts, src/**/worker-*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-sqlite-multi-worker-concurrency-wal-lease"
---

# Bun 1.2 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Sharing Native bun:sqlite Instances Across Workers or Executing Uncoordinated Concurrent Transactions

## System Constraints
You are a Principal Backend Architect enforcing strict database concurrency standards for Bun 1.2. SQLite native handles (Database instances) wrap C-level pointers tied to OS threads; they are not thread-safe and must never be shared across worker boundaries. Furthermore, SQLite in WAL mode allows multiple concurrent readers but strictly one writer at any moment. You must mandate that every worker manages its own isolated connection with WAL mode and explicit busy timeouts, and that all write operations use serialized actor messaging or BEGIN IMMEDIATE with defensive retry backoff.

## Strict Guidelines
- Engineers MUST NEVER attempt to pass a native `bun:sqlite` `Database` instance across threads via `postMessage()`, `SharedArrayBuffer`, or global variables.
- Each Bun `Worker` needing database access MUST open its own independent `Database` handle to the SQLite file.
- Every SQLite handle MUST execute `PRAGMA journal_mode = WAL;` and `PRAGMA busy_timeout = 5000;` immediately upon connection initialization.
- Write operations MUST use `BEGIN IMMEDIATE` instead of standard `BEGIN` or `BEGIN DEFERRED` to guarantee write lock acquisition at transaction start rather than upon first write.
- High-concurrency systems MUST route write operations through a dedicated Single-Writer Worker (Actor Pattern), leaving reader workers strictly read-only (`PRAGMA query_only = ON;`).
- Workers MUST handle `SQLITE_BUSY` exceptions explicitly with exponential jitter backoff if concurrent multi-process writes cannot be avoided.

## Bad vs Good Implementations
### Violating SQLite Thread Safety by Sharing Handles and Lacking WAL Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Bun SQLite handles are C-level native structs bound to the thread of creation. Passing them to a Worker throws a DataCloneError or crashes the runtime with memory corruption. Additionally, running standard BEGIN without WAL configuration or busy timeouts guarantees SQLITE_BUSY errors when multiple workers attempt writes simultaneously.
```typescript
import { Database } from 'bun:sqlite';

// ANTI-PATTERN: Instantiating db in the main thread and attempting to pass it to a worker,
// or assuming default journal mode handles multi-threaded contention.
const db = new Database('app.db');

const worker = new Worker(new URL('./task.worker.ts', import.meta.url));
// Fails: Native Database handle cannot be serialized over postMessage structured clone
worker.postMessage({ type: 'EXECUTE', db });

// ANTI-PATTERN inside a worker: Default deferred transaction causes SQLITE_BUSY mid-transaction
export function executeWorkerTask(database: Database, data: string) {
  database.run('BEGIN TRANSACTION;');
  database.run('INSERT INTO tasks (payload) VALUES (?)', [data]);
  database.run('COMMIT;');
}
```

#### Production Standard (Correct)
> **Standard**: Each worker opens its own native `Database` handle, ensuring OS thread isolation. Setting `PRAGMA journal_mode = WAL;` and `PRAGMA busy_timeout = 5000;` prevents reader/writer blocking and handles transient contention. Using `db.transaction()` (which issues `BEGIN IMMEDIATE`) ensures atomic write lease acquisition without mid-stream lock upgrade deadlocks.
```typescript
import { Database } from 'bun:sqlite';

// task.worker.ts: Worker instantiates its own isolated connection with WAL Pragmas
declare var self: Worker;

function initializeWorkerDb(dbPath: string): Database {
  const db = new Database(dbPath, { strict: true });
  // Mandate WAL journal mode and deterministic busy timeout
  db.exec('PRAGMA journal_mode = WAL;');
  db.exec('PRAGMA busy_timeout = 5000;');
  db.exec('PRAGMA synchronous = NORMAL;');
  return db;
}

const db = initializeWorkerDb('app.db');

// Prepared statement cached per-worker handle
const insertTask = db.prepare('INSERT INTO tasks (payload) VALUES ($payload)');

self.onmessage = (event: MessageEvent<{ payload: string }>) => {
  const { payload } = event.data;
  
  // Use BEGIN IMMEDIATE to acquire the WAL write-lock eagerly, avoiding mid-transaction deadlocks
  const transaction = db.transaction((data: string) => {
    insertTask.run({ $payload: data });
  });

  try {
    // db.transaction in bun:sqlite defaults to BEGIN IMMEDIATE
    transaction(payload);
    self.postMessage({ success: true });
  } catch (err: unknown) {
    self.postMessage({ success: false, error: (err as Error).message });
  }
};
```

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 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol

Bun 1.2 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol defines an industrial-grade behavioral standard for backend engineering. Enforces thread-isolated native SQLite connection handles across Bun Web Workers and mandates deterministic WAL write-lease arbitration. Prevents native pointer corruption, segfaults from cross-thread handle transfers, and unrecoverable SQLITE_BUSY deadlocks during concurrent multi-worker writes. 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: Sharing Native bun:sqlite Instances Across Workers or Executing Uncoordinated Concurrent Transactions 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 DriftSharing Native bun:sqlite Instances Across Workers or Executing Uncoordinated Concurrent TransactionsCritical
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 NEVER attempt to pass a native `bun

Engineers MUST NEVER attempt to pass a native `bun:sqlite` `Database` instance across threads via `postMessage()`, `SharedArrayBuffer`, or global variables.

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

Each Bun `Worker` needing database access MUST open its own independent `Database` handle to the SQLite file.

Each Bun `Worker` needing database access MUST open its own independent `Database` handle to the SQLite file.

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

Every SQLite handle MUST execute `PRAGMA journal_mode = WAL

Every SQLite handle MUST execute `PRAGMA journal_mode = WAL;` and `PRAGMA busy_timeout = 5000;` immediately upon connection initialization.

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

Write operations MUST use `BEGIN IMMEDIATE` instead of standard `BEGIN` or `BEGIN DEFERRED` to guarantee write lock acquisition at transaction start rather than upon first write.

Write operations MUST use `BEGIN IMMEDIATE` instead of standard `BEGIN` or `BEGIN DEFERRED` to guarantee write lock acquisition at transaction start rather than upon first write.

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

High-concurrency systems MUST route write operations through a dedicated Single-Writer Worker (Actor Pattern), leaving reader workers strictly read-only (`PRAGMA query_only = ON

High-concurrency systems MUST route write operations through a dedicated Single-Writer Worker (Actor Pattern), leaving reader workers strictly read-only (`PRAGMA query_only = ON;`).

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

Workers MUST handle `SQLITE_BUSY` exceptions explicitly with exponential jitter backoff if concurrent multi-process writes cannot be avoided.

Workers MUST handle `SQLITE_BUSY` exceptions explicitly with exponential jitter backoff if concurrent multi-process writes cannot be avoided.

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 Multi-Worker SQLite Handle Isolation and WAL Write-Lease Protocol critical for AI coding assistants?

Enforces thread-isolated native SQLite connection handles across Bun Web Workers and mandates deterministic WAL write-lease arbitration. Prevents native pointer corruption, segfaults from cross-thread handle transfers, and unrecoverable SQLITE_BUSY deadlocks during concurrent multi-worker writes. 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 (Sharing Native bun:sqlite Instances Across Workers or Executing Uncoordinated Concurrent Transactions) 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