Skip to content

Answer a run once its tree has ended and read a stopped child as running - #739

Merged
yansfil merged 2 commits into
mainfrom
fix/flaky-729-retirement
Oct 7, 2026
Merged

yansfil merged 2 commits into
mainfrom
fix/flaky-729-retirement

Conversation

@yansfil

@yansfil yansfil commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Closes #729
Related: #728

Summary

  • 실패: hide-kit retirement_without_a_herdr_server_finishes_when_no_plugin_is_registered가 은퇴를 Failed로 받았다.
    • 이유는 "remove login agent failed: login agent command failed: TimedOut"이었다(macOS 로컬, 부하 40-67).
  • 원인은 부하가 아니라 같은 프로세스의 동시 spawn이다. macOS에서 Rust std는 pipe를 pipe()로 만든 뒤 두 끝에 FD_CLOEXEC를 따로 건다.
    • 그 사이 다른 스레드가 posix_spawn한 자식은 두 끝을 물려받는다. Linux는 pipe2(O_CLOEXEC)라 이 틈이 없다.
    • 다른 test의 오래 사는 자식(lock holder perl ... sleep 60, test binary re-exec)이 launchctl 대역 run의 write end를 쥐었다.
    • 대역은 곧바로 exit 113 했지만 run_to_end가 EOF를 5 s deadline까지 기다려 TimedOut이 됐다. 부하는 그 틈에서 스레드가 선점될 확률을 키울 뿐이다.
  • 제품도 같은 이유로 실패할 수 있었다. hided는 다른 스레드에서 오래 사는 자식(label worker의 claude -p 등)을 띄운다.
    • 그래서 user_agents(launchctl), ssh hosts, codex daemon 명령, 로그인 셸 PATH 읽기의 run_to_end가 멀쩡한 답을 두고 TimedOut을 낼 수 있었다.
  • 수정(hide-platform/src/process.rs): 리더 exit를 보면 트리를 끝내고, 트리에 살아 있는 것이 없음을 확인한 뒤 두 pipe를 빈 상태(EAGAIN)까지 읽고 답한다.
    • 모든 write end가 닫히기를 기다리지 않는다. 출력 상한(64 KiB)과 deadline/stop은 그대로다. capture_until도 같은 규칙으로 맞췄다.
    • 트리 종료 확인: Unix는 리더 group의 구성원을 나열해 is_alive로 본다. Windows는 job의 active process 수다.
    • group에 signal 0을 쓰지 않는다. macOS는 좀비만 남았을 때와 kill된 구성원이 아직 종료 중일 때 모두 EPERM을 답한다(이 PR을 만들다 실측).
  • 같은 함수의 두 번째 결함(#728에서 본 것): macOS의 waitid(WEXITED|WNOHANG|WNOWAIT)는 멈춘(SIGSTOP) child에도 답한다.
    • run이 그것을 exit로 읽고 reap할 것이 없어 곧바로 "observed exit could not be reaped"로 실패했고, ssh hosts는 이를 SshFailed로 보고했다.
    • 이제 exit, kill, core dump만 끝으로 센다. 멈춘 child는 deadline까지 기다렸다가 끝낸다.

Review

  • 판단해 주세요: 계약 테스트 an_escaped_pipe_holder_cannot_extend_the_run_deadline의 기대를 TimedOut에서 리더의 답(code 0, 출력 전부)으로 바꿨다.
    • 완화가 아니다. 리더는 정상 종료했고 출력은 이미 pipe에 있으니, TimedOut은 사실과 다른 답이었다.
    • 새 이름은 a_pipe_holder_outside_the_tree_does_not_hold_the_answer다. 같은 test에 "exit 직전에 pipe 버퍼(macOS 16 KiB) 가까이 쓴 출력이 잘리지 않는다"를 더했다.
  • 판단해 주세요: capture_until의 held_pipe 계약도 바뀌었다(group 안 helper가 pipe를 쥐면 deadline 대신 exit에서 helper를 끝내고 답한다). deadline 상한은 살아 있는 리더로 따로 남겼다.
  • 조심해서 볼 파일: hide-platform/src/process.rs - 모든 run_to_end 호출의 failure path. 트리 종료 확인 전에는 답하지 않고, 확인 뒤에는 한 번이 아니라 빈 상태까지 읽는다.
  • 비용 검토: 고빈도 경로가 아니다. 호출은 설치, 은퇴, 진단, 일회성 읽기뿐이다.
    • run마다 리더 exit 뒤 group 확인이 붙는다. macOS는 syscall 하나, Linux는 /proc 한 번 훑기이고, kill된 구성원이 끝날 때까지 20 ms마다 반복한다.
  • 남은 누수: pipe 말고 macOS std가 두 번에 나눠 CLOEXEC를 거는 다른 fd(socket 등)는 여전히 동시에 spawn한 오래 사는 자식에게 샐 수 있다(posix_spawn CLOEXEC_DEFAULT는 이번 범위 밖).
  • 조심해서 볼 파일: docs/ARCHITECTURE.md - 세 OS가 같은 코드인 이유와, Windows에서 이 누수가 std 경로로 생기지 않는 이유(std의 CREATE_PROCESS_LOCK, job에 breakaway 없음).

Evidence

  • 확인한 것:
    • 원인 실측(probe, commit하지 않음): 4 스레드가 sleep 3을 계속 spawn하는 동안 run_to_end(sh -c 'exit 113') 600회.
      • 느린 1회가 3.04 s, 그 holder의 수명과 같았다. lsof로 그 sleep이 run의 pipe 양끝을 쥔 것을 확인했다.
    • std 사실: std가 띄운 자식은 CLOEXEC 없는 fd를 물려받는다(lsof로 확인). std 1.90 anon_pipe 소스에서 macOS는 pipe 다음 set_cloexec, Linux는 pipe2다.
    • hide-kit before: 수정 전 코드에서 문제의 test와 holder spawn test를 함께 20회. 1회가 #729와 같은 이유 문자열로 실패했다.
    • 결정적 재현(수정 전 process.rs + 새 계약 test): outside-holder test는 "the run waited for a write end outside its tree"(5.07 s)로, held-pipe capture는 30 s deadline으로 실패했다.
    • 같은 방식으로 a_stopped_child_is_not_a_finished_one은 수정 전 0.05 s 만에 Err(Wait("observed exit could not be reaped"))였다.
    • after(macOS, 부하 15-57): hide-platform process 계약은 stopped-child 수정 전 36개를 15회 반복해 0 실패, 최종 37개 통과. hide-kit lib 145개 3회, hide-agent-hooks 전부 통과. fmt, clippy(-D warnings) 통과.
  • 확인하지 못한 것: Linux와 Windows의 새 코드(/proc group 나열, job active process 수)는 로컬에서 컴파일·실행하지 못했고 CI lane(rust workspace, windows check, os contract)에 맡긴다.
    • 초당 약 400개를 spawn하는 holder probe 아래에서는 수정 뒤에도 hide-kit test가 가끔 실패했다(40회 중 5회, 다른 40회 중 1회).
    • 실패 시점 trace는 리더(/bin/sh 대역)가 5 s 뒤에도 살아 있고(state S) exit가 한 번도 보이지 않았다. pipe 누수가 아니라 그 storm에서 프로세스 시작 자체가 5 s를 넘긴 것이다.
    • 원래 실패가 이 경로였는지는 가르지 못했다. 이 경로는 제품 deadline이 맞게 TimedOut을 답하는 경우라 이 PR은 다루지 않는다.

run_to_end and capture_until waited for both output pipes to reach their
end after the leader exited. On macOS the standard library makes a pipe and
marks each end close-on-exec in two calls, so a long-lived child another
thread starts in between keeps both ends, and the run then reported
TimedOut although its leader had answered. A helper that left the group
held the answer the same way.

Once the leader exits, the tree is ended, the run waits until no member of
the leader's group is alive (the job's active process count on Windows),
then reads both pipes until they are empty and answers. Signal 0 to the
group cannot say that on macOS, which refuses it with EPERM while a killed
member is still exiting.

The escaped-holder contract now expects the leader's whole answer, output
near a pipe buffer included, and the held-pipe capture answers at the exit;
a live leader still bounds the capture deadline.
has_exited asked waitid for WEXITED with WNOWAIT, and macOS answers that
call for a stopped child too. A run then took the stop for an exit, found
nothing waitpid could reap and failed with "observed exit could not be
reaped" at once, which ssh host resolution reports as SshFailed. Only an
exit, a kill or a core dump now counts, so a stopped child runs to the
deadline and is ended there.
@yansfil
yansfil merged commit 35f480e into main Oct 7, 2026
20 checks passed
@yansfil
yansfil deleted the fix/flaky-729-retirement branch October 7, 2026 10:50
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.

Flaky test: hide-kit retirement_without_a_herdr_server_finishes_when_no_plugin_is_registered

1 participant