Skip to content

[BUG] Memory Leak: Stale 'kiro-cli-chat' processes orphaned after session termination #269

@Joseph19820124

Description

@Joseph19820124

Problem Description

When using kiro-cli as an agent, openab suffers from a significant memory leak. Each session consumes approximately 300MB - 400MB of RAM. After ~10 sessions, system memory usage spikes to over 3GB, potentially leading to OOM (Out-of-Memory) crashes or heavy swap thrashing.

Root Cause Analysis

The issue stems from the hierarchical process structure:

  1. openab (Parent) spawns kiro-cli (Child).
  2. kiro-cli spawns kiro-cli-chat (Grandchild/Core Engine).

Currently, openab uses tokio::process::Command with .kill_on_drop(true) in src/acp/connection.rs. However, in Linux, when the direct child (kiro-cli) is terminated, its own child (kiro-cli-chat) is not automatically killed. Instead, it becomes an orphan process (PPID 1) and continues to occupy memory.

Evidence

Below is the full ps aux output showing the stale processes. Note the high RSS values for kiro-cli-chat compared to the kiro-cli wrappers:

ubuntu    673360  0.0 10.6 3393344 397720 ?      Sl   Apr12   0:01 kiro-cli-chat acp --trust-all-tools
ubuntu    581161  0.0  8.3 3395728 313496 ?      Sl   Apr12   0:03 kiro-cli-chat acp --trust-all-tools
ubuntu    625730  0.0  8.1 3395760 306692 ?      Sl   Apr12   0:06 kiro-cli-chat acp --trust-all-tools
ubuntu    459820  0.0  7.9 3399972 297508 ?      Sl   Apr12   0:02 kiro-cli-chat acp --trust-all-tools
ubuntu    633382  0.0  7.7 3391296 289068 ?      Sl   Apr12   0:03 kiro-cli-chat acp --trust-all-tools
ubuntu    872305  0.0  7.4 3397604 280468 ?      Sl   08:48   0:01 kiro-cli-chat acp --trust-all-tools
ubuntu    724688  0.0  7.4 3391656 279204 ?      Sl   00:43   0:01 kiro-cli-chat acp --trust-all-tools
ubuntu    872764  0.0  6.4 3391292 241312 ?      Sl   08:50   0:01 kiro-cli-chat acp --trust-all-tools
ubuntu    913618  0.0  6.2 4440184 235540 ?      Sl   11:00   0:03 kiro-cli-chat acp --trust-all-tools
ubuntu    907784  0.0  6.2 3391292 232712 ?      Sl   10:39   0:06 kiro-cli-chat acp --trust-all-tools
ubuntu    581153  0.0  0.7 1638124 29800 ?       Sl   Apr12   0:00 kiro-cli acp --trust-all-tools
ubuntu    724679  0.0  0.7 1638128 29576 ?       Sl   00:43   0:00 kiro-cli acp --trust-all-tools
ubuntu    625715  0.0  0.7 1638124 27644 ?       Sl   Apr12   0:00 kiro-cli acp --trust-all-tools
ubuntu    673353  0.0  0.6 1570536 25380 ?       Sl   Apr12   0:00 kiro-cli acp --trust-all-tools
ubuntu    633375  0.0  0.6 1570536 25280 ?       Sl   Apr12   0:00 kiro-cli acp --trust-all-tools
ubuntu    872298  0.0  0.6 1570540 25024 ?       Sl   08:48   0:00 kiro-cli acp --trust-all-tools
ubuntu    940470  0.4  0.5 2326384 20528 pts/0   Sl+  12:28   0:00 /home/ubuntu/.local/bin/kiro-cli-chat chat
ubuntu    872757  0.0  0.3 1570528 14984 ?       Sl   08:50   0:00 kiro-cli acp --trust-all-tools
ubuntu    907774  0.0  0.2 1638128 10880 ?       Sl   10:39   0:00 kiro-cli acp --trust-all-tools
ubuntu    913609  0.0  0.1 1570532 6272 ?        Sl   11:00   0:00 kiro-cli acp --trust-all-tools
ubuntu    940440  0.1  0.0 1841064 2740 pts/0    Sl+  12:28   0:00 kiro-cli chat

Total RSS observed: ~3033 MB.

Proposed Solution

Use Process Groups (setsid) to ensure that the entire tree of the agent is killed when a session ends.

  • In Rust, configure the command to use a new process group: command.process_group(0).
  • When terminating the connection, send the signal to the process group (e.g., kill(-PGID)) instead of just the PID.

Workaround

Manually kill the stale processes:

pkill -f "kiro-cli.*acp"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions