Skip to content

Commit d4bf70b

Browse files
Honajiwenshang
andauthored
fix(bash): memory leak - release parsed syntax trees (#24861)
Co-authored-by: jiwenshang <jiwenshang@xiaohongshu.com>
1 parent ae8904c commit d4bf70b

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

packages/opencode/src/tool/bash.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function tail(text: string, maxLines: number, maxBytes: number) {
252252
const parse = Effect.fn("BashTool.parse")(function* (command: string, ps: boolean) {
253253
const tree = yield* Effect.promise(() => parser().then((p) => (ps ? p.ps : p.bash).parse(command)))
254254
if (!tree) throw new Error("Failed to parse command")
255-
return tree.rootNode
255+
return tree
256256
})
257257

258258
const ask = Effect.fn("BashTool.ask")(function* (ctx: Tool.Context, scan: Scan) {
@@ -596,10 +596,17 @@ export const BashTool = Tool.define(
596596
}
597597
const timeout = params.timeout ?? DEFAULT_TIMEOUT
598598
const ps = Shell.ps(shell)
599-
const root = yield* parse(params.command, ps)
600-
const scan = yield* collect(root, cwd, ps, shell)
601-
if (!Instance.containsPath(cwd)) scan.dirs.add(cwd)
602-
yield* ask(ctx, scan)
599+
yield* Effect.scoped(
600+
Effect.gen(function* () {
601+
const tree = yield* Effect.acquireRelease(
602+
parse(params.command, ps),
603+
(tree) => Effect.sync(() => tree.delete()),
604+
)
605+
const scan = yield* collect(tree.rootNode, cwd, ps, shell)
606+
if (!Instance.containsPath(cwd)) scan.dirs.add(cwd)
607+
yield* ask(ctx, scan)
608+
}),
609+
)
603610

604611
return yield* run(
605612
{

0 commit comments

Comments
 (0)