From b486354a9cd4e2d2c646ede02dc24147dfe4643d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 26 Dec 2016 16:29:32 -0800 Subject: [PATCH] communicator/ssh: Disconnect() should also kill the actual connection --- communicator/ssh/communicator.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index 32a881c04..36b698a9a 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -42,6 +42,8 @@ type Communicator struct { config *sshConfig conn net.Conn address string + + lock sync.Mutex } type sshConfig struct { @@ -96,6 +98,10 @@ func New(s *terraform.InstanceState) (*Communicator, error) { // Connect implementation of communicator.Communicator interface func (c *Communicator) Connect(o terraform.UIOutput) (err error) { + // Grab a lock so we can modify our internal attributes + c.lock.Lock() + defer c.lock.Unlock() + if c.conn != nil { c.conn.Close() } @@ -190,8 +196,19 @@ func (c *Communicator) Connect(o terraform.UIOutput) (err error) { // Disconnect implementation of communicator.Communicator interface func (c *Communicator) Disconnect() error { + c.lock.Lock() + defer c.lock.Unlock() + if c.config.sshAgent != nil { - return c.config.sshAgent.Close() + if err := c.config.sshAgent.Close(); err != nil { + return err + } + } + + if c.conn != nil { + conn := c.conn + c.conn = nil + return conn.Close() } return nil