Skip to content

[ssh-gateway] fix missing output when running simple command #18366

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

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions components/ws-proxy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/supervisor/api v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/ws-manager/api v0.0.0-00010101000000-000000000000
github.com/gitpod-io/golang-crypto v0.0.0-20220823040820-b59f56dfbab3
github.com/gitpod-io/golang-crypto v0.0.0-20230731181530-3ce7221eee49
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/google/go-cmp v0.5.9
github.com/gorilla/handlers v1.5.1
Expand Down Expand Up @@ -73,9 +73,9 @@ require (
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
18 changes: 10 additions & 8 deletions components/ws-proxy/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 27 additions & 14 deletions components/ws-proxy/pkg/sshproxy/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
targetChan, targetReqs, err := targetConn.OpenChannel(originChannel.ChannelType(), originChannel.ExtraData())
if err != nil {
log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Error("open target channel error")
originChannel.Reject(ssh.ConnectionFailed, "open target channel error")
_ = originChannel.Reject(ssh.ConnectionFailed, "open target channel error")
return
}
defer targetChan.Close()
Expand Down Expand Up @@ -53,32 +53,46 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
close(maskedReqs)
}()

originChannelWg := sync.WaitGroup{}
originChannelWg.Add(3)
targetChannelWg := sync.WaitGroup{}
targetChannelWg.Add(3)

wg := sync.WaitGroup{}
wg.Add(2)

go func() {
io.Copy(targetChan, originChan)
targetChan.CloseWrite()
defer wg.Done()
_, _ = io.Copy(targetChan, originChan)
targetChannelWg.Done()
targetChannelWg.Wait()
Copy link
Member

Choose a reason for hiding this comment

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

I wonder should not we wait for stderr/stdout in both directions? won't close on one end trigger close on another

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, they are closed together. It's just that due to the nature of go routines, it is possible that the last few bytes may not be guaranteed to be sent completely, so we need to wait for all of them to be sent before sending a close signal.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, I meant there we can simplify with one wg, which waits for both directions, but like that is fine as well 👍

_ = targetChan.Close()
}()

go func() {
io.Copy(originChan, targetChan)
originChan.CloseWrite()
defer wg.Done()
_, _ = io.Copy(originChan, targetChan)
originChannelWg.Done()
originChannelWg.Wait()
_ = originChan.Close()
}()

go func() {
io.Copy(targetChan.Stderr(), originChan.Stderr())
_, _ = io.Copy(targetChan.Stderr(), originChan.Stderr())
targetChannelWg.Done()
}()

go func() {
io.Copy(originChan.Stderr(), targetChan.Stderr())
_, _ = io.Copy(originChan.Stderr(), targetChan.Stderr())
originChannelWg.Done()
}()

wg := sync.WaitGroup{}
forward := func(sourceReqs <-chan *ssh.Request, targetChan ssh.Channel) {
defer wg.Done()
forward := func(sourceReqs <-chan *ssh.Request, targetChan ssh.Channel, channelWg *sync.WaitGroup) {
defer channelWg.Done()
for ctx.Err() == nil {
select {
case req, ok := <-sourceReqs:
if !ok {
targetChan.Close()
return
}
b, err := targetChan.SendRequest(req.Type, req.WantReply, req.Payload)
Expand All @@ -92,9 +106,8 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
}
}

wg.Add(2)
go forward(maskedReqs, targetChan)
go forward(targetReqs, originChan)
go forward(maskedReqs, targetChan, &targetChannelWg)
go forward(targetReqs, originChan, &originChannelWg)

wg.Wait()
log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Debug("session forward stop")
Expand Down
8 changes: 4 additions & 4 deletions install/installer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ require (
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
github.com/gitpod-io/gitpod/content-service v0.0.0-00010101000000-000000000000 // indirect
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000 // indirect
github.com/gitpod-io/golang-crypto v0.0.0-20220823040820-b59f56dfbab3 // indirect
github.com/gitpod-io/golang-crypto v0.0.0-20230731181530-3ce7221eee49 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-gorp/gorp/v3 v3.0.5 // indirect
github.com/go-logr/logr v1.2.4 // indirect
Expand Down Expand Up @@ -261,9 +261,9 @@ require (
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions install/installer/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.