Skip to content

fix: harden agent process lifecycle handling#16

Merged
axeldelafosse merged 2 commits intomainfrom
fix/process-group-lifecycle
Feb 25, 2026
Merged

fix: harden agent process lifecycle handling#16
axeldelafosse merged 2 commits intomainfrom
fix/process-group-lifecycle

Conversation

@axeldelafosse
Copy link
Copy Markdown
Owner

Summary

  • run codex app-server and legacy runner child processes as detached groups on non-Windows platforms
  • add shared process-group kill logic that prefers process.kill(-pid, signal) and safely falls back to child.kill(...)
  • stop caching app-server thread IDs so thread/start is called per turn
  • make Claude SDK websocket input a raw passthrough and restart the Claude SDK process after each turn to ensure a fresh session ID
  • add test coverage to verify interrupting app-server targets the detached process group when a pid is available

Why

  • child processes could leave descendants running when only the direct child received a signal
  • stale thread/session state could bleed across turns and make lifecycle behavior harder to reason about

Testing

  • bun run check
  • bun test tests/loop/codex-app-server.test.ts

- run app-server and legacy agent processes detached on non-Windows platforms
- kill process groups when interrupting or shutting down, with child.kill fallback
- remove cached thread id in app-server mode so each turn starts a fresh thread
- forward frontend websocket frames directly to Claude SDK and restart session after each turn
- add test coverage for detached process-group interrupt behavior
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @axeldelafosse, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness and reliability of child process management within the agent system. By ensuring that child processes are properly detached and terminated, it prevents issues like orphaned processes and stale state bleeding across turns. The changes streamline the lifecycle of both the Codex app-server and the Claude SDK, leading to more predictable and stable agent behavior.

Highlights

  • Process Lifecycle Management: Child processes for the Codex app-server and legacy runner are now run as detached groups on non-Windows platforms to prevent orphaned processes.
  • Shared Process Kill Logic: A new shared utility function killChildProcess has been introduced, which prioritizes killing process groups using process.kill(-pid, signal) and gracefully falls back to child.kill(...) if group killing is unavailable.
  • Claude SDK Integration: The Claude SDK websocket input has been changed to a raw passthrough, and the Claude SDK process is now restarted after each turn to ensure a fresh session ID and prevent stale state.
  • App-Server Thread Management: Caching of app-server thread IDs has been removed, ensuring that thread/start is called for every turn to maintain fresh session state.
  • Test Coverage: New test coverage has been added to verify that interrupting the app-server correctly targets the detached process group when a PID is available.
Changelog
  • src/loop/claude-sdk-server.ts
    • Claude SDK websocket input was changed to a raw passthrough.
    • The Claude SDK process is now restarted after each turn to ensure a fresh session ID.
  • src/loop/codex-app-server.ts
    • Introduced a KillSignal type to include SIGKILL.
    • Added a DETACH_CHILD_PROCESS constant to control process detachment.
    • Implemented a shared killChildProcess utility that attempts to kill process groups on non-Windows platforms.
    • Removed caching of threadId to ensure thread/start is called per turn.
    • Configured the app-server child process to run as a detached group on non-Windows platforms.
    • Updated all child process termination calls to use the new killChildProcess utility.
  • src/loop/runner.ts
    • Introduced a KillSignal type.
    • Implemented a killChildProcess utility for consistent child process termination.
    • Added killChildrenHard to ensure all child processes are terminated on exit.
    • Configured the legacy agent runner child process to run as a detached group on non-Windows platforms.
    • Updated killChildren to use the new killChildProcess utility.
  • tests/loop/codex-app-server.test.ts
    • Updated TestProcess interface to include killSignals and pid.
    • Modified installSpawn to record kill signals and assign a mock PID to spawned processes.
    • Added a new test to verify that interruptAppServer correctly targets detached process groups using process.kill(-pid, signal).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively hardens the agent process lifecycle management. The introduction of detached process groups on non-Windows platforms and the logic to kill process groups are significant improvements for ensuring that child processes and their descendants are properly terminated. The changes to restart the Claude SDK process after each turn and to stop caching app-server thread IDs are good measures to prevent stale state and improve the reliability of the system. The added test coverage for the new process group killing logic is also a valuable addition.

I have one suggestion regarding code duplication that would further improve the maintainability of the new process killing logic.

Comment thread src/loop/runner.ts Outdated
@axeldelafosse axeldelafosse merged commit ae74624 into main Feb 25, 2026
2 checks passed
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.

1 participant