--- id: "bug-1807" status: "fixed" status_updated: "2026-09-24" status_note: "" component: "harness" severity: "unknown" timestamp: "2026-09-14T15:50:00Z" last_seen: "2026-09-19T09:00:00Z" occurrences: 1 files: ["src/harness/test.c (all ~20 cmd_* handlers)", "src/harness/join_worker_args.c"] tags: ["confirmed", "getopt", "getopt_long", "cli-parsing", "xce_test", "harness_unit_test", "test_test_cli.c", "flagspec"] related: [] legacy: true --- # bug-1807: harness_unit_test's test_test_cli.c: 4 of 132 tests deterministically FAIL every run… ## Description harness_unit_test's test_test_cli.c: 4 of 132 tests deterministically FAIL every run -- test_migrated_commands_sample_reject_unrecognized_flag (jobkill unrecognized-flag rejection), test_qctrl_hostctrl_confirm_gate_destructiveness_determination (qctrl -c or hostctrl -c prompt missing -- WHICH one shifts between builds), test_destructive_commands_prompt_by_default_and_yes_bypasses (upgradeconfig prompt missing), test_confirm_gate_session_toggle_skips_and_restores ('set +y' restore prompt missing). Each failing assertion burns its full 5000ms clitest_wait_for_text() timeout (total suite runtime ~28-29s vs ~9s once fixed) -- not a race, a genuine miss every time. ## Environment _Not recorded (migrated from buglog.json)._ ## Reproduction _Not recorded (migrated from buglog.json)._ ## Root cause CONFIRMED via isolated pty repro (bypassing Unity/gdb entirely): every cmd_*() REPL command handler resets getopt_long() state with `optind = 1; opterr = 0;` before its own parse loop. glibc's getopt_long only re-initializes its internal static parser state (nextchar, first_nonopt/last_nonopt) when optind==0 OR on the process's very first-ever getopt_long() call (guarded by the internal __getopt_initialized flag) -- optind=1 alone is NOT sufficient once any earlier call in the same process has already run once. Since xce_test's REPL dispatches many independent cmd_*() calls (each with its OWN, differently-sized argv array) across the lifetime of one process, a command whose parse ends by consuming a short no-argument flag as its LAST token (e.g. `qctrl -q normal -a`, `hostctrl -h node01 -o`) leaves stale internal state that corrupts the NEXT unrelated command's getopt_long() call -- observed corrupting the NEXT command so badly that argv[0] (the command's own name, e.g. 'upgradeconfig' or 'jobkill') is misread as an unrecognized flag via flagspec_reject(). 100% reproducible in isolation: `qctrl -q normal -a` immediately followed by ANY other command (upgradeconfig, jobkill) corrupts that next command every time; the same session's commands run individually/first are unaffected. Explains why the specific victim test shifted between builds (bug-1807 was first seen failing on hostctrl -c, then qctrl -c after an unrelated same-file edit) -- runtime sequence-dependent, not random. ## Fix FIXED. Changed all 20 occurrences of `optind = 1;` in src/harness/test.c and the 1 occurrence in src/harness/join_worker_args.c to `optind = 0;` (the documented glibc idiom that forces getopt_long to fully reinitialize, including nextchar/first_nonopt/last_nonopt, on every independent parse). Verified: (1) the 3 isolated minimal pty repros that previously reproduced the corruption 100% of the time now pass cleanly; (2) harness_unit_test run 10x consecutively under the debug profile: 132 Tests 0 Failures every run (was 4 Failures every run pre-fix), runtime dropped from ~28-29s to ~8-9s per run, confirming the failures were full timeout burns, not near-miss races; (3) full `/xce-build harness asan` (meson test, ASAN-instrumented) also reports 5/5 OK including harness_unit_test, no sanitizer findings. ## Verification _Not recorded (migrated from buglog.json)._ ## References _Not recorded (migrated from buglog.json)._