Skip to content

Commit

Permalink
Handle db pool errors on appview (#1483)
Browse files Browse the repository at this point in the history
handle db pool errors on bav
  • Loading branch information
devinivy authored Aug 16, 2023
1 parent 8eb9303 commit 34b8413
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/bsky/src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Database {
throw new Error(`Postgres schema must only contain [A-Za-z_]: ${schema}`)
}

pool.on('error', onPoolError)
pool.on('connect', (client) => {
client.on('error', onClientError)
// Used for trigram indexes, e.g. on actor search
Expand Down Expand Up @@ -86,4 +87,5 @@ export class Database {

export default Database

const onPoolError = (err: Error) => dbLogger.error({ err }, 'db pool error')
const onClientError = (err: Error) => dbLogger.error({ err }, 'db client error')
11 changes: 11 additions & 0 deletions packages/bsky/tests/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe('db', () => {
await expect(tryKillConnection).rejects.toThrow()
})

it('handles pool errors without crashing.', async () => {
const conn1 = await db.pool.connect()
const conn2 = await db.pool.connect()
const result = await conn1.query('select pg_backend_pid() as pid;')
const conn1pid: number = result.rows[0].pid
conn1.release()
await wait(100) // let release apply, conn is now idle on pool.
await conn2.query(`select pg_terminate_backend(${conn1pid});`)
conn2.release()
})

describe('transaction()', () => {
it('commits changes', async () => {
const result = await db.transaction(async (dbTxn) => {
Expand Down

0 comments on commit 34b8413

Please sign in to comment.