mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 18:24:21 +01:00
fix: spawns hanging (#3192)
This commit is contained in:
@@ -145,14 +145,26 @@ export const BashTool = Tool.define("bash", {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const process = spawn(params.command, {
|
const proc = spawn(params.command, {
|
||||||
shell: true,
|
shell: true,
|
||||||
cwd: Instance.directory,
|
cwd: Instance.directory,
|
||||||
signal: ctx.abort,
|
signal: ctx.abort,
|
||||||
stdio: ["ignore", "pipe", "pipe"],
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
timeout,
|
timeout,
|
||||||
|
detached: process.platform !== "win32",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!ctx.abort.aborted) {
|
||||||
|
ctx.abort.addEventListener("abort", () => {
|
||||||
|
if (!proc.pid) return
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
proc.kill()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
process.kill(-proc.pid)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let output = ""
|
let output = ""
|
||||||
|
|
||||||
// Initialize metadata with empty output
|
// Initialize metadata with empty output
|
||||||
@@ -163,7 +175,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
process.stdout?.on("data", (chunk) => {
|
proc.stdout?.on("data", (chunk) => {
|
||||||
output += chunk.toString()
|
output += chunk.toString()
|
||||||
ctx.metadata({
|
ctx.metadata({
|
||||||
metadata: {
|
metadata: {
|
||||||
@@ -173,7 +185,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
process.stderr?.on("data", (chunk) => {
|
proc.stderr?.on("data", (chunk) => {
|
||||||
output += chunk.toString()
|
output += chunk.toString()
|
||||||
ctx.metadata({
|
ctx.metadata({
|
||||||
metadata: {
|
metadata: {
|
||||||
@@ -184,7 +196,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
process.on("close", () => {
|
proc.on("close", () => {
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -192,7 +204,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
ctx.metadata({
|
ctx.metadata({
|
||||||
metadata: {
|
metadata: {
|
||||||
output: output,
|
output: output,
|
||||||
exit: process.exitCode,
|
exit: proc.exitCode,
|
||||||
description: params.description,
|
description: params.description,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -202,7 +214,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
output += "\n\n(Output was truncated due to length limit)"
|
output += "\n\n(Output was truncated due to length limit)"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.signalCode === "SIGTERM" && params.timeout) {
|
if (proc.signalCode === "SIGTERM" && params.timeout) {
|
||||||
output += `\n\n(Command timed out after ${timeout} ms)`
|
output += `\n\n(Command timed out after ${timeout} ms)`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +222,7 @@ export const BashTool = Tool.define("bash", {
|
|||||||
title: params.command,
|
title: params.command,
|
||||||
metadata: {
|
metadata: {
|
||||||
output,
|
output,
|
||||||
exit: process.exitCode,
|
exit: proc.exitCode,
|
||||||
description: params.description,
|
description: params.description,
|
||||||
},
|
},
|
||||||
output,
|
output,
|
||||||
|
|||||||
Reference in New Issue
Block a user