Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(mcp): reserve new connections before async acquisition yields

Rudy Celekli66436ccf0c92
diff --git a/v3/@claude-flow/mcp/__tests__/connection-pool-reservation.test.ts b/v3/@claude-flow/mcp/__tests__/connection-pool-reservation.test.ts
new file mode 100644
index 0000000000..14101b0f6f
--- /dev/null
+++ b/v3/@claude-flow/mcp/__tests__/connection-pool-reservation.test.ts
@@ -0,0 +1,26 @@
+import { describe, it, expect, vi } from 'vitest';
+import { ConnectionPool as MCPPool } from '../src/connection-pool.js';
+import { ConnectionPool as SharedPool } from '../../shared/src/mcp/connection-pool.js';
+const logger = { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() };
+describe.each([
+ ['mcp', MCPPool],
+ ['shared', SharedPool],
+] as const)('%s connection reservation', (_, Pool) => {
+ it('returns distinct leases to simultaneous demand creation', async () => {
+ const pool = new Pool(
+ { minConnections: 0, maxConnections: 2, evictionRunInterval: 60000 },
+ logger,
+ );
+ try {
+ const a = pool.acquire();
+ const b = pool.acquire();
+ const [first, second] = await Promise.all([a, b]);
+ expect(first.id).not.toBe(second.id);
+ expect(pool.getStats().busyConnections).toBe(2);
+ pool.release(first);
+ pool.release(second);
+ } finally {
+ await pool.clear();
+ }
+ });
+});
diff --git a/v3/@claude-flow/mcp/src/connection-pool.ts b/v3/@claude-flow/mcp/src/connection-pool.ts
index e3216d1ba8..496515e641 100644
--- a/v3/@claude-flow/mcp/src/connection-pool.ts
+++ b/v3/@claude-flow/mcp/src/connection-pool.ts
@@ -104,9 +104,11 @@ export class ConnectionPool extends EventEmitter implements IConnectionPool {
});
}
- private async createConnection(): Promise<ManagedConnection> {
+ private async createConnection(reserved = false): Promise<ManagedConnection> {
const id = `conn-${++this.connectionCounter}-${Date.now()}`;
const connection = new ManagedConnection(id, this.transportType);
+ // Reserve before exposing it in the map or yielding to another acquirer.
+ if (reserved) connection.state = 'busy';
this.connections.set(id, connection);
this.stats.totalCreated++;
@@ -138,7 +140,7 @@ export class ConnectionPool extends EventEmitter implements IConnectionPool {
}
if (this.connections.size < this.config.maxConnections) {
- const connection = await this.createConnection();
+ const connection = await this.createConnection(true);
connection.acquire();
this.stats.totalAcquired++;
this.recordAcquireTime(startTime);
diff --git a/v3/@claude-flow/shared/src/mcp/connection-pool.ts b/v3/@claude-flow/shared/src/mcp/connection-pool.ts
index 1d64e412be..cb0ed0e984 100644
--- a/v3/@claude-flow/shared/src/mcp/connection-pool.ts
+++ b/v3/@claude-flow/shared/src/mcp/connection-pool.ts
@@ -146,9 +146,11 @@ export class ConnectionPool extends EventEmitter implements IConnectionPool {
/**
* Create a new connection
*/
- private async createConnection(): Promise<ManagedConnection> {
+ private async createConnection(reserved = false): Promise<ManagedConnection> {
const id = `conn-${++this.connectionCounter}-${Date.now()}`;
const connection = new ManagedConnection(id, this.transportType);
+ // Reserve before exposing it in the map or yielding to another acquirer.
+ if (reserved) connection.state = 'busy';
this.connections.set(id, connection);
this.stats.totalCreated++;
@@ -185,7 +187,7 @@ export class ConnectionPool extends EventEmitter implements IConnectionPool {
// Create new connection if under limit
if (this.connections.size < this.config.maxConnections) {
- const connection = await this.createConnection();
+ const connection = await this.createConnection(true);
connection.acquire();
this.stats.totalAcquired++;
this.recordAcquireTime(startTime);