Skip to content

fix(opencode): resolve heap unlimited + orphan processes on Linux#24297

Open
herjarsa wants to merge 1 commit intoanomalyco:devfrom
herjarsa:fix/linux-heap-orphan-processes
Open

fix(opencode): resolve heap unlimited + orphan processes on Linux#24297
herjarsa wants to merge 1 commit intoanomalyco:devfrom
herjarsa:fix/linux-heap-orphan-processes

Conversation

@herjarsa
Copy link
Copy Markdown

Issue for this PR

Closes #15348

Type of change

  • Bug fix

What does this PR do?

Fixes critical memory leak and orphan process issues on Linux:

  1. Orphan processes: Added SIGTERM handler alongside existing SIGHUP handler in exit.tsx and hread.ts to ensure graceful shutdown when terminal closes or process receives termination signal.

  2. Heap growth (37+ GB): Added Effect.ensuring to abort AbortController in prompt.ts execRead function. Previously, controllers were only aborted on interrupt, causing accumulation during normal operation.

  3. Unbounded cache: Replaced unbounded Map with LRU cache (max 20 entries) in instance.ts to prevent unbounded growth of instance contexts.

How did you verify your code works?

  • Ran �un typecheck in packages/opencode - passes with no errors
  • Verified signal handlers are properly registered and cleaned up
  • Confirmed AbortController is aborted in both interrupt and success paths
  • LRU cache bounds instance retention to 20 entries max

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

- Add SIGTERM handler in exit.tsx alongside existing SIGHUP handler
- Add SIGTERM handler in thread.ts to gracefully stop worker
- Add Effect.ensuring to abort AbortController in prompt.ts execRead
- Replace unbounded Map with LRU cache in instance.ts (max 20 entries)

Fixes anomalyco#15348

This addresses the critical issue where:
1. Processes spawned by opencode on Linux become orphaned when terminal closes
2. AbortController instances accumulate in session prompts causing heap growth
3. Instance cache grows without bound holding references to contexts

Closes anomalyco#15348
@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search results, I found potential related PRs:

Related PRs:

  1. PR fix(cli): handle SIGHUP to prevent orphaned processes on terminal close #12718 - fix(cli): handle SIGHUP to prevent orphaned processes on terminal close

    • Related to the SIGTERM/SIGHUP handler work for graceful shutdown
  2. PR fix: resolve multiple memory leaks causing unbounded growth #16695 - fix: resolve multiple memory leaks causing unbounded growth

    • Similar scope: addresses memory leaks with unbounded growth
  3. PR fix(opencode): memory leaks in session handling #10392 - fix(opencode): memory leaks in session handling

    • Memory leak related work
  4. PR feat(util): Add bounded LRU cache utility #9141 - feat(util): Add bounded LRU cache utility

    • Related to the LRU cache implementation mentioned in this PR

These PRs address similar issues (memory management, process cleanup, signal handling), but since PR #24297 (the current PR) is a comprehensive fix specifically addressing the orphan processes + heap growth on Linux, you may want to verify these related PRs are either closed/merged or confirm there's no overlapping work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Critical: Unbounded heap growth (37+ GB RSS) + orphaned processes on Linux

1 participant