Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions src/js/internal/sql/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,31 +319,35 @@ class PooledMySQLConnection {
}

const connectionInfo = this.connectionInfo;
if (connectionInfo?.onconnect) {
connectionInfo.onconnect(err);
}
this.storedError = err;
if (!err) {
this.connectStartedAt = 0;
this.flags |= PooledConnectionFlags.canBeConnected;
}
this.state = err ? PooledConnectionState.closed : PooledConnectionState.connected;
const onFinish = this.onFinish;
if (onFinish) {
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
this.flags &= ~PooledConnectionFlags.preReserved;

// pool is closed, lets finish the connection
// pool is closed, lets finish the connection
if (err) {
onFinish(err);
try {
// user code; a throw must not abort the pool bookkeeping below
// (the exception keeps propagating after the finally block runs)
if (connectionInfo?.onconnect) {
connectionInfo.onconnect(err);
}
} finally {
this.storedError = err;
if (!err) {
this.connectStartedAt = 0;
this.flags |= PooledConnectionFlags.canBeConnected;
}
this.state = err ? PooledConnectionState.closed : PooledConnectionState.connected;
const onFinish = this.onFinish;
if (onFinish) {
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
this.flags &= ~PooledConnectionFlags.preReserved;

// pool is closed, lets finish the connection
if (err) {
onFinish(err);
} else {
this.connection?.close();
}
} else {
this.connection?.close();
this.adapter.release(this, true);
}
return;
}
this.adapter.release(this, true);
}

#onClose(err) {
Expand Down Expand Up @@ -380,29 +384,34 @@ class PooledMySQLConnection {

#finishClose(err) {
const connectionInfo = this.connectionInfo;
if (connectionInfo?.onclose) {
connectionInfo.onclose(err);
}
this.state = PooledConnectionState.closed;
this.storedError = err;
try {
// user code; a throw must not abort the pool bookkeeping below
// (the exception keeps propagating after the finally block runs)
if (connectionInfo?.onclose) {
connectionInfo.onclose(err);
}
} finally {
this.state = PooledConnectionState.closed;
this.storedError = err;

// remove from ready connections if its there
this.adapter.readyConnections.delete(this);
const queries = new Set(this.queries);
this.queries?.clear?.();
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;

// remove from ready connections if its there
this.adapter.readyConnections.delete(this);
const queries = new Set(this.queries);
this.queries?.clear?.();
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
// notify all queries that the connection is closed
for (const onClose of queries) {
onClose(err);
}
const onFinish = this.onFinish;
if (onFinish) {
onFinish(err);
}

// notify all queries that the connection is closed
for (const onClose of queries) {
onClose(err);
}
const onFinish = this.onFinish;
if (onFinish) {
onFinish(err);
this.adapter.release(this, true);
}

this.adapter.release(this, true);
}

constructor(connectionInfo: Bun.SQL.__internal.DefinedMySQLOptions, adapter: MySQLAdapter) {
Expand Down
101 changes: 58 additions & 43 deletions src/js/internal/sql/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ function onQueryFinish(this: PooledPostgresConnection, onClose: (err: Error) =>
this.adapter.release(this);
}

function closeNT(onClose: (err: Error) => void, err: Error | null) {
onClose(err as Error);
}

class PooledPostgresConnection {
private static async createConnection(
options: Bun.SQL.__internal.DefinedPostgresOrMySQLOptions,
Expand Down Expand Up @@ -515,7 +519,9 @@ class PooledPostgresConnection {
!prepare,
);
} catch (e) {
onClose(e as Error);
// defer so the callback never runs while the adapter is still filling
// this.connections (it scans that array); mysql.ts does the same
process.nextTick(closeNT, onClose, e);
return null;
}
}
Expand All @@ -542,31 +548,35 @@ class PooledPostgresConnection {
err = wrapPostgresError(err);
}
const connectionInfo = this.connectionInfo;
if (connectionInfo?.onconnect) {
connectionInfo.onconnect(err);
}
this.storedError = err;
if (!err) {
this.connectStartedAt = 0;
this.flags |= PooledConnectionFlags.canBeConnected;
}
this.state = err ? PooledConnectionState.closed : PooledConnectionState.connected;
const onFinish = this.onFinish;
if (onFinish) {
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
this.flags &= ~PooledConnectionFlags.preReserved;

// pool is closed, lets finish the connection
// pool is closed, lets finish the connection
if (err) {
onFinish(err);
try {
// user code; a throw must not abort the pool bookkeeping below
// (the exception keeps propagating after the finally block runs)
if (connectionInfo?.onconnect) {
connectionInfo.onconnect(err);
}
} finally {
this.storedError = err;
if (!err) {
this.connectStartedAt = 0;
this.flags |= PooledConnectionFlags.canBeConnected;
}
this.state = err ? PooledConnectionState.closed : PooledConnectionState.connected;
const onFinish = this.onFinish;
if (onFinish) {
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
this.flags &= ~PooledConnectionFlags.preReserved;

// pool is closed, lets finish the connection
if (err) {
onFinish(err);
} else {
this.connection?.close();
}
} else {
this.connection?.close();
this.adapter.release(this, true);
}
return;
}
this.adapter.release(this, true);
}

#onClose(err) {
Expand Down Expand Up @@ -603,29 +613,34 @@ class PooledPostgresConnection {

#finishClose(err) {
const connectionInfo = this.connectionInfo;
if (connectionInfo?.onclose) {
connectionInfo.onclose(err);
}
this.state = PooledConnectionState.closed;
this.storedError = err;
try {
// user code; a throw must not abort the pool bookkeeping below
// (the exception keeps propagating after the finally block runs)
if (connectionInfo?.onclose) {
connectionInfo.onclose(err);
}
} finally {
this.state = PooledConnectionState.closed;
this.storedError = err;

// remove from ready connections if its there
this.adapter.readyConnections?.delete(this);
const queries = new Set(this.queries);
this.queries?.clear?.();
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;

// remove from ready connections if its there
this.adapter.readyConnections?.delete(this);
const queries = new Set(this.queries);
this.queries?.clear?.();
this.queryCount = 0;
this.flags &= ~PooledConnectionFlags.reserved;
// notify all queries that the connection is closed
for (const onClose of queries) {
onClose(err);
}
const onFinish = this.onFinish;
if (onFinish) {
onFinish(err);
}

// notify all queries that the connection is closed
for (const onClose of queries) {
onClose(err);
}
const onFinish = this.onFinish;
if (onFinish) {
onFinish(err);
this.adapter.release(this, true);
}

this.adapter.release(this, true);
}

constructor(connectionInfo: Bun.SQL.__internal.DefinedPostgresOrMySQLOptions, adapter: PostgresAdapter) {
Expand Down
Loading
Loading