{"record":{"id":"38c38eefef6a0650","repo":"ruvnet/ruflo","slug":"connection-pool-already-initialized","errorCode":null,"errorMessage":"Connection pool already initialized","messagePattern":"Connection pool already initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/plugins/src/integrations/ruvector/ruvector-bridge.ts","lineNumber":191,"sourceCode":"\n  constructor(config: RuVectorConfig) {\n    super();\n    this.config = config;\n    this.retryConfig = config.retry ?? {\n      maxAttempts: 3,\n      initialDelayMs: 1000,\n      maxDelayMs: 30000,\n      backoffMultiplier: 2,\n      jitter: true,\n    };\n  }\n\n  /**\n   * Initialize the connection pool.\n   */\n  async initialize(): Promise<ConnectionResult> {\n    if (this.pool) {\n      throw new Error('Connection pool already initialized');\n    }\n\n    const poolConfig = this.buildPoolConfig();\n\n    try {\n      // Dynamically import pg to avoid bundling issues\n      const pg = await this.loadPg();\n      this.pool = new pg.Pool(poolConfig);\n\n      // Set up event handlers\n      this.pool.on('connect', () => {\n        this.connectionId++;\n        this.emit('connection:open', {\n          connectionId: `conn-${this.connectionId}`,\n          host: this.config.host,\n          port: this.config.port,\n          database: this.config.database,\n        });","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/plugins/src/integrations/ruvector/ruvector-bridge.ts#L173-L209","documentation":"Thrown by the connection pool manager's initialize() when this.pool is already set. The manager supports exactly one pg.Pool per instance; a second initialize() would leak the existing pool and duplicate event handlers, so it is rejected.","triggerScenarios":"Calling initialize() in both application startup and a plugin's onMount; retry middleware re-invoking initialize() after a health-check flap on the same instance; module imported twice (ESM/CJS dual packaging) so two code paths both initialize the singleton.","commonSituations":"Framework hot-reload (nest dev mode, next dev) re-running init code against a cached module; refactoring that moved initialize() into a helper called from two places; tests sharing a module-level manager across cases.","solutions":["Make initialize() idempotent at the call site: skip when the manager already reports a live pool / isConnected","Centralize initialization in one bootstrap function and pass the ready manager to consumers","Call shutdown()/close() before intentionally re-initializing (e.g. config reload)"],"exampleFix":"// before\nawait bridge.initialize();\nawait bridge.initialize(); // throws\n\n// after\nawait bridge.initialize();\nif (!isConnected(bridge)) {\n  await bridge.initialize();\n}","handlingStrategy":"validation","validationCode":"// expose/simulate a connected check before init\nasync function ensureInitialized(bridge: RuVectorBridge): Promise<void> {\n  if (isConnected(bridge)) return; // pool exists\n  await bridge.initialize();\n}","typeGuard":"const isBridgeReady = (b: { isConnected?: boolean }): boolean =>\n  b.isConnected === true;","tryCatchPattern":"try {\n  await bridge.initialize();\n} catch (err) {\n  if (err instanceof Error && err.message === 'Connection pool already initialized') {\n    return; // already up — treat as success\n  }\n  throw err;\n}","preventionTips":["Initialize the bridge exactly once in a bootstrap function and share the ready instance","Treat 'already initialized' as idempotent success in wrappers","Call shutdown/close before re-initializing on config reload, and reset the cached instance on hot-reload"],"tags":["database","connection-pool","lifecycle","state-validation"],"backgroundTag":"double-initialization","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}