Skip to content

Commit

Permalink
fix "early EOF" issue when using with git
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jul 27, 2024
1 parent fdbfa53 commit 4ef5969
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions tssh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,10 @@ func sshStart(args *sshArgs) error {
if args.Background {
_ = ss.client.Wait()
}

// wait for the output to be read by the parent process
if !isTerminal {
outputWaitGroup.Wait()
}
return nil
}
26 changes: 17 additions & 9 deletions tssh/trzsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ import (
"os"
"runtime"
"strings"
"sync"
"time"

"github.com/trzsz/trzsz-go/trzsz"
)

var outputWaitGroup sync.WaitGroup

func writeAll(dst io.Writer, data []byte) error {
m := 0
l := len(data)
Expand All @@ -53,6 +56,11 @@ func writeAll(dst io.Writer, data []byte) error {
func wrapStdIO(serverIn io.WriteCloser, serverOut io.Reader, serverErr io.Reader, tty bool) {
win := runtime.GOOS == "windows"
forwardIO := func(reader io.Reader, writer io.WriteCloser, input bool) {
done := true
if !input {
done = false
outputWaitGroup.Add(1)
}
defer writer.Close()
buffer := make([]byte, 32*1024)
for {
Expand All @@ -72,20 +80,20 @@ func wrapStdIO(serverIn io.WriteCloser, serverOut io.Reader, serverErr io.Reader
}
}
if err == io.EOF {
if win && tty && input {
if win && isTerminal && tty && input {
_, _ = writer.Write([]byte{0x1A}) // ctrl + z
continue
}
if !input {
continue // ignore output EOF
if input {
return // input EOF
}
// delay and close
for {
time.Sleep(time.Second)
if lastTime := lastServerAliveTime.Load(); lastTime != nil && time.Since(*lastTime) > 2*time.Second {
return
}
// ignore output EOF
if !done {
outputWaitGroup.Done()
done = true
}
time.Sleep(100 * time.Millisecond)
continue
}
if err != nil {
return
Expand Down

0 comments on commit 4ef5969

Please sign in to comment.