Repository navigation
fix(mcp): prevent memory leak from uncleared interval timer in MCPCir… #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||
| private cleanupTimer?: NodeJS.Timeout; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| constructor( | ||||||||||||||||||||||||||||
| private name: string, | ||||||||||||||||||||||||||||
|
|
@@ -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); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| * 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
AI
Aug 18, 2025
There was a problem hiding this comment.
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.
| * 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. |
There was a problem hiding this comment.
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.