ductor icon indicating copy to clipboard operation
ductor copied to clipboard

[Windows] Gemini CLI Provider fails with Exit Code 42 or Hang in background tasks

Open dimotsai opened this issue 9 months ago • 1 comments

🐛 Bug Description

On Windows (Host mode), executing background Cron tasks using the Gemini provider fails consistently. The failure manifests in two ways depending on the execution strategy:

  1. Exit Code 42 (FATAL_INPUT_ERROR): When passing the prompt via stdin without the -p flag.
  2. Process Hang / Timeout: When using the -p flag but not properly managing the stdin pipe (e.g., setting it to DEVNULL).

This prevents automated/scheduled tasks from running on Windows when using Gemini.

💻 Environment

  • OS: Windows 10/11
  • Ductor Version: v0.5.3
  • Provider: Google Gemini (gemini-3-flash, gemini-3-pro)
  • Gemini CLI: @google/gemini-cli (Node.js)

🔍 Root Cause Analysis

Through extensive debugging, we identified a conflict between gemini-cli's interactive check and Windows IPC behavior:

  1. The TTY Check (Exit 42):

    • By default, gemini-cli assumes interactive mode.
    • It checks process.stdin.isTTY. In a Python background subprocess (Cron), this is false.
    • If no -p (prompt) flag is provided, it detects "Non-TTY and No Arguments" -> throws FATAL_INPUT_ERROR (42).
  2. The Stdin Hang (Timeout):

    • Using -p "prompt" forces headless mode (bypassing the TTY check).
    • However, if asyncio.create_subprocess_exec sets stdin=asyncio.subprocess.DEVNULL, the Node.js process on Windows often hangs indefinitely, waiting for a proper EOF.

🛠️ Proposed Fix: "Hybrid Mode"

We verified a robust solution that satisfies all constraints on Windows and bypasses command-line length limits (32k):

  1. Pass -p "" (Empty Prompt Flag): This tricks gemini-cli into "Headless Mode," causing it to skip the isTTY check.
  2. Feed Actual Prompt via Stdin: Since we are now in headless mode, the CLI happily accepts input from stdin. This bypasses Windows 32k command-line limit.
  3. Always Open Stdin Pipe: Ensure Python opens stdin=asyncio.subprocess.PIPE and writes to it. This prevents the Node.js process from hanging.

Implementation Details (ductor_bot/cron/execution.py)

def _build_gemini_cmd(exec_config: TaskExecutionConfig, prompt: str) -> tuple[list[str], str | None] | None:
    # ... existing setup ...
    # Force non-interactive mode to bypass TTY checks
    cmd += ["-p", ""]
    return cmd, prompt

async def execute_one_shot(...):
    # Always use PIPE for stdin to ensure proper EOF for Node.js on Windows
    proc = await asyncio.create_subprocess_exec(
        *cmd,
        cwd=str(cwd),
        stdin=asyncio.subprocess.PIPE, 
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    
    # Send prompt via stdin (or empty bytes to close pipe)
    stdin_input = prompt.encode() if prompt else b""
    stdout, stderr = await proc.communicate(input=stdin_input)

✅ Verification

Validated on Windows 11 with a standalone reproduction script.

  • Strategy A (Positional): Failed (Exit 42).
  • Strategy B (Pure Stdin): Failed (Exit 42).
  • Strategy C (Hybrid -p "" + Stdin): SUCCESS (Return Code 0, correct output).

dimotsai avatar Feb 25 '26 00:02 dimotsai

Hey! Thanks for the incredibly detailed analysis and the reproduction results — that made fixing this straightforward.

v0.5.5 implements exactly the hybrid mode you proposed:

  • Cron/Webhook path: _build_gemini_cmd now passes -p "" and feeds the prompt via stdin=PIPE instead of -- prompt with DEVNULL. A new OneShotCommand dataclass carries both the command and stdin payload through the pipeline.
  • Heartbeat path (via GeminiCLI.send()): Added -p "" to _build_command() — stdin was already PIPE, only the flag was missing.
  • execute_one_shot() accepts an optional stdin_input parameter. Claude and Codex paths remain unchanged (stdin_input=None → DEVNULL).

Upgrade via ductor upgrade. Release: https://github.com/PleasePrompto/ductor/releases/tag/v0.5.5

If you run into any issues on Windows with this fix, feel free to reopen.

PleasePrompto avatar Feb 25 '26 03:02 PleasePrompto