javascript icon indicating copy to clipboard operation
javascript copied to clipboard

k8s log request hangs on the stream

Open preemoDez opened this issue 3 years ago • 8 comments

Describe the bug After creating a streaming request with k8s Log.log({follow:true}) the code hangs waiting for the data from the log stream. The reason is not clear. I expect this is related to networking issues, perhaps dropping connections, etc.

** Client Version ** 0.18.1

** Server Version ** 1.27.3-gke.1700

To Reproduce Steps to reproduce the behavior:

This is an intermittent issue. It occurs more frequently during the long-running job. I suspect this is related to network issues in the GKE cluster.

Expected behavior Log request should be reliable and close the underlying stream in case of any network issues, dropping or closing connections from either the server or the client side. It should never hang.

My initial expectation, when I saw this API as a new TS developer:

Ideally it could even implement a retry mechanism, to reconnect in case of a connection failure, so that one call takes care of the whole streaming.

** Example Code** logStream is a new passthrough stream.

      log.debug(`Creating log stream request for pod: ${podName}`);
      const streamReq = await k8sLog.log(
        K8S_JOB_NAMESPACE,
        podName,
        K8S_JOB_CONTAINER,
        logStream,
        {
          follow: true,
        },
      );

      log.debug(`Starting to stream logs for pod: ${podName}`);
      await this.handleLogStream({
        log,
        logStream: wrapStreamAsLineStream(logStream),
        userLog,
      });

handleLogStream contains the loop, where the code hangs:

for await (const logLine of logStream) {
}

Environment (please complete the following information):

  • OS: Container-Optimized OS from Google
  • NodeJS Version: v18.18.2
  • Cloud runtime: GKE cluster

Additional context We added the following part to the request returned by the log:

      // TS k8s client log method does not add an error handler to the stream request.
      streamReq.on('error', (error) => {
        log.error(
          `Error while streaming logs for pod: ${podName}. Error: ${stringify(error)}. Manually ending stream...`,
        );
        logStream.end();
      });

From my research it looks like this is a necessary step, which should handle all networking issues and prevent the hang. I did not find it in the k8s client code. It would be nice if that was added to the client method.

If that does not help, I would implement a timeout mechanism, to close the stream and abort the request if I haven't received a message for some time.

I am not a networking expert but it would be nice, if that functionality was part of the log method itself - disconnect after passed timeout or keep the connection alive and disconnect automatically in case of failure detection?

preemoDez avatar Oct 30 '23 23:10 preemoDez

You are setting follow to true which indicates that you want to continue streaming the logs forever. The Log client only returns from the await when the response completes, but because follow is true, the response will never complete.

You want to set up an async data handler (e.g. logStream.on('data', ...) before you make the call to log if you want to use follow: true

brendandburns avatar Oct 31 '23 19:10 brendandburns

Hi @brendandburns, thanks for the response! Let me clarify my use case.

The log streams from the k8s job, which is completed. The stream ends and the response completes, but the code hangs in the middle of the request does not stream more data, and does not end.

I would like to keep the single log request throughout the whole job, but I noticed that it is difficult, due to the log rotation or other networking events along the way. That's fine, it's a stretch goal, I can reconnect. The solution should not hang.

Why should I set up an async data handler? I don't need any custom behavior, the default streaming works for me.

preemoDez avatar Oct 31 '23 19:10 preemoDez

The reason that you want an async data handler is that without it, you're going to fill up buffers (network, etc) that sit in the middle between the client code and the server, because you aren't reading data that is being sent.

The network buffers in various places are finite, and once they fill up, the network traffic will stall, which I suspect is what is causing the hang.

Any time that you are waiting for a network call to complete, and that call is returning potentially large amounts of data, you need to be reading the data asyncronously with waiting for the call to complete.

It's not hard to do this, just add the data to a buffer in your data handler and then you can read through each line like you are already doing.

I do not believe that this is a bug in the client, but rather the way the client is being used. If you determine that it is a bug, we will need a clean reproduction of the bug in order to debug further.

brendandburns avatar Nov 02 '23 17:11 brendandburns

Ok, thanks for the answer. I misunderstood your previous response. I think this is handled already by the following code:

export const wrapStreamAsLineStream = (stream: Readable): AsyncIterable<string> => {
  stream.setEncoding('utf8');

  const lineStream = split();
  pipeWithErrors(stream, lineStream);
  return lineStream;
};
export const pipeWithErrors = (src: Readable, dest: Writable) => {
  src.on('error', function (err) {
    dest.emit('error', err);
  });

  return src.pipe(dest);
};

And the split function.

preemoDez avatar Nov 02 '23 18:11 preemoDez

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Jan 31 '24 20:01 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Mar 01 '24 21:03 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Mar 31 '24 22:03 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Mar 31 '24 22:03 k8s-ci-robot