X Tutup
Skip to content

Commit 9398022

Browse files
committed
Close properly SSH clients created by NativeClient
Signed-off-by: Lucas BEE <pouulet@gmail.com>
1 parent 983bdb3 commit 9398022

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

libmachine/ssh/client.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type NativeClient struct {
4646
Hostname string
4747
Port int
4848
openSession *ssh.Session
49+
openClient *ssh.Client
4950
}
5051

5152
type Auth struct {
@@ -156,43 +157,49 @@ func NewNativeConfig(user string, auth *Auth) (ssh.ClientConfig, error) {
156157
}
157158

158159
func (client *NativeClient) dialSuccess() bool {
159-
if _, err := ssh.Dial("tcp", net.JoinHostPort(client.Hostname, strconv.Itoa(client.Port)), &client.Config); err != nil {
160+
conn, err := ssh.Dial("tcp", net.JoinHostPort(client.Hostname, strconv.Itoa(client.Port)), &client.Config)
161+
if err != nil {
160162
log.Debugf("Error dialing TCP: %s", err)
161163
return false
162164
}
165+
closeConn(conn)
163166
return true
164167
}
165168

166-
func (client *NativeClient) session(command string) (*ssh.Session, error) {
169+
func (client *NativeClient) session(command string) (*ssh.Client, *ssh.Session, error) {
167170
if err := mcnutils.WaitFor(client.dialSuccess); err != nil {
168-
return nil, fmt.Errorf("Error attempting SSH client dial: %s", err)
171+
return nil, nil, fmt.Errorf("Error attempting SSH client dial: %s", err)
169172
}
170173

171174
conn, err := ssh.Dial("tcp", net.JoinHostPort(client.Hostname, strconv.Itoa(client.Port)), &client.Config)
172175
if err != nil {
173-
return nil, fmt.Errorf("Mysterious error dialing TCP for SSH (we already succeeded at least once) : %s", err)
176+
return nil, nil, fmt.Errorf("Mysterious error dialing TCP for SSH (we already succeeded at least once) : %s", err)
174177
}
178+
session, err := conn.NewSession()
175179

176-
return conn.NewSession()
180+
return conn, session, err
177181
}
178182

179183
func (client *NativeClient) Output(command string) (string, error) {
180-
session, err := client.session(command)
184+
conn, session, err := client.session(command)
181185
if err != nil {
182186
return "", nil
183187
}
188+
defer closeConn(conn)
189+
defer session.Close()
184190

185191
output, err := session.CombinedOutput(command)
186-
defer session.Close()
187192

188193
return string(output), err
189194
}
190195

191196
func (client *NativeClient) OutputWithPty(command string) (string, error) {
192-
session, err := client.session(command)
197+
conn, session, err := client.session(command)
193198
if err != nil {
194199
return "", nil
195200
}
201+
defer closeConn(conn)
202+
defer session.Close()
196203

197204
fd := int(os.Stdin.Fd())
198205

@@ -214,13 +221,12 @@ func (client *NativeClient) OutputWithPty(command string) (string, error) {
214221
}
215222

216223
output, err := session.CombinedOutput(command)
217-
defer session.Close()
218224

219225
return string(output), err
220226
}
221227

222228
func (client *NativeClient) Start(command string) (io.ReadCloser, io.ReadCloser, error) {
223-
session, err := client.session(command)
229+
conn, session, err := client.session(command)
224230
if err != nil {
225231
return nil, nil, err
226232
}
@@ -237,15 +243,27 @@ func (client *NativeClient) Start(command string) (io.ReadCloser, io.ReadCloser,
237243
return nil, nil, err
238244
}
239245

246+
client.openClient = conn
240247
client.openSession = session
241248
return ioutil.NopCloser(stdout), ioutil.NopCloser(stderr), nil
242249
}
243250

244251
func (client *NativeClient) Wait() error {
245252
err := client.openSession.Wait()
253+
if err != nil {
254+
return err
255+
}
256+
246257
_ = client.openSession.Close()
258+
259+
err = client.openClient.Close()
260+
if err != nil {
261+
return err
262+
}
263+
247264
client.openSession = nil
248-
return err
265+
client.openClient = nil
266+
return nil
249267
}
250268

251269
func (client *NativeClient) Shell(args ...string) error {
@@ -256,6 +274,7 @@ func (client *NativeClient) Shell(args ...string) error {
256274
if err != nil {
257275
return err
258276
}
277+
defer closeConn(conn)
259278

260279
session, err := conn.NewSession()
261280
if err != nil {
@@ -414,3 +433,10 @@ func (client *ExternalClient) Wait() error {
414433
client.cmd = nil
415434
return err
416435
}
436+
437+
func closeConn(c io.Closer) {
438+
err := c.Close()
439+
if err != nil {
440+
log.Debugf("Error closing SSH Client: %s", err)
441+
}
442+
}

0 commit comments

Comments
 (0)
X Tutup