drizzle-team / drizzle-team/drizzle-orm

[BUG]: Insert commands in transaction fail to throw error on connection drop

Open
#1,008 0 comments 1 reaction 0 assignees View on GitHub
bug improvement qb/transactions
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.28.1

### What version of `drizzle-kit` are you using?

_No response_

### Describe the Bug

When the postgres client is disconnected, while a transaction with an insert command is running, the transaction will hang.

Steps to reproduce:
1. Connect drizzle client
2. Run a transaction that executes an insert command
3. Disconnect the client while the transaction is still running

```typescript
import postgres from 'postgres';
import { drizzle } from 'drizzle-orm/postgres-js';
import { sql } from 'drizzle-orm';

export async function runTest() {
const pgClient = postgres('');
const drizzleClient = drizzle(pgClient);

console.log('start test');
await drizzleClient.execute(sql`
DROP TABLE IF EXISTS __TEST__TABLE;
CREATE TABLE __TEST__TABLE ( data TEXT );
`);

const longString = 'X'.repeat(1_000_000);

// disconnect postgres client after 500ms
setTimeout(() => {
console.log('disconnect postgres client');
pgClient.end({ timeout: 0 });
}, 500);

await drizzleClient.transaction(async tx => {
for (let i = 0; i < 2000; i++) {
console.log('insert row', i);

// this will neither throw an error nor return a result, it will just hang
await tx.execute(sql`
INSERT INTO __TEST__TABLE (data)
VALUES (${longString});
`);
}
});

console.log('end test');
await pgClient.end();
}
```

### Expected behavior

i expect the execution of the insert command to fail with an error (reject promise)
```typescript
await tx.execute(sql`
INSERT INTO __TEST__TABLE (data)
VALUES (${longString});
`);
```

### Environment & setup

- postgres.js: 3.3.5
- Postgres: 15.3
- Node: 18.16.1 & 20.4.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.