From 8af1951f13e3db3959c5e3b18995f2319a76b9d0 Mon Sep 17 00:00:00 2001 From: ekko <152005280+EKKOLearnAI@users.noreply.github.com> Date: Sat, 2 May 2026 17:02:33 +0800 Subject: [PATCH] fix(db): add startup delays to prevent resource race conditions (#398) * feat(chat): redesign attachments with ContentBlock format and file downloads - Redesign attachment handling using Anthropic-style ContentBlock array format with discriminated unions (text, image, file types) - Add frontend file download functionality supporting both ContentBlock and Markdown formats with authentication tokens - Fix multi-process conflict causing SQLite database resets by eliminating redundant nodemon instances - Update chat store to build ContentBlock arrays from attachments - Improve image handling with base64 conversion for upstream API Co-Authored-By: Claude Sonnet 4.6 * fix(db): add startup delays to prevent resource race conditions - Add 1 second delay after gateway manager initialization - Add 1 second delay after store initialization before session sync - Code formatting cleanup in schemas.ts These delays ensure all resources are fully initialized before proceeding to the next startup step, preventing potential race conditions and database access issues during server bootstrap. Co-Authored-By: Claude Sonnet 4.6 --------- Co-authored-by: Claude Sonnet 4.6 --- packages/server/src/db/hermes/schemas.ts | 4 ++-- packages/server/src/index.ts | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/server/src/db/hermes/schemas.ts b/packages/server/src/db/hermes/schemas.ts index af8b049..7995dab 100644 --- a/packages/server/src/db/hermes/schemas.ts +++ b/packages/server/src/db/hermes/schemas.ts @@ -543,8 +543,8 @@ export function initAllHermesTables(retryCount = 0): void { } // 3. 删除 WAL 和 SHM 文件 - try { unlinkSync(dbPath + '-wal') } catch {} - try { unlinkSync(dbPath + '-shm') } catch {} + try { unlinkSync(dbPath + '-wal') } catch { } + try { unlinkSync(dbPath + '-shm') } catch { } // 4. 重新初始化(增加重试计数) console.log('[Schema] Reinitializing database...') diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index b58d7c7..452c5e6 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -48,10 +48,12 @@ export async function bootstrap() { await initGatewayManager() console.log('[bootstrap] gateway manager initialized') - + await new Promise(resolve => setTimeout(resolve, 1000)) // Initialize all web-ui SQLite tables const { initAllStores } = await import('./db/hermes/init') + // Wait 1 second before initializing stores to ensure all resources are ready initAllStores() + await new Promise(resolve => setTimeout(resolve, 1000)) console.log('[bootstrap] all stores initialized') // Sync Hermes sessions from all profiles (only if local DB is empty)