Skip to content

Prevent native child termination from signaling reused PIDs - #786

Merged
emil-e merged 3 commits into
dakra:mainfrom
emil-e:fix/native-child-pid-reuse
Nov 5, 2026
Merged

emil-e merged 3 commits into
dakra:mainfrom
emil-e:fix/native-child-pid-reuse

Conversation

@emil-e

@emil-e emil-e commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #779, keeping the refcounter refactor and termination fix in separate commits:

  • Extract intrusive RefCount(Parent) for terminal and reader ownership.
  • Retain child controls on the event pipe instead of terminating native children through recorded numeric PIDs.
  • On POSIX, observe exit with waitid(WNOWAIT), then disable termination and reap under the signaling mutex so the PID cannot be reused while signaling remains possible.
  • On Windows, retain the process HANDLE independently of PTY teardown.
  • Balance failed setup/userptr ownership and order Debug finalization after child termination.

Verification

On the current main-based branch:

  • Debug module build and 50 Zig tests passed.
  • 82 native-process, spawn, and exec ERT tests passed on macOS.

Before cherry-picking onto current main, release-runtime tests, Elisp byte-compilation/checkdoc, and Linux/FreeBSD/Windows cross-builds also passed. Non-macOS runtime remains unverified.

Independent static review found no actionable defects. POSIX identity safety requires this control to be the sole consuming reaper.

Share RefCount(Parent) between GhostelTerm and NativeProcess, with direct retain/release calls and destruction on the final reference. Acquire the reader's reference explicitly before spawning its thread.

Cover parent recovery, multiple initial references, and concurrent final release.
Retain child controls independently of the PTY backend and use them for native termination instead of signaling recorded numeric PIDs.

Use waitid(WNOWAIT) to reserve POSIX child PIDs until termination is disabled under the reaping mutex. Keep Windows process HANDLEs open until final reference release.

Make userptr ownership transfer fallible, clean up failed setup, and run Debug finalization after child termination. Cover reaping, retained-handle lifetime, spawn failure, and sentinel cleanup.
@emil-e
emil-e force-pushed the fix/native-child-pid-reuse branch from b5bfa0f to 58b151f Compare November 4, 2026 23:08
kill(2) can report ESRCH for a child that is already exiting.  The
unreaped child still reserves its PID, so that means it is gone.

Allocate ChildProcess before spawning, which removes the post-spawn
kill-and-reap path from both backends.  Hand the Emacs handle's
reference out of NativeProcess.init instead of keeping a child field.
Reduce the spawn cleanup in Elisp to deleting the pipe on failure.
@emil-e
emil-e merged commit 6591d24 into dakra:main Nov 5, 2026
30 checks passed
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.

2 participants