Skip to content

fix(hooks): AfterTool hang on large output or stdin stall in Gemini CLI #242

@tndhuy

Description

@tndhuy

Description

When using context-mode with Gemini CLI (Node v24+), the AfterTool hook frequently fails with a Hook(s) failed for event AfterTool warning. This happens because the hook tries to read from stdin without a timeout, and processes potentially large tool responses (multi-MB) within the strict <20ms budget of the CLI.

Symptoms

  • Constant AfterTool failed warnings in Gemini CLI UI.
  • Hook processes hanging or being killed by the system.
  • No clear error in aftertool-debug.log because the process is terminated mid-execution.

Root Cause

  1. Stdin Hang: readStdin() lacks a timeout guard.
  2. Performance Budget: Extracting events from very large tool outputs (e.g., massive run_shell_command output) exceeds the time limit.

Proposed Solution (Applied locally)

I have applied a patch to hooks/gemini-cli/aftertool.mjs with:

  • A 10s setTimeout guard on stdin to prevent permanent hangs.
  • A size check: if tool_response > 1MB, skip event extraction to preserve CLI responsiveness.
// Stdin timeout guard
const stdinTimeout = setTimeout(() => process.exit(0), 10000);

// ... readStdin ...
clearTimeout(stdinTimeout);

// Response size guard
const responseSize = typeof input.tool_response === 'string' ? input.tool_response.length : 0;
if (responseSize > 1024 * 1024) {
  process.exit(0);
}

Environment

  • OS: Darwin (macOS)
  • Node: v24.4.1
  • Gemini CLI: Latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions