Skip to content

Commit dfaf501

Browse files
committed
Add debug output for Named Pipe server startup
1 parent 740b538 commit dfaf501

3 files changed

Lines changed: 72 additions & 18 deletions

File tree

.github/workflows/cross-platform-test.yml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ jobs:
271271
$bgProcess = [System.Diagnostics.Process]::Start($bgPsi)
272272
Write-Host "Background pwsh PID: $($bgProcess.Id)"
273273
274+
# Start async stderr reading to capture debug output
275+
$bgStderrBuilder = [System.Text.StringBuilder]::new()
276+
$bgStderrTask = $bgProcess.StandardError.ReadToEndAsync()
277+
274278
# Wait for Named Pipe server to be ready
275279
Write-Host "Waiting for Named Pipe server..."
276280
$pipeName = "PowerShell.MCP.Communication"
@@ -284,10 +288,9 @@ jobs:
284288
Write-Host "=== stdout ===" -ForegroundColor Yellow
285289
Write-Host $bgProcess.StandardOutput.ReadToEnd()
286290
Write-Host "=== stderr ===" -ForegroundColor Yellow
287-
Write-Host $bgProcess.StandardError.ReadToEnd()
291+
if ($bgStderrTask.IsCompleted) { Write-Host $bgStderrTask.Result }
288292
throw "Background pwsh process exited unexpectedly"
289293
}
290-
291294
if ($IsWindows) {
292295
$pipeReady = Test-Path "\\.\pipe\$pipeName"
293296
} else {
@@ -320,13 +323,19 @@ jobs:
320323
Write-Host ([char]$bgProcess.StandardOutput.Read()) -NoNewline
321324
}
322325
Write-Host ""
323-
Write-Host "=== Available stderr ===" -ForegroundColor Yellow
324-
while ($bgProcess.StandardError.Peek() -ge 0) {
325-
Write-Host ([char]$bgProcess.StandardError.Read()) -NoNewline
326-
}
327-
Write-Host ""
326+
# Kill process to allow stderr task to complete
328327
$bgProcess.Kill()
328+
$bgProcess.WaitForExit(5000)
329+
}
330+
331+
# Wait for stderr task and display
332+
Write-Host "=== stderr (debug output) ===" -ForegroundColor Yellow
333+
if ($bgStderrTask.Wait(3000)) {
334+
Write-Host $bgStderrTask.Result
335+
} else {
336+
Write-Host "(stderr read timeout)"
329337
}
338+
330339
throw "Named Pipe server did not start within 15 seconds"
331340
}
332341
@@ -390,9 +399,18 @@ jobs:
390399
Write-Host "`n=== All Named Pipe tests passed ===" -ForegroundColor Green
391400
392401
} finally {
402+
# Dump bgProcess stderr for debugging before cleanup
403+
Write-Host "=== Background pwsh stderr ===" -ForegroundColor Cyan
404+
if (-not $bgProcess.HasExited) {
405+
$bgProcess.Kill()
406+
$bgProcess.WaitForExit(5000)
407+
}
408+
if ($bgStderrTask.Wait(3000)) {
409+
Write-Host $bgStderrTask.Result
410+
}
411+
393412
if (-not $process.HasExited) { $process.Kill() }
394413
$process.Dispose()
395-
if (-not $bgProcess.HasExited) { $bgProcess.Kill() }
396414
$bgProcess.Dispose()
397415
}
398416

PowerShell.MCP/MCPModuleInitializer.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,22 @@ public void OnImport()
5151
ps.Invoke();
5252
}
5353

54-
// Start Named Pipe Server
54+
// Start Named Pipe Server with error handling
55+
Console.Error.WriteLine("[DEBUG] Starting Named Pipe server task...");
5556
Task.Run(async () =>
5657
{
57-
await _namedPipeServer.StartAsync(_tokenSource.Token);
58+
try
59+
{
60+
Console.Error.WriteLine($"[DEBUG] Named Pipe server task started on thread {Environment.CurrentManagedThreadId}");
61+
Console.Error.WriteLine($"[DEBUG] Pipe name: {NamedPipeServer.PipeName}");
62+
await _namedPipeServer.StartAsync(_tokenSource.Token);
63+
Console.Error.WriteLine("[DEBUG] Named Pipe server StartAsync completed");
64+
}
65+
catch (Exception ex)
66+
{
67+
Console.Error.WriteLine($"[ERROR] Named Pipe server failed: {ex.GetType().Name}: {ex.Message}");
68+
Console.Error.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
69+
}
5870
}, _tokenSource.Token);
5971

6072
// Output information message

