@@ -14,6 +14,11 @@ public class CodexExecTests
1414{
1515 private const string LongRunningCliScript = "#!/bin/sh\n echo $$\n exec /bin/sleep 30\n " ;
1616 private const string DescendantHoldingStderrScript = "/usr/bin/setsid /bin/sleep 30 >&2 & echo $!; exit 0" ;
17+ private const string DescendantHoldingStderrWhileParentRunsScript =
18+ "/usr/bin/setsid /bin/sleep 30 >&2 & echo $!; exec /bin/sleep 30" ;
19+ private const string WindowsDuplexStdinCommandTemplate = "$pressure = 'o' * {0}; [Console]::Out.WriteLine($pressure); [Console]::Out.Flush(); [Console]::Error.WriteLine(('e' * {0})); [Console]::Error.Flush(); $prompt = [Console]::In.ReadToEnd(); [Console]::Out.Write($prompt); [Console]::Out.WriteLine(\" stdin-length:$($prompt.Length)\" ); [Console]::Out.WriteLine('stdin-eof')" ;
20+ private const string PosixDuplexStdinScriptTemplate = "head -c {0} /dev/zero | tr '\\ 000' 'o'; printf '\\ n'; head -c {0} /dev/zero | tr '\\ 000' 'e' >&2; printf '\\ n' >&2; prompt=$(cat); printf '%s\\ n' \" $prompt\" ; printf 'stdin-length:%s\\ n' \" ${#prompt}\" ; printf 'stdin-eof\\ n'" ;
21+ private const string DuplexPressureTemplatePlaceholder = "{0}" ;
1722 private const string PosixFixtureSkipReason = "The public CLI yield-boundary fixture currently uses a POSIX executable script." ;
1823 private const string LinuxFixtureSkipReason = "The detached stderr-retention fixture requires Linux setsid." ;
1924 private const string StderrClosureFailure = "stderr stream closed" ;
@@ -34,12 +39,119 @@ public class CodexExecTests
3439 private const string ExpectedSecondLine = "second" ;
3540 private const int SmallOutputLimitCharacters = 64 ;
3641 private const int AggregateOutputLimitCharacters = 24 ;
42+ private const int DuplexPipePressureCharacters = 131072 ;
43+ private const int DuplexPromptCharacters = 262144 ;
44+ private const int DuplexMaximumProcessOutputCharacters = 1048576 ;
45+ private static readonly TimeSpan ProcessOutputCleanupAssertionBound = TimeSpan . FromSeconds ( 8 ) ;
3746 private const string TestInput = "test" ;
3847 private const string CancellationScriptFileName = "codex-cancel.sh" ;
3948 private const string MissingExecutableNamePrefix = "missing-codex-cli-" ;
4049 private const string PosixShellPath = "/bin/sh" ;
4150 private const string PosixShellCommandFlag = "-c" ;
4251
52+ [ Test ]
53+ public async Task DefaultProcessRunner_DrainsBothOutputPipesWhileWritingLargePrompt ( )
54+ {
55+ var sandboxDirectory = CreateSandboxDirectory ( ) ;
56+ var prompt = new string ( 'p' , DuplexPromptCharacters ) ;
57+
58+ try
59+ {
60+ var invocation = OperatingSystem . IsWindows ( )
61+ ? new CodexProcessInvocation (
62+ WindowsPowerShellPath ,
63+ [
64+ WindowsNoProfileFlag ,
65+ WindowsNonInteractiveFlag ,
66+ WindowsCommandFlag ,
67+ WindowsDuplexStdinCommandTemplate . Replace (
68+ DuplexPressureTemplatePlaceholder ,
69+ DuplexPipePressureCharacters . ToString ( CultureInfo . InvariantCulture ) ,
70+ StringComparison . Ordinal ) ,
71+ ] ,
72+ CreateProcessEnvironment ( ) ,
73+ prompt )
74+ : CreateShellScript (
75+ sandboxDirectory ,
76+ "duplex-stdin" ,
77+ PosixDuplexStdinScriptTemplate . Replace (
78+ DuplexPressureTemplatePlaceholder ,
79+ DuplexPipePressureCharacters . ToString ( CultureInfo . InvariantCulture ) ,
80+ StringComparison . Ordinal ) )
81+ . Invocation with
82+ { Input = prompt } ;
83+ invocation = invocation with { MaximumProcessOutputCharacters = DuplexMaximumProcessOutputCharacters } ;
84+ var runner = new DefaultCodexProcessRunner ( ) ;
85+
86+ var lines = await DrainToListAsync ( runner . RunAsync ( invocation , NullLogger . Instance , CancellationToken . None ) ) ;
87+
88+ await Assert . That ( lines ) . Contains ( prompt ) ;
89+ await Assert . That ( lines ) . Contains ( new string ( 'o' , DuplexPipePressureCharacters ) ) ;
90+ await Assert . That ( lines ) . Contains ( $ "stdin-length:{ prompt . Length } ") ;
91+ await Assert . That ( lines ) . Contains ( "stdin-eof" ) ;
92+ }
93+ finally
94+ {
95+ Directory . Delete ( sandboxDirectory , recursive : true ) ;
96+ }
97+ }
98+
99+ [ Test ]
100+ public async Task CancellationWithDescendantHoldingStderrSurfacesUnconfirmedCleanup ( )
101+ {
102+ if ( ! OperatingSystem . IsLinux ( ) )
103+ {
104+ Skip . Test ( LinuxFixtureSkipReason ) ;
105+ return ;
106+ }
107+
108+ using var cancellation = new CancellationTokenSource ( ) ;
109+ var standardErrorReaderCompleted = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
110+ var standardOutputReadCompleted = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
111+ var invocation = new CodexProcessInvocation (
112+ PosixShellPath ,
113+ [ PosixShellCommandFlag , DescendantHoldingStderrWhileParentRunsScript ] ,
114+ CreateProcessEnvironment ( ) ,
115+ string . Empty )
116+ {
117+ ProcessTerminationTimeout = TimeSpan . FromMilliseconds ( 250 ) ,
118+ StandardErrorReaderCompleted = ( ) => standardErrorReaderCompleted . TrySetResult ( ) ,
119+ StandardOutputReadCompleted = ( ) => standardOutputReadCompleted . TrySetResult ( ) ,
120+ } ;
121+ var runner = new DefaultCodexProcessRunner ( ) ;
122+ await using var enumerator = runner . RunAsync ( invocation , NullLogger . Instance , cancellation . Token )
123+ . GetAsyncEnumerator ( cancellation . Token ) ;
124+ var childProcessId = 0 ;
125+
126+ try
127+ {
128+ await Assert . That ( await enumerator . MoveNextAsync ( ) ) . IsTrue ( ) ;
129+ childProcessId = int . Parse ( enumerator . Current , CultureInfo . InvariantCulture ) ;
130+ cancellation . Cancel ( ) ;
131+
132+ var action = async ( ) => await enumerator . MoveNextAsync ( ) . AsTask ( ) . WaitAsync ( TimeSpan . FromSeconds ( 3 ) ) ;
133+ var exception = await Assert . That ( action ) . ThrowsException ( ) ;
134+
135+ await Assert . That ( exception ) . IsTypeOf < InvalidOperationException > ( ) ;
136+ await Assert . That ( exception ! . Message ) . Contains ( StderrClosureFailure ) ;
137+ await standardOutputReadCompleted . Task . WaitAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
138+ await standardErrorReaderCompleted . Task . WaitAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
139+ }
140+ finally
141+ {
142+ try
143+ {
144+ using var child = Process . GetProcessById ( childProcessId ) ;
145+ child . Kill ( entireProcessTree : true ) ;
146+ await child . WaitForExitAsync ( ) . WaitAsync ( TimeSpan . FromSeconds ( 5 ) ) ;
147+ }
148+ catch ( ArgumentException )
149+ {
150+ // The detached fixture child already exited.
151+ }
152+ }
153+ }
154+
43155 [ Test ]
44156 public async Task DefaultProcessRunner_PreservesMultilineOutputWithinConfiguredBudget ( )
45157 {
@@ -87,19 +199,19 @@ public async Task DefaultProcessRunner_RejectsAggregateMultilineOutputAboveConfi
87199 [ Test ]
88200 public async Task DefaultProcessRunner_StandardErrorPressureStopsQuietRootWithinBound ( )
89201 {
202+ var stopwatch = new Stopwatch ( ) ;
90203 var invocation = CreateOutputInvocation (
91204 OperatingSystem . IsWindows ( ) ? WindowsStandardErrorPressureCommand : PosixStandardErrorPressureCommand ,
92- TimeSpan . FromSeconds ( 2 ) , SmallOutputLimitCharacters ) ;
205+ TimeSpan . FromSeconds ( 2 ) , SmallOutputLimitCharacters , stopwatch . Start ) ;
93206 var runner = new DefaultCodexProcessRunner ( ) ;
94- var stopwatch = Stopwatch . StartNew ( ) ;
95207 var action = async ( ) => await DrainToListAsync ( runner . RunAsync ( invocation , NullLogger . Instance , CancellationToken . None ) ) ;
96208
97209 var exception = await Assert . That ( action ) . ThrowsException ( ) ;
98210 stopwatch . Stop ( ) ;
99211
100212 await Assert . That ( exception ) . IsTypeOf < InvalidOperationException > ( ) ;
101213 await Assert . That ( exception ! . Message ) . Contains ( ProcessOutputLimitMessage ) ;
102- await Assert . That ( stopwatch . Elapsed < TimeSpan . FromSeconds ( 5 ) ) . IsTrue ( ) ;
214+ await Assert . That ( stopwatch . Elapsed < ProcessOutputCleanupAssertionBound ) . IsTrue ( ) ;
103215 }
104216
105217 [ Test ]
@@ -679,7 +791,8 @@ private static string CreateSandboxDirectory()
679791 private static CodexProcessInvocation CreateOutputInvocation (
680792 string command ,
681793 TimeSpan processTerminationTimeout ,
682- int maximumProcessOutputCharacters )
794+ int maximumProcessOutputCharacters ,
795+ Action ? standardErrorOutputLimitExceeded = null )
683796 {
684797 return new CodexProcessInvocation (
685798 OperatingSystem . IsWindows ( ) ? WindowsPowerShellPath : PosixShellPath ,
@@ -691,6 +804,7 @@ private static CodexProcessInvocation CreateOutputInvocation(
691804 {
692805 ProcessTerminationTimeout = processTerminationTimeout ,
693806 MaximumProcessOutputCharacters = maximumProcessOutputCharacters ,
807+ StandardErrorOutputLimitExceeded = standardErrorOutputLimitExceeded ,
694808 } ;
695809 }
696810
0 commit comments