From c82b8aebcaa9220e6de5904ae5f6bc9561aede3d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 19 May 2026 11:15:05 +0000 Subject: [PATCH 1/3] Add retry mechanism for database connection on startup Agent-Logs-Url: https://github.com/gabriel20xx/CollabTable/sessions/2bd9068b-15e3-43bb-81b9-94b4fb93bfdc Co-authored-by: gabriel20xx <21219769+gabriel20xx@users.noreply.github.com> --- CollabTableServer/package-lock.json | 35 +---------------------------- CollabTableServer/src/db.ts | 26 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/CollabTableServer/package-lock.json b/CollabTableServer/package-lock.json index 041ca74..3f7feb0 100644 --- a/CollabTableServer/package-lock.json +++ b/CollabTableServer/package-lock.json @@ -18,8 +18,7 @@ "eslint-config-prettier": "^9.1.0", "express": "^4.18.2", "pg": "^8.11.3", - "prettier": "^3.3.3", - "ws": "^8.16.0" + "prettier": "^3.3.3" }, "devDependencies": { "@types/better-sqlite3": "^7.6.11", @@ -27,7 +26,6 @@ "@types/express": "^4.17.21", "@types/node": "^20.10.6", "@types/pg": "^8.10.2", - "@types/ws": "^8.5.10", "ts-node-dev": "^2.0.0", "typescript": "^5.3.3" } @@ -452,16 +450,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@types/ws": { - "version": "8.18.1", - "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz", - "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==", - "dev": true, - "license": "MIT", - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "7.18.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.18.0.tgz", @@ -3717,27 +3705,6 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "license": "ISC" }, - "node_modules/ws": { - "version": "8.18.3", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz", - "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==", - "license": "MIT", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/CollabTableServer/src/db.ts b/CollabTableServer/src/db.ts index f47d34b..84650f4 100644 --- a/CollabTableServer/src/db.ts +++ b/CollabTableServer/src/db.ts @@ -296,7 +296,31 @@ export const dbAdapter: DBAdapter = clientType === 'postgres' || clientType === : new SqliteAdapter(); export async function initializeDatabase() { - await dbAdapter.initialize(); + const maxRetries = parseInt(process.env.DB_CONNECT_RETRIES || '10', 10); + const retryDelayMs = parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS || '3000', 10); + + for (let attempt = 1; attempt <= maxRetries; attempt++) { + try { + await dbAdapter.initialize(); + return; + } catch (err: any) { + const isConnectionError = + err?.code === 'ECONNREFUSED' || + err?.code === 'ENOTFOUND' || + err?.code === 'ETIMEDOUT' || + err?.message?.includes('connect'); + + if (!isConnectionError || attempt === maxRetries) { + throw err; + } + + const delay = retryDelayMs * attempt; + console.warn( + `Database connection attempt ${attempt}/${maxRetries} failed (${err.code || err.message}). Retrying in ${delay}ms...` + ); + await new Promise(resolve => setTimeout(resolve, delay)); + } + } } export type { DBAdapter }; From 76c4a5ef9648a20ffdbd5847093b2e4b6567461f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 19 May 2026 11:16:09 +0000 Subject: [PATCH 2/3] Use exponential backoff and tighten connection error detection Agent-Logs-Url: https://github.com/gabriel20xx/CollabTable/sessions/2bd9068b-15e3-43bb-81b9-94b4fb93bfdc Co-authored-by: gabriel20xx <21219769+gabriel20xx@users.noreply.github.com> --- CollabTableServer/src/db.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CollabTableServer/src/db.ts b/CollabTableServer/src/db.ts index 84650f4..c891139 100644 --- a/CollabTableServer/src/db.ts +++ b/CollabTableServer/src/db.ts @@ -307,14 +307,13 @@ export async function initializeDatabase() { const isConnectionError = err?.code === 'ECONNREFUSED' || err?.code === 'ENOTFOUND' || - err?.code === 'ETIMEDOUT' || - err?.message?.includes('connect'); + err?.code === 'ETIMEDOUT'; if (!isConnectionError || attempt === maxRetries) { throw err; } - const delay = retryDelayMs * attempt; + const delay = retryDelayMs * Math.pow(2, attempt - 1); console.warn( `Database connection attempt ${attempt}/${maxRetries} failed (${err.code || err.message}). Retrying in ${delay}ms...` ); From 09ae5309d42c1101f57c4100e8c0acb45fe38490 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 19 May 2026 11:23:06 +0000 Subject: [PATCH 3/3] Clamp and validate DB_CONNECT_RETRIES and DB_CONNECT_RETRY_DELAY_MS env vars Agent-Logs-Url: https://github.com/gabriel20xx/CollabTable/sessions/3d38d5f2-ce12-45a3-bcfe-9fb96f3581ce Co-authored-by: gabriel20xx <21219769+gabriel20xx@users.noreply.github.com> --- CollabTableServer/src/db.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CollabTableServer/src/db.ts b/CollabTableServer/src/db.ts index c891139..d177ef4 100644 --- a/CollabTableServer/src/db.ts +++ b/CollabTableServer/src/db.ts @@ -296,8 +296,11 @@ export const dbAdapter: DBAdapter = clientType === 'postgres' || clientType === : new SqliteAdapter(); export async function initializeDatabase() { - const maxRetries = parseInt(process.env.DB_CONNECT_RETRIES || '10', 10); - const retryDelayMs = parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS || '3000', 10); + const parsedRetries = parseInt(process.env.DB_CONNECT_RETRIES || '10', 10); + const maxRetries = Number.isNaN(parsedRetries) || parsedRetries < 1 ? 10 : parsedRetries; + + const parsedDelay = parseInt(process.env.DB_CONNECT_RETRY_DELAY_MS || '3000', 10); + const retryDelayMs = Number.isNaN(parsedDelay) || parsedDelay < 0 ? 3000 : parsedDelay; for (let attempt = 1; attempt <= maxRetries; attempt++) { try {