Skip to content

Commit cd7461e

Browse files
holimancp-wjhan
authored andcommitted
build: close sftp connection when done (ethereum#24593)
1 parent 31dc97f commit cd7461e

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

internal/build/util.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package build
1818

1919
import (
20+
"bufio"
2021
"bytes"
2122
"flag"
2223
"fmt"
@@ -116,7 +117,6 @@ func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x
116117
// the form sftp://[user@]host[:port].
117118
func UploadSFTP(identityFile, host, dir string, files []string) error {
118119
sftp := exec.Command("sftp")
119-
sftp.Stdout = os.Stdout
120120
sftp.Stderr = os.Stderr
121121
if identityFile != "" {
122122
sftp.Args = append(sftp.Args, "-i", identityFile)
@@ -131,6 +131,10 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
131131
if err != nil {
132132
return fmt.Errorf("can't create stdin pipe for sftp: %v", err)
133133
}
134+
stdout, err := sftp.StdoutPipe()
135+
if err != nil {
136+
return fmt.Errorf("can't create stdout pipe for sftp: %v", err)
137+
}
134138
if err := sftp.Start(); err != nil {
135139
return err
136140
}
@@ -139,24 +143,34 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
139143
fmt.Fprintln(in, "put", f, path.Join(dir, filepath.Base(f)))
140144
}
141145
fmt.Fprintln(in, "exit")
142-
// Avoid travis timout after 10m of inactivity by printing something
143-
// every 8 minutes.
144-
done := make(chan bool)
146+
// Some issue with the PPA sftp server makes it so the server does not
147+
// respond properly to a 'bye', 'exit' or 'quit' from the client.
148+
// To work around that, we check the output, and when we see the client
149+
// exit command, we do a hard exit.
150+
// See
151+
// https://github.com/kolban-google/sftp-gcs/issues/23
152+
// https://github.com/mscdex/ssh2/pull/1111
153+
aborted := false
145154
go func() {
146-
for {
147-
select {
148-
case <-time.After(8 * time.Minute):
149-
fmt.Println("keepalive log")
150-
continue
151-
case <-done:
152-
return
155+
scanner := bufio.NewScanner(stdout)
156+
for scanner.Scan() {
157+
txt := scanner.Text()
158+
fmt.Println(txt)
159+
if txt == "sftp> exit" {
160+
// Give it .5 seconds to exit (server might be fixed), then
161+
// hard kill it from the outside
162+
time.Sleep(500 * time.Millisecond)
163+
aborted = true
164+
sftp.Process.Kill()
153165
}
154-
155166
}
156167
}()
157168
stdin.Close()
158-
defer close(done)
159-
return sftp.Wait()
169+
err = sftp.Wait()
170+
if aborted {
171+
return nil
172+
}
173+
return err
160174
}
161175

162176
// FindMainPackages finds all 'main' packages in the given directory and returns their

0 commit comments

Comments
 (0)