Spawned processes may not be cleaned up on error in PR command #8274

Open
opened 2026-02-16 18:09:34 -05:00 by yindo · 3 comments
Owner

Originally created by @riftzen-bit on GitHub (Feb 1, 2026).

Originally assigned to: @rekram1-node on GitHub.

Summary

The PR command spawns child processes that may continue running if an error occurs, leading to resource leaks.

Location

packages/opencode/src/cli/cmd/pr.ts:97-108

Issue

const opencodeProcess = spawn("opencode", opencodeArgs, {
  cwd: workingDir,
  env: {...},
  stdio: ["inherit", "inherit", "inherit"],
})

await new Promise<void>((resolve, reject) => {
  opencodeProcess.on("exit", (code) => {
    if (code === 0) resolve()
    else reject(new Error(\`opencode exited with code \${code}\`))
  })
  opencodeProcess.on("error", reject)
})

If the Promise rejects (either from non-zero exit or spawn error), there's no cleanup code to ensure the process is terminated. The process may continue running in the background.

Impact

  • Severity: Low-Medium
  • Type: Resource Leak
  • Effect: Orphaned child processes

Suggested Fix

Add cleanup in finally block or use AbortController:

const opencodeProcess = spawn("opencode", opencodeArgs, {...})

try {
  await new Promise<void>((resolve, reject) => {
    opencodeProcess.on("exit", (code) => {
      if (code === 0) resolve()
      else reject(new Error(\`opencode exited with code \${code}\`))
    })
    opencodeProcess.on("error", reject)
  })
} finally {
  // Ensure process is terminated
  if (!opencodeProcess.killed) {
    opencodeProcess.kill()
  }
}
Originally created by @riftzen-bit on GitHub (Feb 1, 2026). Originally assigned to: @rekram1-node on GitHub. ## Summary The PR command spawns child processes that may continue running if an error occurs, leading to resource leaks. ## Location `packages/opencode/src/cli/cmd/pr.ts:97-108` ## Issue ```typescript const opencodeProcess = spawn("opencode", opencodeArgs, { cwd: workingDir, env: {...}, stdio: ["inherit", "inherit", "inherit"], }) await new Promise<void>((resolve, reject) => { opencodeProcess.on("exit", (code) => { if (code === 0) resolve() else reject(new Error(\`opencode exited with code \${code}\`)) }) opencodeProcess.on("error", reject) }) ``` If the Promise rejects (either from non-zero exit or spawn error), there's no cleanup code to ensure the process is terminated. The process may continue running in the background. ## Impact - **Severity**: Low-Medium - **Type**: Resource Leak - **Effect**: Orphaned child processes ## Suggested Fix Add cleanup in finally block or use AbortController: ```typescript const opencodeProcess = spawn("opencode", opencodeArgs, {...}) try { await new Promise<void>((resolve, reject) => { opencodeProcess.on("exit", (code) => { if (code === 0) resolve() else reject(new Error(\`opencode exited with code \${code}\`)) }) opencodeProcess.on("error", reject) }) } finally { // Ensure process is terminated if (!opencodeProcess.killed) { opencodeProcess.kill() } } ```
Author
Owner

@github-actions[bot] commented on GitHub (Feb 1, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #11527: OpenCode leaves an orphaned process when it itself is killed - related to process deparenting when OpenCode is terminated
  • #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit - general orphaned process issue when terminal closes
  • #6633: [Bug] MCP processes not terminated after session ends - processes not cleaned up on session termination
  • #3057: Provide a way to stop all running scripts when OpenCode is interrupted - user-facing issue about child processes not stopping with Ctrl+C

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Feb 1, 2026): This issue might be a duplicate of existing issues. Please check: - #11527: OpenCode leaves an orphaned process when it itself is killed - related to process deparenting when OpenCode is terminated - #10563: Memory Leak: Orphaned processes when terminal closes without explicit exit - general orphaned process issue when terminal closes - #6633: [Bug] MCP processes not terminated after session ends - processes not cleaned up on session termination - #3057: Provide a way to stop all running scripts when OpenCode is interrupted - user-facing issue about child processes not stopping with Ctrl+C Feel free to ignore if none of these address your specific case.
Author
Owner

@mohamedbouddi7777-dev commented on GitHub (Feb 1, 2026):

Free the world

في الاثنين، ٢ فبراير ٢٠٢٦، ٠٣:٠٣ github-actions[bot] <
@.***> كتب:

github-actions[bot] left a comment (anomalyco/opencode#11704)
https://github.com/anomalyco/opencode/issues/11704#issuecomment-3832537748

This issue might be a duplicate of existing issues. Please check:

Feel free to ignore if none of these address your specific case.


Reply to this email directly, view it on GitHub
https://github.com/anomalyco/opencode/issues/11704#issuecomment-3832537748,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/B2DFN3T4UCY27JDCKL3GLGT4J2V7VAVCNFSM6AAAAACTUA4EIGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQMZSGUZTONZUHA
.
You are receiving this because you are subscribed to this thread.Message
ID: @.***>

@mohamedbouddi7777-dev commented on GitHub (Feb 1, 2026): Free the world في الاثنين، ٢ فبراير ٢٠٢٦، ٠٣:٠٣ github-actions[bot] < ***@***.***> كتب: > *github-actions[bot]* left a comment (anomalyco/opencode#11704) > <https://github.com/anomalyco/opencode/issues/11704#issuecomment-3832537748> > > This issue might be a duplicate of existing issues. Please check: > > - #11527 <https://github.com/anomalyco/opencode/issues/11527>: > OpenCode leaves an orphaned process when it itself is killed - related to > process deparenting when OpenCode is terminated > - #10563 <https://github.com/anomalyco/opencode/issues/10563>: Memory > Leak: Orphaned processes when terminal closes without explicit exit - > general orphaned process issue when terminal closes > - #6633 <https://github.com/anomalyco/opencode/issues/6633>: [Bug] MCP > processes not terminated after session ends - processes not cleaned up on > session termination > - #3057 <https://github.com/anomalyco/opencode/issues/3057>: Provide a > way to stop all running scripts when OpenCode is interrupted - user-facing > issue about child processes not stopping with Ctrl+C > > Feel free to ignore if none of these address your specific case. > > — > Reply to this email directly, view it on GitHub > <https://github.com/anomalyco/opencode/issues/11704#issuecomment-3832537748>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/B2DFN3T4UCY27JDCKL3GLGT4J2V7VAVCNFSM6AAAAACTUA4EIGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTQMZSGUZTONZUHA> > . > You are receiving this because you are subscribed to this thread.Message > ID: ***@***.***> >
Author
Owner

@riftzen-bit commented on GitHub (Feb 1, 2026):

I forgot that.

@riftzen-bit commented on GitHub (Feb 1, 2026): I forgot that.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8274