@@ -34,6 +34,7 @@ public GetStreamResult ExecuteCommand(CommandProfileDto commandProfile, string s
3434
3535 using var registration = cancellationToken . Register ( ( ) =>
3636 {
37+ logger . LogDebug ( "Cancellation requested for Stream process" ) ;
3738 GracefullyTerminateProcess ( ) ;
3839 } ) ;
3940
@@ -46,7 +47,20 @@ public GetStreamResult ExecuteCommand(CommandProfileDto commandProfile, string s
4647 return new GetStreamResult ( null , - 1 , error ) ;
4748 }
4849
50+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
51+ {
52+ try
53+ {
54+ Process . Start ( "setpgrp" , $ "{ _process . Id } ") ;
55+ }
56+ catch ( Exception ex )
57+ {
58+ logger . LogWarning ( ex , "Failed to set process group for {ProcessId}" , _process . Id ) ;
59+ }
60+ }
61+
4962 string stderrFilePath = Path . Combine ( BuildInfo . CommandErrorFolder , $ "stderr_{ _process . Id } .log") ;
63+ Directory . CreateDirectory ( Path . GetDirectoryName ( stderrFilePath ) ! ) ;
5064 errorWriter = new StreamWriter ( stderrFilePath , append : true , Encoding . UTF8 ) ;
5165
5266 // Clean up older logs to keep only the latest 10
@@ -64,6 +78,7 @@ public GetStreamResult ExecuteCommand(CommandProfileDto commandProfile, string s
6478 }
6579 } ;
6680 _process . BeginErrorReadLine ( ) ;
81+ _process . EnableRaisingEvents = true ; // Ensure Exited event is raised
6782 _process . Exited += Process_Exited ;
6883
6984 stopwatch . Stop ( ) ;
@@ -242,6 +257,16 @@ private static void ConfigureProcess(Process process, string commandExec, string
242257 process . StartInfo . RedirectStandardOutput = true ;
243258 process . StartInfo . RedirectStandardError = true ;
244259 process . StartInfo . StandardOutputEncoding = Encoding . UTF8 ;
260+ process . StartInfo . StandardErrorEncoding = Encoding . UTF8 ;
261+ process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
262+
263+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
264+ {
265+ process . StartInfo . Environment [ "SM_PROCESS_ID" ] = process . Id . ToString ( ) ;
266+ process . StartInfo . Environment [ "SM_PROCESS_TYPE" ] = "STREAM" ;
267+ }
268+
269+ process . EnableRaisingEvents = true ;
245270 }
246271
247272 /// <summary>
@@ -273,4 +298,4 @@ public void Dispose()
273298 _disposed = true ;
274299 GC . SuppressFinalize ( this ) ;
275300 }
276- }
301+ }
0 commit comments