Skip to content

Commit 7508ffe

Browse files
fix: Exec hangs (#1267)
* fix: exec hanging * fix: exec hang * chore: increase buffer * chore: add StandardOutput timeout * chore: switch to event based * chore: simplify logic * chore: fix logic * fix: remove async * chore: simplify logic * fix: revert to non Event based logic * fix: simplify logic * fix: switch to GetAwaiter().GetResult() * feat: externalize exec timeout * feat: send errors to event * chore: formatting * fix: add null check for event handler * feat: switch to TimeSpan
1 parent 2a37ba8 commit 7508ffe

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

+21-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public partial class KubernetesClientConfiguration
2828
// For testing
2929
internal static string KubeConfigEnvironmentVariable { get; set; } = "KUBECONFIG";
3030

31+
/// <summary>
32+
/// Exec process timeout
33+
/// </summary>
34+
public static TimeSpan ExecTimeout { get; set; } = TimeSpan.FromMinutes(2);
35+
36+
/// <summary>
37+
/// Exec process Standard Errors
38+
/// </summary>
39+
public static event EventHandler<DataReceivedEventArgs> ExecStdError;
40+
3141
/// <summary>
3242
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from default locations
3343
/// If the KUBECONFIG environment variable is set, then that will be used.
@@ -552,25 +562,25 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
552562
try
553563
{
554564
process.Start();
565+
if (ExecStdError != null)
566+
{
567+
process.ErrorDataReceived += (s, e) => ExecStdError.Invoke(s, e);
568+
process.BeginErrorReadLine();
569+
}
555570
}
556571
catch (Exception ex)
557572
{
558573
throw new KubeConfigException($"external exec failed due to: {ex.Message}");
559574
}
560575

561-
var stdout = process.StandardOutput.ReadToEnd();
562-
var stderr = process.StandardError.ReadToEnd();
563-
if (string.IsNullOrWhiteSpace(stderr) == false)
564-
{
565-
throw new KubeConfigException($"external exec failed due to: {stderr}");
566-
}
567-
568-
// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
569-
process.WaitForExit(5);
570-
571576
try
572577
{
573-
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdout);
578+
if (!process.WaitForExit((int)(ExecTimeout.TotalMilliseconds)))
579+
{
580+
throw new KubeConfigException("external exec failed due to timeout");
581+
}
582+
583+
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(process.StandardOutput.ReadToEnd());
574584
if (responseObject == null || responseObject.ApiVersion != config.ApiVersion)
575585
{
576586
throw new KubeConfigException(

0 commit comments

Comments
 (0)