[Windows] Gemini CLI Provider fails with Exit Code 42 or Hang in background tasks
🐛 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:
- Exit Code 42 (
FATAL_INPUT_ERROR): When passing the prompt viastdinwithout the-pflag. - Process Hang / Timeout: When using the
-pflag but not properly managing thestdinpipe (e.g., setting it toDEVNULL).
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:
-
The TTY Check (Exit 42):
- By default,
gemini-cliassumes interactive mode. - It checks
process.stdin.isTTY. In a Python background subprocess (Cron), this isfalse. - If no
-p(prompt) flag is provided, it detects "Non-TTY and No Arguments" -> throwsFATAL_INPUT_ERROR(42).
- By default,
-
The Stdin Hang (Timeout):
- Using
-p "prompt"forces headless mode (bypassing the TTY check). - However, if
asyncio.create_subprocess_execsetsstdin=asyncio.subprocess.DEVNULL, the Node.js process on Windows often hangs indefinitely, waiting for a proper EOF.
- Using
🛠️ Proposed Fix: "Hybrid Mode"
We verified a robust solution that satisfies all constraints on Windows and bypasses command-line length limits (32k):
- Pass
-p ""(Empty Prompt Flag): This tricksgemini-cliinto "Headless Mode," causing it to skip theisTTYcheck. - 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. - Always Open Stdin Pipe: Ensure Python opens
stdin=asyncio.subprocess.PIPEand 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).
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_cmdnow passes-p ""and feeds the prompt viastdin=PIPEinstead of-- promptwithDEVNULL. A newOneShotCommanddataclass carries both the command and stdin payload through the pipeline. - Heartbeat path (via
GeminiCLI.send()): Added-p ""to_build_command()— stdin was alreadyPIPE, only the flag was missing. execute_one_shot()accepts an optionalstdin_inputparameter. 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.