From 2800b83f2ff4d248f221470fdb50237b2c7e5ddf Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 13 Dec 2016 12:50:27 -0800 Subject: [PATCH] Add operation lock for containers Signed-off-by: Mrunal Patel --- oci/oci.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/oci/oci.go b/oci/oci.go index af97b9df..ba7dada4 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -143,6 +143,8 @@ func (r *Runtime) CreateContainer(c *Container) error { // StartContainer starts a container. func (r *Runtime) StartContainer(c *Container) error { + c.opLock.Lock() + defer c.opLock.Unlock() if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name); err != nil { return err } @@ -265,6 +267,8 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp // StopContainer stops a container. func (r *Runtime) StopContainer(c *Container) error { + c.opLock.Lock() + defer c.opLock.Unlock() if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil { return err } @@ -291,13 +295,15 @@ func (r *Runtime) StopContainer(c *Container) error { // DeleteContainer deletes a container. func (r *Runtime) DeleteContainer(c *Container) error { + c.opLock.Lock() + defer c.opLock.Unlock() return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "delete", c.name) } // UpdateStatus refreshes the status of the container. func (r *Runtime) UpdateStatus(c *Container) error { - c.stateLock.Lock() - defer c.stateLock.Unlock() + c.opLock.Lock() + defer c.opLock.Unlock() out, err := exec.Command(r.path, "state", c.name).CombinedOutput() if err != nil { return fmt.Errorf("error getting container state for %s: %s: %q", c.name, err, out) @@ -331,8 +337,8 @@ func (r *Runtime) UpdateStatus(c *Container) error { // ContainerStatus returns the state of a container. func (r *Runtime) ContainerStatus(c *Container) *ContainerState { - c.stateLock.Lock() - defer c.stateLock.Unlock() + c.opLock.Lock() + defer c.opLock.Unlock() return c.state } @@ -350,7 +356,7 @@ type Container struct { terminal bool state *ContainerState metadata *pb.ContainerMetadata - stateLock sync.Mutex + opLock sync.Mutex } // ContainerState represents the status of a container.