diff --git a/commands/commands.go b/commands/commands.go index fb6f7c85e6..e35d164ce9 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -12,6 +12,7 @@ import ( "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/cert" "github.com/docker/machine/libmachine/crashreport" + "github.com/docker/machine/libmachine/drivers/rpc" "github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/mcnutils" @@ -116,6 +117,8 @@ func fatalOnError(command func(commandLine CommandLine, api libmachine.API) erro mcnutils.GithubAPIToken = api.GithubAPIToken ssh.SetDefaultClient(api.SSHClientType) + defer rpcdriver.CloseDrivers() + if err := command(&contextCommandLine{context}, api); err != nil { log.Fatal(err) } diff --git a/commands/create.go b/commands/create.go index 8a2cf65430..ab53bb5e62 100644 --- a/commands/create.go +++ b/commands/create.go @@ -321,12 +321,6 @@ func cmdCreateOuter(c CommandLine, api libmachine.API) error { driver = serialDriver.Driver } - if rpcd, ok := driver.(*rpcdriver.RPCClientDriver); ok { - if err := rpcd.Close(); err != nil { - return err - } - } - return c.Application().Run(os.Args) } diff --git a/libmachine/drivers/rpc/client_driver.go b/libmachine/drivers/rpc/client_driver.go index f3a61a5c42..54b3f35332 100644 --- a/libmachine/drivers/rpc/client_driver.go +++ b/libmachine/drivers/rpc/client_driver.go @@ -3,6 +3,7 @@ package rpcdriver import ( "fmt" "net/rpc" + "sync" "time" "github.com/docker/machine/libmachine/drivers" @@ -15,6 +16,8 @@ import ( var ( heartbeatInterval = 5 * time.Second + openedDrivers = []*RPCClientDriver{} + openedDriversLock = &sync.Mutex{} ) type RPCClientDriver struct { @@ -85,6 +88,17 @@ func NewInternalClient(rpcclient *rpc.Client) *InternalClient { } } +func CloseDrivers() { + openedDriversLock.Lock() + + for _, openedDriver := range openedDrivers { + openedDriver.close() + } + openedDrivers = []*RPCClientDriver{} + + openedDriversLock.Unlock() +} + func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, error) { mcnName := "" @@ -116,6 +130,10 @@ func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, heartbeatDoneCh: make(chan bool), } + openedDriversLock.Lock() + openedDrivers = append(openedDrivers, c) + openedDriversLock.Unlock() + var serverVersion int if err := c.Client.Call(GetVersionMethod, struct{}{}, &serverVersion); err != nil { // this is the first call we make to the server. We try to play nice with old pre 0.5.1 client, @@ -141,7 +159,7 @@ func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, case <-time.After(heartbeatInterval): if err := c.Client.Call(HeartbeatMethod, struct{}{}, nil); err != nil { log.Warnf("Error attempting heartbeat call to plugin server: %s", err) - c.Close() + c.close() return } } @@ -168,7 +186,7 @@ func (c *RPCClientDriver) UnmarshalJSON(data []byte) error { return c.SetConfigRaw(data) } -func (c *RPCClientDriver) Close() error { +func (c *RPCClientDriver) close() error { c.heartbeatDoneCh <- true close(c.heartbeatDoneCh)