Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions src/lib/mcp/mcpCircuitBreaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export class MCPCircuitBreaker extends EventEmitter {
private lastFailureTime = 0;
private halfOpenCalls = 0;
private lastStateChange = new Date();
// Store the cleanup timer reference for proper cleanup

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a JSDoc comment to document the purpose of the cleanupTimer property, explaining that it holds the reference to the interval timer for proper cleanup.

Suggested change
// Store the cleanup timer reference for proper cleanup
/**
* Holds the reference to the interval timer used for periodic cleanup of old call records.
* This allows proper cleanup of the timer when the circuit breaker is disposed.
*/

Copilot uses AI. Check for mistakes.
private cleanupTimer?: NodeJS.Timeout;

constructor(
private name: string,
Expand All @@ -142,8 +144,8 @@ export class MCPCircuitBreaker extends EventEmitter {
statisticsWindowSize: config.statisticsWindowSize ?? 300000, // 5 minutes
};

// Clean up old call records periodically
setInterval(() => this.cleanupCallHistory(), 60000);
// Clean up old call records periodically - now storing the timer reference
this.cleanupTimer = setInterval(() => this.cleanupCallHistory(), 60000);
}

/**
Expand Down Expand Up @@ -423,6 +425,28 @@ export class MCPCircuitBreaker extends EventEmitter {
isHalfOpen(): boolean {
return this.state === "half-open";
}

/**
* Destroy the circuit breaker and clean up resources
* This method should be called when the circuit breaker is no longer needed
* to prevent memory leaks from the cleanup timer

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destroy() method should have a JSDoc comment explaining when and why it should be called, its side effects (clearing timers, listeners, and memory), and that the circuit breaker becomes unusable after calling this method.

Suggested change
* to prevent memory leaks from the cleanup timer
* Destroys the circuit breaker and cleans up all associated resources.
*
* When and why to call:
* - Call this method when the circuit breaker is no longer needed, such as during application shutdown or when the breaker will not be used again.
*
* Side effects:
* - Clears the internal cleanup timer to prevent memory leaks.
* - Removes all event listeners attached to this circuit breaker instance.
* - Clears the call history to free up memory.
*
* After calling this method:
* - The circuit breaker instance becomes unusable and should not be used for further operations.

Copilot uses AI. Check for mistakes.
*/
destroy(): void {
// Clear the interval timer to prevent memory leaks
if (this.cleanupTimer) {
clearInterval(this.cleanupTimer);
this.cleanupTimer = undefined;
mcpLogger.debug(`[CircuitBreaker:${this.name}] Cleanup timer cleared`);
}

// Clear any remaining event listeners
this.removeAllListeners();

// Clear call history to free memory
this.callHistory = [];

mcpLogger.debug(`[CircuitBreaker:${this.name}] Destroyed and cleaned up`);
}
}

/**
Expand Down Expand Up @@ -451,16 +475,21 @@ export class CircuitBreakerManager {
}

/**
* Remove a circuit breaker
* Remove a circuit breaker and clean up its resources
*/
removeBreaker(name: string): boolean {
const removed = this.breakers.delete(name);
if (removed) {
const breaker = this.breakers.get(name);
if (breaker) {
// Destroy the breaker to clean up its timer and resources
breaker.destroy();
this.breakers.delete(name);

mcpLogger.debug(
`[CircuitBreakerManager] Removed circuit breaker: ${name}`,
`[CircuitBreakerManager] Removed and cleaned up circuit breaker: ${name}`,
);
return true;
}
return removed;
return false;
}

/**
Expand Down Expand Up @@ -534,6 +563,19 @@ export class CircuitBreakerManager {
unhealthyBreakers,
};
}

/**
* Destroy all circuit breakers and clean up their resources
* This should be called during application shutdown to prevent memory leaks

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destroyAll() method should have a JSDoc comment documenting its intended use during application shutdown and that it renders all managed circuit breakers unusable.

Suggested change
* This should be called during application shutdown to prevent memory leaks
* Destroys all managed circuit breakers and cleans up their resources.
* Intended to be called during application shutdown to prevent memory leaks.
* After calling this method, all managed circuit breakers become unusable.

Copilot uses AI. Check for mistakes.
*/
destroyAll(): void {
for (const breaker of this.breakers.values()) {
breaker.destroy();
}
this.breakers.clear();

mcpLogger.info("[CircuitBreakerManager] Destroyed all circuit breakers");
}
}

// Global circuit breaker manager instance
Expand Down