Expose foreground process group PID on Unix#918
Open
srid wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
Add a `foregroundPid` getter that returns `tcgetpgrp(fd)` on the pty master fd, exposing the pid of the foreground process group attached to the pty (or `undefined` on failure). This is the same syscall the existing `pty_getproc` already invokes internally to resolve the foreground process *name* — it's just returning the pid instead of throwing it away. Useful for callers that need to identify which process is currently in the foreground of a terminal (e.g. to detect that an agent like claude-code is running, where the process name resolves to "node"). Cross-platform: works on both Linux and macOS via tcgetpgrp(3). Not exposed on Windows (no equivalent ConPTY concept).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unix terminals can now report the foreground process group PID directly.
processalready resolves the foreground process name by callingtcgetpgrp(3)internally; this exposes the same process-group id asforegroundPidfor callers that need to identify the active foreground program without reverse-mapping from a process title.The property is optional on the public
IPtytype because Windows/ConPTY does not expose an equivalent concept. On Unix it returnsnumber | undefined, matching the native lookup behavior when the pty has already exited ortcgetpgrpfails.Verification
npm run buildnpm test -- --grep "foreground process group"npm teston the same macOS host currently reaches 23 passing tests and then times out in the existingshould return the name of the sub processtest; the new foregroundPid test passes before that unrelated timeout.Generated by Codex (model
gpt-5).