-
Notifications
You must be signed in to change notification settings - Fork 11
fix ghost ps process instances #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,15 @@ function Start-McpServer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [string[]]$Tools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Get parent process ID to monitor for disconnection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $currentProcess = Get-Process -Id $PID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ($null -eq $currentProcess.Parent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Write-Log -LogEntry @{ Level = 'Error'; Message = "Current process has no parent process. Shutting down MCP server..." } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $parentProcessId = $currentProcess.Parent.Id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Write-Log -LogEntry @{ Level = 'Info'; Message = "MCP Server started with PID: $PID, Parent PID: $parentProcessId" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Check if the tools are provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (-not $Tools) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Write-Log -LogEntry @{ Level = 'Error'; Message = "No tools provided to Start-McpServer" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -13,6 +22,20 @@ function Start-McpServer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Write-Log -LogEntry @{ Level = 'Info'; Message = "Starting MCP Server" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+32
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ($null -eq $parentProcess) { | |
| Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." } | |
| exit | |
| } |
Copilot
AI
Dec 27, 2025
There was a problem hiding this comment.
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.
| 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 | |
| } | |
| } |
Uh oh!
There was an error while loading. Please reload this page.