On Wed, 22 Apr 2026 12:50:23 GMT, Michael McMahon <[email protected]> wrote:

>> Hi,
>> 
>> This is a small fix for j.l.ProcessHandle on MacOS. Unlike other platforms, 
>> Mac returns a ProcessHandle for pid 0 whose descendants are _all_ processes 
>> on the system. This specific scenario tickles an off by one error where the 
>> descendants method tries to access an element past the end of the array of 
>> pids. The fix is to break from the loop before accessing this element.
>> 
>> Thanks,
>> 
>> Michael
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Michael McMahon has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains seven additional 
> commits since the last revision:
> 
>  - Merge branch 'master' into pidzero
>  - Update src/java.base/share/classes/java/lang/ProcessHandleImpl.java
>    
>    Co-authored-by: Andrey Turbanov <[email protected]>
>  - updated impl and test
>  - Merge branch 'master' into pidzero
>  - remove files
>  - remove imports not needed
>  - fix

> > ProcessHandle.of(0) returns a real handle on Mac, but not on Linux or 
> > Windows. We could disallow its creation on Mac too, but existing code could 
> > be using it. The parent of `1` is `0` and the parent of `0` is itself.
> 
> ok, pid 0 is the kernel on MacOS. The anomaly is caused by the overload of 0 
> when calling native to return all processes. The invariant should be that the 
> stream returned from `getDescendants()` does not return a ProcessHandle for 
> the pid itself. But I suppose that is a different bug. The current bug would 
> not occur if the requested pid was ignored.



> > ProcessHandle.of(0) returns a real handle on Mac, but not on Linux or 
> > Windows. We could disallow its creation on Mac too, but existing code could 
> > be using it. The parent of `1` is `0` and the parent of `0` is itself.
> 
> ok, pid 0 is the kernel on MacOS. The anomaly is caused by the overload of 0 
> when calling native to return all processes. The invariant should be that the 
> stream returned from `getDescendants()` does not return a ProcessHandle for 
> the pid itself. But I suppose that is a different bug. The current bug would 
> not occur if the requested pid was ignored.

Yes. While you can argue it is reasonable that 
`ProcessHandle.of(0).get().descendants()` should return all processes, it's 
interesting that `ProcessHandle.of(0).get().children()` also returns all 
processes, due to that overloaded meaning of 0 in the native code. But, it 
should only return the handle for pid `1`.

We could change the native code to use `-1` as the special value to return all 
processes, but in the end it would be new behavior and wouldn't return anything 
useful. 

I'm okay with just disallowing the creation of `ProcessHandle.of(0)` on Mac.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/30763#issuecomment-4304252931

Reply via email to