Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exec hangs #1267

Merged
merged 18 commits into from
May 1, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace k8s
Expand Down Expand Up @@ -557,13 +558,38 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
throw new KubeConfigException($"external exec failed due to: {ex.Message}");
}

var stdout = process.StandardOutput.ReadToEnd();
var stderr = process.StandardError.ReadToEnd();
if (string.IsNullOrWhiteSpace(stderr) == false)
var sb = new StringBuilder();
var buffer = new char[1024];
while (true)
{
var readTask = process.StandardError.ReadAsync(buffer, 0, buffer.Length);

if (readTask.Wait(TimeSpan.FromSeconds(2)))
IvanJosipovic marked this conversation as resolved.
Show resolved Hide resolved
{
var bytesRead = readTask.Result;
if (bytesRead == 0)
{
break; // end of stream reached
}

sb.Append(buffer, 0, bytesRead);
}
else
{
// timeout occurred
break;
}
}

var stderr = sb.ToString();

if (!string.IsNullOrWhiteSpace(stderr))
{
throw new KubeConfigException($"external exec failed due to: {stderr}");
}

var stdout = process.StandardOutput.ReadToEnd();

// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
process.WaitForExit(5);
IvanJosipovic marked this conversation as resolved.
Show resolved Hide resolved

Expand Down