electric-sql / electric-sql/pglite

[Bug] WASM 'Unreachable code' error when using PGlite Socket Server with Bun

Open
#812 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

Disclaimer: written with AI, came up in a vibe coding session (but reviewed/edited by @divyenduz the human). Already tried the latest versions of `@electric-sql/pglite@0.3.10` and `@electric-sql/pglite-socket@0.0.15`.

## Bug Description

When using `@electric-sql/pglite-socket` with `@electric-sql/pglite@0.2.17` in Bun runtime, the server crashes with a WASM error when a PostgreSQL client attempts to connect.

## Error Message w/ `pglite` 0.2.17

```
RuntimeError: Unreachable code should not be executed (evaluating 'getWasmTableEntry(e)(t)')
at invoke_ii (.../node_modules/@electric-sql/pglite/dist/index.js:3:230030)
at execProtocolRaw (.../node_modules/@electric-sql/pglite/dist/index.js:3:239233)
at handleData (.../node_modules/@electric-sql/pglite-socket/dist/chunk-F6KLIXM7.js:1:2032)
```

## Error Message w/ `pglite` 0.3.10

```
Command failed, RuntimeError: Unreachable code should not be executed (evaluating 'this.mod._pgl_backend()')
at unknown
at unknown
at unknown
at (/Users/divyendusingh/Documents/zoid/playground/dbdev/node_modules/@electric-sql/pglite/dist/index.js:3:192674)
at async create (/Users/divyendusingh/Documents/zoid/playground/dbdev/node_modules/@electric-sql/pglite/dist/index.js:3:186333)
at async cmd (/Users/divyendusingh/Documents/zoid/playground/dbdev/src/commands/start.ts:27:27)
at async Te (/Users/divyendusingh/Documents/zoid/playground/dbdev/node_modules/@stricli/core/dist/index.js:7:41)
at processTicksAndRejections (native:7:39)
```

## Minimal Reproduction

```typescript
import { PGlite } from '@electric-sql/pglite';
import { PGLiteSocketServer } from '@electric-sql/pglite-socket';

async function main() {
const db = await PGlite.create('memory://', {
debug: 0,
});

const server = new PGLiteSocketServer({
db,
port: 5432,
host: '127.0.0.1',
});

await server.start();
console.log('Server started on port 5432');
}

main().catch(console.error);
```

**Steps to reproduce:**

1. Save the above code as `index.ts`
2. Create `package.json`:
```json
{
"name": "repro",
"type": "module",
"dependencies": {
"@electric-sql/pglite": "0.2.17",
"@electric-sql/pglite-socket": "0.0.15"
}
}
```
3. Run: `bun install && bun run index.ts`
4. In another terminal: `PGSSLMODE=disable psql -h localhost -p 5432 -U postgres -d postgres`

## Expected Behavior

The psql client should successfully connect to the PGlite Socket Server and be able to execute queries.

## Actual Behavior

The server crashes immediately when the client attempts to connect, with a WASM "Unreachable code" error in `execProtocolRaw`.

## Environment

- **Bun**: v1.3.0
- **@electric-sql/pglite**: 0.2.17
- **@electric-sql/pglite-socket**: 0.0.15
- **OS**: macOS arm64 (Darwin 24.6.0)
- **Architecture**: arm64

## Additional Context

- The same code works correctly in Node.js runtime
- The error occurs specifically when handling the PostgreSQL wire protocol messages
- Downgrading to `@electric-sql/pglite@0.2.12` avoids the WASM crash but causes a different error (`runExclusive is not a function`) since that method doesn't exist in v0.2.12
- The error suggests this might be a WASM compilation or compatibility issue between PGlite's WASM module and Bun's WASM runtime

## Workaround

Currently no workaround available for using PGlite Socket Server with Bun. A handrolled proxy server using `execProtocolRaw` directly with PGlite 0.2.12 works, but that's not a viable long-term solution.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.