PowerShell.MCP/Services/NamedPipeServer.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class NamedPipeServer : IDisposable
3333
/// </summary>
3434
public async Task StartAsync(CancellationToken cancellationToken)
3535
{
36+
Console.Error.WriteLine($"[DEBUG] NamedPipeServer.StartAsync called, MaxConcurrentConnections={MaxConcurrentConnections}");
3637
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(
3738
cancellationToken, _internalCancellation.Token);
3839

@@ -41,20 +42,25 @@ public async Task StartAsync(CancellationToken cancellationToken)
4142
// Start multiple server instances
4243
for (int i = 0; i < MaxConcurrentConnections; i++)
4344
{
45+
Console.Error.WriteLine($"[DEBUG] Starting server instance {i + 1}/{MaxConcurrentConnections}");
4446
var task = RunServerInstanceAsync(combinedCts.Token);
4547
_serverTasks.Add(task);
4648
}
4749

50+
Console.Error.WriteLine("[DEBUG] All server instances started, waiting for completion...");
4851
// Wait for all server instances to complete
4952
await Task.WhenAll(_serverTasks);
53+
Console.Error.WriteLine("[DEBUG] All server instances completed");
5054
}
5155
catch (OperationCanceledException)
5256
{
57+
Console.Error.WriteLine("[DEBUG] StartAsync cancelled");
5358
// Cancellation is normal termination
5459
}
5560
catch (Exception ex)
5661
{
57-
Console.Error.WriteLine($"Named Pipe Server error: {ex.Message}");
62+
Console.Error.WriteLine($"[ERROR] Named Pipe Server error: {ex.GetType().Name}: {ex.Message}");
63+
Console.Error.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
5864
}
5965
}
6066

@@ -63,25 +69,31 @@ public async Task StartAsync(CancellationToken cancellationToken)
6369
/// </summary>
6470
private async Task RunServerInstanceAsync(CancellationToken cancellationToken)
6571
{
72+
Console.Error.WriteLine($"[DEBUG] RunServerInstanceAsync started on thread {Environment.CurrentManagedThreadId}");
6673
while (!cancellationToken.IsCancellationRequested)
6774
{
6875
try
6976
{
77+
Console.Error.WriteLine("[DEBUG] Creating Named Pipe server...");
7078
using var pipeServer = CreateNamedPipeServer();
79+
Console.Error.WriteLine($"[DEBUG] Named Pipe server created, waiting for connection...");
7180

7281
// Wait for client connection
7382
await pipeServer.WaitForConnectionAsync(cancellationToken);
83+
Console.Error.WriteLine("[DEBUG] Client connected to Named Pipe");
7484

7585
// Handle communication with connected client
7686
await HandleClientAsync(pipeServer, cancellationToken);
7787
}
7888
catch (OperationCanceledException)
7989
{
90+
Console.Error.WriteLine("[DEBUG] Named Pipe server cancelled");
8091
break;
8192
}
8293
catch (Exception ex)
8394
{
84-
Console.Error.WriteLine($"Named Pipe Server instance error: {ex.Message}");
95+
Console.Error.WriteLine($"[ERROR] Named Pipe Server instance error: {ex.GetType().Name}: {ex.Message}");
96+
Console.Error.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
8597

8698
// Wait a moment before retrying on error
8799
try
@@ -94,19 +106,31 @@ private async Task RunServerInstanceAsync(CancellationToken cancellationToken)
94106
}
95107
}
96108
}
109+
Console.Error.WriteLine("[DEBUG] RunServerInstanceAsync exiting");
97110
}
98111

99112
/// <summary>
100113
/// Creates a Named Pipe server
101114
/// </summary>
102115
private static NamedPipeServerStream CreateNamedPipeServer()
103116
{
104-
return new NamedPipeServerStream(
105-
PipeName,
106-
PipeDirection.InOut,
107-
NamedPipeServerStream.MaxAllowedServerInstances,
108-
PipeTransmissionMode.Byte,
109-
PipeOptions.Asynchronous);
117+
Console.Error.WriteLine($"[DEBUG] CreateNamedPipeServer: PipeName={PipeName}");
118+
try
119+
{
120+
var server = new NamedPipeServerStream(
121+
PipeName,
122+
PipeDirection.InOut,
123+
NamedPipeServerStream.MaxAllowedServerInstances,
124+
PipeTransmissionMode.Byte,
125+
PipeOptions.Asynchronous);
126+
Console.Error.WriteLine($"[DEBUG] NamedPipeServerStream created successfully");
127+
return server;
128+
}
129+
catch (Exception ex)
130+
{
131+
Console.Error.WriteLine($"[ERROR] Failed to create NamedPipeServerStream: {ex.GetType().Name}: {ex.Message}");
132+
throw;
133+
}
110134
}
111135

112136
/// <summary>

0 commit comments

Comments
 (0)