@@ -558,46 +558,27 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
558
558
throw new KubeConfigException ( $ "external exec failed due to: { ex . Message } ") ;
559
559
}
560
560
561
- var stderrBuilder = new StringBuilder ( ) ;
562
561
563
562
var stdOutTask = process . StandardOutput . ReadToEndAsync ( ) ; // Assumes process exits
564
563
565
- var buffer = new char [ 1024 ] ;
564
+ var buffer = new char [ 4096 ] ;
565
+ var stdErrorTask = process . StandardError . ReadAsync ( buffer , 0 , buffer . Length ) ; // Assumes process will continue
566
566
567
- var stopwatch = Stopwatch . StartNew ( ) ;
567
+ var result = Task . WaitAny ( new Task [ ] { stdErrorTask , stdOutTask } , TimeSpan . FromSeconds ( 10 ) ) ;
568
568
569
- // Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
570
- while ( ! stdOutTask . IsCompleted && stopwatch . Elapsed < TimeSpan . FromSeconds ( 5 ) )
569
+ if ( result == - 1 )
571
570
{
572
- var readTask = process . StandardError . ReadAsync ( buffer , 0 , buffer . Length ) ;
573
-
574
- if ( readTask . Wait ( TimeSpan . FromMilliseconds ( 500 ) ) )
575
- {
576
- var bytesRead = readTask . Result ;
577
- if ( bytesRead == 0 )
578
- {
579
- break ; // end of stream reached
580
- }
581
-
582
- stderrBuilder . Append ( buffer , 0 , bytesRead ) ;
583
- }
584
- else
585
- {
586
- // timeout occurred
587
- break ;
588
- }
571
+ throw new KubeConfigException ( "external exec failed due to timeout" ) ;
589
572
}
590
-
591
- var stdError = stderrBuilder . ToString ( ) ;
592
-
593
- if ( ! string . IsNullOrWhiteSpace ( stdError ) )
573
+ else if ( result == 0 )
594
574
{
595
- throw new KubeConfigException ( $ "external exec failed due to: { stdError } " ) ;
596
- }
575
+ var stderrBuilder = new StringBuilder ( ) ;
576
+ stderrBuilder . Append ( buffer , 0 , stdErrorTask . Result ) ;
597
577
598
- if ( ! stdOutTask . IsCompleted )
599
- {
600
- throw new KubeConfigException ( "external exec failed due to timeout" ) ;
578
+ if ( stderrBuilder . Length > 0 )
579
+ {
580
+ throw new KubeConfigException ( $ "external exec failed due to: { stderrBuilder } ") ;
581
+ }
601
582
}
602
583
603
584
try
0 commit comments