Skip to content

Commit

Permalink
Fix interrupted pending writes on socket write shutdown from eager close
Browse files Browse the repository at this point in the history
Some channel/connection implementations may signal EOF to parallel
readers before tasks related to the CloseWrite (shutdown) have
completed progressing. This creates the potential for a race with
a parallel Close(), leading to a premature abort of certain activies
(cancelling the send of buffered data).

This change ensures that the two goroutines copying each direction
of the stream wait until CloseWrite has completed in both directions
before fully closing.

Signed-off-by: Jason T. Greene <[email protected]>
  • Loading branch information
n1hility authored and cfergeau committed Dec 5, 2022
1 parent 19b9c49 commit 1382207
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/sshclient/ssh_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"runtime"
"strings"
"sync"
"time"

"github.com/containers/gvisor-tap-vsock/pkg/fs"
Expand Down Expand Up @@ -228,14 +229,22 @@ func acceptConnection(ctx context.Context, listener net.Listener, bastion *Basti
return nil // eat
}

go forward(src, dest)
go forward(dest, src)
complete := new(sync.WaitGroup)
complete.Add(2)
go forward(src, dest, complete)
go forward(dest, src, complete)

go func() {
complete.Wait()
src.Close()
dest.Close()
}()

return nil
}

func forward(src io.ReadCloser, dest CloseWriteStream) {
defer src.Close()
func forward(src io.ReadCloser, dest CloseWriteStream, complete *sync.WaitGroup) {
defer complete.Done()
_, _ = io.Copy(dest, src)

// Trigger an EOF on the other end
Expand Down

0 comments on commit 1382207

Please sign in to comment.