Skip to content

Commit

Permalink
Handle db client errors on appview (bluesky-social#1481)
Browse files Browse the repository at this point in the history
handle db client errors on bav
  • Loading branch information
devinivy authored and mloar committed Sep 25, 2023
1 parent 0f4aead commit d0b3b73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/bsky/src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Kysely, PostgresDialect } from 'kysely'
import { Pool as PgPool, types as pgTypes } from 'pg'
import DatabaseSchema, { DatabaseSchemaType } from './database-schema'
import { PgOptions } from './types'
import { dbLogger } from '../logger'

export class Database {
pool: PgPool
Expand Down Expand Up @@ -41,6 +42,7 @@ export class Database {
}

pool.on('connect', (client) => {
client.on('error', onClientError)
// Used for trigram indexes, e.g. on actor search
client.query('SET pg_trgm.word_similarity_threshold TO .4;')
if (schema) {
Expand Down Expand Up @@ -83,3 +85,5 @@ export class Database {
}

export default Database

const onClientError = (err: Error) => dbLogger.error({ err }, 'db client error')
14 changes: 14 additions & 0 deletions packages/bsky/tests/db.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { once } from 'events'
import { sql } from 'kysely'
import { wait } from '@atproto/common'
import { TestNetwork } from '@atproto/dev-env'
import { Database } from '../src'
Expand All @@ -20,6 +21,19 @@ describe('db', () => {
await network.close()
})

it('handles client errors without crashing.', async () => {
const tryKillConnection = db.transaction(async (dbTxn) => {
const result = await sql`select pg_backend_pid() as pid;`.execute(
dbTxn.db,
)
const pid = result.rows[0]?.['pid'] as number
await sql`select pg_terminate_backend(${pid});`.execute(db.db)
await sql`select 1;`.execute(dbTxn.db)
})
// This should throw, but no unhandled error
await expect(tryKillConnection).rejects.toThrow()
})

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

0 comments on commit d0b3b73

Please sign in to comment.