Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

fix: add Postgres pool connect timeout and idle error handler (#762)

Bhavesh Patelc221ac626b2f
diff --git a/.changeset/postgres-pool-idle-error.md b/.changeset/postgres-pool-idle-error.md
new file mode 100644
index 000000000..57547b66b
--- /dev/null
+++ b/.changeset/postgres-pool-idle-error.md
@@ -0,0 +1,5 @@
+---
+"@truefoundry/trueforge": patch
+---
+
+Fail Postgres connects after 10s and log idle pool errors so a backend restart cannot crash the process.
diff --git a/packages/trueforge/src/db/postgres/client.ts b/packages/trueforge/src/db/postgres/client.ts
index a92f595b9..e83e6f77b 100644
--- a/packages/trueforge/src/db/postgres/client.ts
+++ b/packages/trueforge/src/db/postgres/client.ts
@@ -47,16 +47,23 @@ export function createDb(options: {
}): Kysely<Database> {
const { connectionString, poolMax, statementTimeoutMs, idleInTransactionSessionTimeoutMs, ssl } = options;
configurePgTypeParsers();
+ const pool = new Pool({
+ connectionString,
+ max: poolMax,
+ // pg default 0 waits forever; 10s covers in-cluster TCP+auth and fails fast if Postgres is down.
+ connectionTimeoutMillis: 10_000,
+ statement_timeout: statementTimeoutMs,
+ idle_in_transaction_session_timeout: idleInTransactionSessionTimeoutMs,
+ options: `-c search_path=${TRUEFORGE_SCHEMA}`,
+ ...(ssl !== undefined ? { ssl } : {}),
+ });
+ // Idle clients emit 'error' when the backend closes; without a listener Node exits.
+ pool.on('error', (error: Error) => {
+ console.error('Unexpected Postgres pool error on idle client', error);
+ });
return new Kysely<Database>({
dialect: new PostgresDialect({
- pool: new Pool({
- connectionString,
- max: poolMax,
- statement_timeout: statementTimeoutMs,
- idle_in_transaction_session_timeout: idleInTransactionSessionTimeoutMs,
- options: `-c search_path=${TRUEFORGE_SCHEMA}`,
- ...(ssl !== undefined ? { ssl } : {}),
- }),
+ pool,
}),
});
}