Skip to content

fix ghost ps process instances#8

Merged
dfinke merged 3 commits intodfinke:mainfrom
luchezarno:fix-phantom-Powershell-processes
Dec 28, 2025
Merged

fix ghost ps process instances#8
dfinke merged 3 commits intodfinke:mainfrom
luchezarno:fix-phantom-Powershell-processes

Conversation

@luchezarno
Copy link
Contributor

Check if parent process is still running otherwise exit. Due to not to hanging ps in Processes each time PSMCP is used by LLM

Check if parent process is still running otherwise exit.
Due to not to hanging ps in Processes each time PSMCP is used by LLM
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a parent process monitoring mechanism to prevent ghost PowerShell processes by checking if the parent process is still running and exiting if it's not. The goal is to avoid hanging ps processes each time the PSMCP is used by an LLM.

Key Changes:

  • Added parent process health check in the main server loop
  • Implemented graceful shutdown when parent process exits

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


# Check if parent process is still running otherwise exit
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable $parentProcessId is not defined anywhere in the function. It's not included in the function parameters, and there's no evidence of it being set in the module scope. This code will fail at runtime because PowerShell will not be able to resolve this variable. Consider adding $parentProcessId as a parameter to the function, or use the automatic variable $PID with a mechanism to track the parent process ID.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops :) Sorry, seems I forgot to insert from the beginning 🤦‍♂️ Will fix it now

    # Get parent process ID to monitor for disconnection
    $currentProcess = Get-Process -Id $PID
    $parentProcessId = $currentProcess.Parent.Id
    Write-Log -LogEntry @{ Level = 'Info'; Message = "MCP Server started with PID: $PID, Parent PID: $parentProcessId" }

Comment on lines +20 to +23
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check is unreachable code. When Get-Process -ErrorAction Stop succeeds, it will always return a process object, never null. If the process doesn't exist, it throws an exception which is caught by the catch block. This condition at line 20-23 will never execute. The catch block already handles the case when the parent process is not found.

Suggested change
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}

Copilot uses AI. Check for mistakes.
Comment on lines 15 to +28
while ($true) {

# Check if parent process is still running otherwise exit
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if the parent process is running on every iteration of the while loop could impact performance, especially when processing many requests. Consider checking the parent process less frequently, such as every N iterations or using a timer-based approach, to reduce the overhead while still catching orphaned processes in a reasonable timeframe.

Suggested change
while ($true) {
# Check if parent process is still running otherwise exit
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
# Control how often we check whether the parent process is still running
$parentCheckInterval = 100
$iterationCount = 0
while ($true) {
$iterationCount++
# Check if parent process is still running otherwise exit (only every N iterations)
if ($iterationCount % $parentCheckInterval -eq 0) {
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
}

Copilot uses AI. Check for mistakes.
added forgotten $parentProcessId
@luchezarno
Copy link
Contributor Author

@dfinke seems only you are able to run Copilot review, Please rerun it once more
Thank you!

@luchezarno
Copy link
Contributor Author

luchezarno commented Dec 27, 2025

BTW Seems it works only in PowerShell 7+ 🤔

$currentProcess = Get-Process -Id $PID
$parentProcessId = $currentProcess.Parent.Id

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dfinke
Copy link
Owner

dfinke commented Dec 28, 2025

Cool. Copilot in motion. Yeah only the repo owner can kick it off.

I asked AI about that snippet.

image

Good stuff

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@luchezarno
Copy link
Contributor Author

Cool. Copilot in motion. Yeah only the repo owner can kick it off.

I asked AI about that snippet.

Well, since you are using -Depth XX in code which only supported in 7+ then I'd leave .parent as it. So PS7 is prerequisite

@dfinke dfinke added the enhancement New feature or request label Dec 28, 2025
@dfinke dfinke merged commit a088cae into dfinke:main Dec 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants