Skip to content

fix: end a tied ssh's whole process tree, orphans included, before its attempt ends - #1847

Merged
yansfil merged 12 commits into
mainfrom
fix/tied-tree-orphans
Oct 30, 2026
Merged

yansfil merged 12 commits into
mainfrom
fix/tied-tree-orphans

Conversation

@yansfil

@yansfil yansfil commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes with-hide/hide-private#335
Closes with-hide/hide-private#337
Related: #1843, #1830

Summary

  • 장비 ssh가 띄운 프로세스가 setsid로 떨어져 나가고 부모가 바로 끝나면, 그 프로세스가 ssh보다 오래 살아남을 수 있었습니다(Windows 기기 브리지: 원격 데몬의 pane bootstrap을 LocalListener/LocalStream 위로 #335).
    지금까지는 표본을 떠서 기억해 둔 프로세스만 정리했기 때문입니다.
  • 이제 ssh를 감독하는 프로세스는 셸이 아니라 Hide 자신의 프로그램입니다.
    같은 프로그램을 감독 역할로 한 번 더 띄우고, main보다 먼저 감독 일을 시작합니다.
  • 정리 방법은 하나뿐이고, ssh가 끝날 때, 취소할 때, Hide가 갑자기 죽을 때 모두 그 하나를 씁니다.
    • "찾아봤는데 없다"는 끝난 증거가 아닙니다.
      Linux는 커널이 "남은 자식이 없다"고 답할 때만 끝난 것으로 칩니다.
      macOS는 프로세스 표를 빠짐없이 읽고 하나도 못 찾은 look이 두 번 연달아 나올 때만 끝난 것으로 칩니다.
      표를 읽고 거르는 사이에 사라진 프로세스가 있었다면 그 look은 세지 않습니다.
    • 정해진 시간(5초) 안에 증거가 없으면 "끝났다"고 답하지 않고 오류로 답합니다.
  • ssh는 Hide가 "시작해도 된다"(go)고 답한 뒤에만 돌기 시작합니다.
    시작을 기다리다 포기한 Hide는 답하지 않으므로, 감독이 붙잡고 있던 ssh를 스스로 끝냅니다.
    그래서 Hide가 모르는 ssh가 남지 않습니다.
  • 끝을 확인하지 못해도 Hide는 멈추지 않습니다.
    감독과 Hide가 남기는 로그 한 줄은 막히지 않는 통로로만 쓰고, 못 쓰면 버립니다.
    제때 거두지 못한 프로세스는 프로세스마다 하나인 스레드가 최대 64개까지 맡고, 넘치면 로그만 남깁니다.
  • 앱이 업데이트된 뒤 예전 hided가 계속 돌면(macOS) ssh를 띄울 수 없습니다.
    이제 장비 줄, core 연결 띠, core 옮기기 점검이 "Hide를 종료하고 다시 열면 연결돼요"라고 네 언어로 말하고, 다시 시도 버튼을 내지 않습니다.
    다시 시도해도 같은 이유로 실패하기 때문입니다.
  • CI에서 자꾸 깨지던 #337은 순서 문제였습니다. 감독만 거두고, 아직 죽어 가던 나머지를 기다리지 않았습니다.

업데이트로 바뀐 Hide의 장비 줄: 다시 열라는 문장, 다시 시도 없음 (갤러리)

Review

  • 판단해 주세요: 감독을 Hide 자신의 프로그램으로 다시 띄우는 방식(ctor 크레이트의 생성자가 main보다 먼저 돌며, 표시 변수가 없으면 getenv 한 번만 하고 바로 돌아갑니다).
    • Linux는 /proc/self/exe를 띄워서, 업데이트로 파일이 바뀌어도 지금 돌고 있는 그 파일을 씁니다.
    • macOS는 경로로만 띄울 수 있어서, 그 경로의 파일이 처음 시작한 파일과 다르면 띄우지 않고 program_replaced 오류로 답합니다.
    • 표시 변수에는 프로토콜 버전(이번에 2)이 들어갑니다. 버전이 다른 감독은 그렇다고 말하고, 어느 쪽도 다른 방식으로 대신 띄우지 않습니다.
  • 판단해 주세요: 시작 약속.
    감독은 spawned를 말한 뒤 자식을 붙잡은 채 go를 10초까지 기다립니다.
    Hide는 자기 시작 한도 안에서만 go를 씁니다. 그 뒤에는 failed가 아닌 모든 결과(침묵, 감독의 끝)에서 자식을 호출자에게 넘기고, 그 끝은 다른 끝과 같은 정리를 거칩니다.
  • 판단해 주세요: macOS의 증거 기준.
    ESRCH가 아닌 이유로 id를 못 읽은 프로세스가 하나라도 있으면 그 look은 증거가 되지 않습니다.
    어떤 프로세스가 늘 그렇게 실패하면 모든 끝이 5초 뒤 "확인 안 됨"이 됩니다(이 Mac에서는 모든 프로세스가 답했습니다).
  • 판단해 주세요: 로그 통로.
    stderr가 일반 파일이면 그대로 쓰고, 아니면 이름으로 다시 열어 막히지 않는 통로를 따로 만듭니다(Linux는 소켓 말고 모두, macOS는 터미널만).
    그 밖에는 /dev/null입니다.
    hide connect가 띄운 hided는 stderr가 /dev/null이라 이 줄들은 거기서 사라집니다. 이번 변경 전부터 그랬고, hide-platform에서 hided의 로그로 가는 길은 없습니다.
  • 판단해 주세요: 업데이트된 Hide의 장비 줄에도 "테스트" 버튼은 남습니다. 서버 키가 바뀐 줄과 같습니다. 테스트는 진단이고 다시 연결하지 않습니다.
  • 조심해서 볼 파일:
    • hide-platform/src/process.rs - ownership, failure path:
      • started와 감독의 start/answered: go를 주고받는 시작 약속.
      • Tree::look_between: macOS에서 두 번 연달아 조용한 look.
      • diagnostics_of_its_own, diagnose, Reaping/admit/reap_waiting, drop_tied.
      • supervisor 모듈은 생성자 안에서 돕니다. 그래서 C 라이브러리 호출과 main 준비가 필요 없는 표준 라이브러리만 씁니다.
    • hide-node-link/src/device.rs, herdr-core/src/model.rs, contracts/snapshot-wire-enums.json - snapshot wire: 새 값 hide_updated, 새 동작 reopen_hide.
    • web/src/settings.ts(retryCanHelp), web/src/connection.ts, web/src/settings/CoreMoveDialog.tsx: 다시 시도를 숨기는 세 곳.
    • hide-node/src/ssh/host.rs: 끝이 확인되지 않으면 host.child_end_unconfirmed를 남기고 시도를 놓지 않습니다.
  • 비용 검토:
    • 상시 작업은 없습니다. 감독은 자식이 끝나거나 말을 들을 때까지 poll에서 잡니다.
    • spawn마다 /bin/sh 대신 Hide 프로그램을 한 번 exec 하고, 시작 때 왕복 한 번(spawned/go)이 더 있습니다.
    • 트리를 끝낼 때만 프로세스 표를 걷습니다. macOS는 마지막에 한 번 더 걷습니다.
    • 거두는 스레드는 프로세스마다 하나이고, 맡은 것이 있을 때만 100ms마다 기다리지 않고 묻습니다.

Evidence

  • 이전 커밋(d7fe335)에서 실패하는 테스트 (macOS, 새 테스트를 옛 process.rs에 붙여 돌림):
    • a_tied_supervisor_lets_its_child_go_only_once_its_owner_answers: 옛 감독은 버전 1이라 거절합니다. 버전 1 표시로 다시 돌리면, 답하지 않았는데도 자식을 돌렸습니다(failed 60 대신 started).
    • unconfirmed_tied_drops_leave_no_thread_or_child_behind(stderr를 가득 채움): drop 6번에 스레드가 2개에서 8개로 늘었습니다.
    • a_member_that_ends_between_the_table_and_the_look_leaves_no_proof(macOS 단위): 옛 코드에는 look_between이 없어 붙일 수 없습니다. 규칙을 한 번의 look으로 되돌리면 실패합니다.
    • hide_updated 테스트들(core, web): 옛 코드에는 그 값이 없어 컴파일되지 않습니다.
  • 확인한 것 (main 병합 전 07cb645, 병합 후 2f09251):
    • macOS 15.1: hide-platform, herdr-core, hide-node, hided, hide-node-link, hide-agent-hooks 5118/5118. scripts/verify-cargo.sh lint, Windows 대상 hide-platform clippy가 깨끗합니다.
    • main 병합 뒤: lint가 깨끗하고, hide-node, hide-node-link, herdr-core, hided 4418/4419입니다.
      실패 하나는 이 PR이 건드리지 않는 hided link_mux::tests::the_connection_ending_frees_every_stream입니다. 전체 병렬 실행 3번 중 2번 실패했고(한 번은 EINVAL, 한 번은 15초 대기), 혼자 20번 돌리면 모두 통과합니다.
    • scripts/verify-web.sh: 2283 통과(1 건너뜀), desktop 298 통과(4 건너뜀). node scripts/check-design-contract.mjs 통과.
    • Linux(CI와 같은 ubuntu:26.04 이미지, 이 Mac의 Docker): process 68/68, 단위 48/48, 끝난 뒤 남은 프로세스 없음. 워크스페이스 cargo clippy --locked --workspace --all-targets -- -D warnings가 깨끗합니다.
  • 확인하지 못한 것:
    • Windows 테스트는 CI에서만 돕니다(Windows 호스트 없음).
    • 실제 sshd를 쓰는 system_ssh 테스트는 CI의 remote mailbox 레인에서만 돕니다.
    • 실제 앱 업데이트 중의 장비 줄은 돌려 보지 않았습니다. 화면은 갤러리 캡처뿐이고, 설치된 앱은 건드리지 않았습니다.
    • macOS start()에서 감독이나 자식의 id 읽기가 실패하는 경로는 테스트가 없습니다.
    • 이번에는 부하 반복을 돌리지 않았습니다(공용 Mac).
Verification record

…s as ended

On Linux the tied supervisor is a child subreaper, so a process whose parent
ends comes back under it; when the child exits the supervisor kills what is
under it until nothing lives, and the owner's kill stops the supervisor and
does the same before ending it. On macOS the tree is what chains by parent
unique id to the supervisor or the child, whose ids the owner reads while
the child is held before it runs. The sampling keeper is gone.
…d an exiting process as alive

The watcher no longer stops the supervisor after the owner's death: a
stopped member of a group with no parent outside it gets the whole group
hung up, which ended the walk before it reached the orphan. It sends TERM
instead, and the supervisor ends what is under it.

On Linux a process whose first thread ended still lists Z while its other
threads exit, so is_alive now waits for the last thread; on both systems a
process reaped between signal 0 and the state read counts as ended.

The orphan tests wait until the orphan has left the group, so the group's
kill cannot end it for them.
A tied child's supervisor is now this program started again, which a
constructor runs before main: one end, identity-checked and judged by
is_alive alone, runs when the child exits, when the owner says end and
when the owner dies, and an end past its bound is an error rather than a
status. Windows confirms the job runs nothing before a tied child counts
as ended.
…ed end

A look that finds nothing no longer counts as the tree's end. On Linux
the end waits for the kernel to say the supervisor has no child left
(ECHILD), reaping as it looks and keeping the child's status, so a
process started after the /proc pass, or one that cannot be pinned, keeps
the end open. On macOS a look proves the end only when it read the whole
table and every existing process's ids, and the ids found are kept from
look to look; a failed read of the supervisor's or child's own ids ends
the start before the child runs.

A start is rolled back only while the supervisor still holds the child.
Once it said `spawned`, silence or its end hands the child to its owner,
whose end of it runs the tree's end; after `failed` it says `confirmed 0`.

The supervisor ignores HUP, INT, QUIT, TERM and PIPE from before it
starts the child, which gets them back as they were given. Its diagnostic
write gives up after a second and its watch writes never block, and a
tied child's drop waits for its supervisor within a bound, kills it, and
leaves one that still runs to a reaping thread.

The supervisor state is boxed, so every owner of a child stays small
(clippy large_enum_variant). `program_replaced` names the macOS error for
a program file an update replaced.
…gram

On macOS a hided left running from a bundle an update replaced cannot
start ssh. The device row now says Hide was updated and to quit and open
it again, which starts the new build, instead of an error about the
tied supervisor.
…its drop

The supervisor now says spawned while it still holds the child and lets it
go only once the owner answers go inside its start bound; an owner that gave
up never answers, so the supervisor ends the held child itself (marker v2).

macOS proves an end only by two quiet looks in a row, and a member that ends
between the process table and the look makes that look not quiet.

Every tied diagnostic goes through a nonblocking description of its own and
is dropped when it does not fit, and children a drop could not reap go to one
capped reaper thread per process instead of a thread each.
… Retry

A program_replaced failure is now the typed problem hide_updated, whose
action is reopen_hide. The device row, a node's stopped core link strip and
the move dialog's connection check say in four languages that Hide was
updated and must be opened again, and none offers Retry or dials on its own.
@yansfil
yansfil merged commit 4f9f8d9 into main Oct 30, 2026
55 checks passed
@yansfil
yansfil deleted the fix/tied-tree-orphans branch October 30, 2026 00:24
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.

1 participant