@@ -178,7 +178,7 @@ func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.Client, error) {
178
178
179
179
// addr is the user specified host:port. While we don't actually dial it,
180
180
// 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 ) {
182
182
sshd , err := exec .LookPath ("sshd" )
183
183
if err != nil {
184
184
s .t .Skipf ("skipping test: %v" , err )
@@ -188,13 +188,26 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
188
188
if err != nil {
189
189
s .t .Fatalf ("unixConnection: %v" , err )
190
190
}
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
+ }()
191
203
192
- cmd := testenv .Command (s .t , sshd , "-f" , s .configfile , "-i" , "-e" )
193
204
f , err := c2 .File ()
194
205
if err != nil {
195
206
s .t .Fatalf ("UnixConn.File: %v" , err )
196
207
}
197
208
defer f .Close ()
209
+
210
+ cmd := testenv .Command (s .t , sshd , "-f" , s .configfile , "-i" , "-e" )
198
211
cmd .Stdin = f
199
212
cmd .Stdout = f
200
213
cmd .Stderr = new (bytes.Buffer )
@@ -223,7 +236,7 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
223
236
// processes are killed too.
224
237
cmd .Process .Signal (os .Interrupt )
225
238
cmd .Wait ()
226
- if s .t .Failed () {
239
+ if s .t .Failed () || testing . Verbose () {
227
240
// log any output from sshd process
228
241
s .t .Logf ("sshd:\n %s" , cmd .Stderr )
229
242
}
0 commit comments