Skip to content

Commit 0fa5e34

Browse files
committed
fix: auto patch for api_provider_clients
1 parent c673569 commit 0fa5e34

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/api/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export interface ApiHandler {
5656
* @returns A promise resolving to the token count
5757
*/
5858
countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number>
59+
60+
/**
61+
* Optional method to dispose of any resources held by the API handler.
62+
*/
63+
dispose?: () => void
5964
}
6065

6166
export function buildApiHandler(configuration: ProviderSettings): ApiHandler {

src/api/providers/base-provider.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { countTokens } from "../../utils/countTokens"
1010
* Base class for API providers that implements common functionality.
1111
*/
1212
export abstract class BaseProvider implements ApiHandler {
13+
protected client: any // Added protected client field
14+
1315
abstract createMessage(
1416
systemPrompt: string,
1517
messages: Anthropic.Messages.MessageParam[],
@@ -32,4 +34,23 @@ export abstract class BaseProvider implements ApiHandler {
3234

3335
return countTokens(content, { useWorker: true })
3436
}
37+
38+
/**
39+
* Disposes of any resources held by the provider.
40+
* Attempts common disposal methods on the client if it exists.
41+
*/
42+
public dispose(): void {
43+
if (this.client) {
44+
// Try common disposal methods that SDKs might have
45+
if (typeof (this.client as any).close === "function") {
46+
;(this.client as any).close()
47+
} else if (typeof (this.client as any).destroy === "function") {
48+
;(this.client as any).destroy()
49+
} else if (typeof (this.client as any).dispose === "function") {
50+
;(this.client as any).dispose()
51+
}
52+
// Clear the reference
53+
this.client = undefined
54+
}
55+
}
3556
}

src/core/task/Task.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ export class Task extends EventEmitter<ClineEvents> {
10011001
this.browserSession.closeBrowser()
10021002
this.rooIgnoreController?.dispose()
10031003
this.fileContextTracker.dispose()
1004+
this.api?.dispose?.() // Added this line
10041005

10051006
// If we're not streaming then `abortStream` (which reverts the diff
10061007
// view changes) won't be called, so we need to revert the changes here.
@@ -1012,6 +1013,25 @@ export class Task extends EventEmitter<ClineEvents> {
10121013
await this.saveClineMessages()
10131014
}
10141015

1016+
// Added new dispose method as per leak report suggestion
1017+
public dispose(): void {
1018+
console.log(`[subtasks] disposing task ${this.taskId}.${this.instanceId}`)
1019+
this.abortTask(true) // Call abortTask to ensure all resources are released
1020+
1021+
// Explicitly call dispose on the api handler again, in case abortTask didn't catch it
1022+
// or if dispose is called directly without abortTask.
1023+
this.api?.dispose?.()
1024+
1025+
// Clear any other task-specific resources if they weren't handled by abortTask
1026+
if (this.pauseInterval) {
1027+
clearInterval(this.pauseInterval)
1028+
this.pauseInterval = undefined
1029+
}
1030+
// Ensure other disposables are handled if not already by abortTask
1031+
// this.rooIgnoreController?.dispose(); // Already called in abortTask
1032+
// this.fileContextTracker.dispose(); // Already called in abortTask
1033+
}
1034+
10151035
// Used when a sub-task is launched and the parent task is waiting for it to
10161036
// finish.
10171037
// TBD: The 1s should be added to the settings, also should add a timeout to

0 commit comments

Comments
 (0)