Skip to content

Conversation

xinyanmsft
Copy link
Contributor

In StreamDemuxer, if the buffer is created before connection is established, the port bytes are not removed when the bytes are delivering to the client. Repro code looks like (the key is to call remoteStreams.Start AFTER GetStream):

var ws = await kubernetesClient.WebSocketNamespacedPodPortForwardAsync (... )
var remoteStreams = new StreamDemuxer(ws);
var stream = remoteStreams.GetStream(0, 0);
remoteStreams.Start();

This change filters out the port bytes which are the 2nd and 3rd bytes sent in all cases.

when multiple call to GetStream happens around the same time, on the
same inputIndex, a race condition will cause this.buffers.Add() to throw
exception.
In StreamDemuxer, if the buffer is created before connection is established, the port bytes are not removed when
the bytes are delivering to the client. Repro code looks like (the key is to call remoteStreams.Start AFTER
GetStream):

  var ws = await kubernetesClient.WebSocketNamespacedPodPortForwardAsync (... )
  var remoteStreams = new StreamDemuxer(ws);
  var stream = remoteStreams.GetStream(0, 0);
  remoteStreams.Start();

This change filters out the port bytes which are the 2nd and 3rd bytes sent in all cases.
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 28, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: xinyanmsft
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: krabhishek8260

If they are not already assigned, you can assign the PR to them by writing /assign @krabhishek8260 in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jan 28, 2019
@xinyanmsft xinyanmsft changed the title bug fix - remote port bytes during port forwarding bug fix - remove port bytes during port forwarding Jan 28, 2019
add an enum StreamType to StreamDemuxer so it knows whether / how
the data stream should be handled.
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jan 30, 2019
@@ -199,6 +204,13 @@ protected async Task RunLoop(CancellationToken cancellationToken)

var streamIndex = buffer[0];
var extraByteCount = 1;
byteCount += result.Count - extraByteCount;
if (this.streamType == StreamType.PortForward && byteCount <= 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will work correctly, what if byteCount becomes greater than 2 immediately, you won't strip the first two bytes.

I think the right thing to do is to have a boolean skippedPort outside the loop and then clip the two bytes at the front if !skippedPort and then set skippedPort to true...

Eitherway, I think this deserves a unit test to make sure it's working.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's another pass. there was a mistake in my previous change where I didn't handle StreamDemuxer with multiple streams. Now there is a dictionary that holds the skipped bytes for each stream.
it turns out there is 0 StreamDemuxer tests. add StreamDemuxerTests that covers send / receive for both command and port forwarding. Added WebSocketMock as a helper.

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 31, 2019
@brendandburns
Copy link
Contributor

Awesome, thanks!

@brendandburns brendandburns merged commit 5adf9a3 into kubernetes-client:master Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants