Skip to content

Commit 403f699

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
ssh/test: avoid leaking a net.UnixConn in server.TryDialWithAddr
For golang/go#64959. Change-Id: I2153166f4960058cdc2b82ae34ca250dcc6ba1c6 Cq-Include-Trybots: luci.golang.try:x_crypto-gotip-linux-amd64-longtest,x_crypto-gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/crypto/+/554062 Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 055043d commit 403f699

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

ssh/test/test_unix_test.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.Client, error) {
178178

179179
// addr is the user specified host:port. While we don't actually dial it,
180180
// we need to know this for host key matching
181-
func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Client, error) {
181+
func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (client *ssh.Client, err error) {
182182
sshd, err := exec.LookPath("sshd")
183183
if err != nil {
184184
s.t.Skipf("skipping test: %v", err)
@@ -188,13 +188,26 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
188188
if err != nil {
189189
s.t.Fatalf("unixConnection: %v", err)
190190
}
191+
defer func() {
192+
// Close c2 after we've started the sshd command so that it won't prevent c1
193+
// from returning EOF when the sshd command exits.
194+
c2.Close()
195+
196+
// Leave c1 open if we're returning a client that wraps it.
197+
// (The client is responsible for closing it.)
198+
// Otherwise, close it to free up the socket.
199+
if client == nil {
200+
c1.Close()
201+
}
202+
}()
191203

192-
cmd := testenv.Command(s.t, sshd, "-f", s.configfile, "-i", "-e")
193204
f, err := c2.File()
194205
if err != nil {
195206
s.t.Fatalf("UnixConn.File: %v", err)
196207
}
197208
defer f.Close()
209+
210+
cmd := testenv.Command(s.t, sshd, "-f", s.configfile, "-i", "-e")
198211
cmd.Stdin = f
199212
cmd.Stdout = f
200213
cmd.Stderr = new(bytes.Buffer)
@@ -223,7 +236,7 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
223236
// processes are killed too.
224237
cmd.Process.Signal(os.Interrupt)
225238
cmd.Wait()
226-
if s.t.Failed() {
239+
if s.t.Failed() || testing.Verbose() {
227240
// log any output from sshd process
228241
s.t.Logf("sshd:\n%s", cmd.Stderr)
229242
}

0 commit comments

Comments
 (0)