summaryrefslogtreecommitdiff
path: root/libpod/container_api.go
diff options
context:
space:
mode:
authorMarco Vedovati <mvedovati@suse.com>2019-06-17 15:14:54 +0200
committerMarco Vedovati <mvedovati@suse.com>2019-06-26 10:17:29 +0200
commit4f56964d556a7379c09a903258fd44c2232a686a (patch)
tree57f2be6e11e5f413122172505c1b7661bae2a5e1 /libpod/container_api.go
parent6e9b490f5e2c84a903e6cc86440599e3ea7c63d2 (diff)
downloadpodman-4f56964d556a7379c09a903258fd44c2232a686a.tar.gz
podman-4f56964d556a7379c09a903258fd44c2232a686a.tar.bz2
podman-4f56964d556a7379c09a903258fd44c2232a686a.zip
libpod: fix hang on container start and attach
When a container is attached upon start, the WaitGroup counter may never be decremented if an error is raised before start, causing the caller to hang. Synchronize with the start & attach goroutine using a channel, to be able to detect failures before start. Signed-off-by: Marco Vedovati <mvedovati@suse.com>
Diffstat (limited to 'libpod/container_api.go')
-rw-r--r--libpod/container_api.go17
1 files changed, 10 insertions, 7 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go
index b8c339a39..d5d8e9906 100644
--- a/libpod/container_api.go
+++ b/libpod/container_api.go
@@ -7,7 +7,6 @@ import (
"io/ioutil"
"os"
"strconv"
- "sync"
"time"
"github.com/containers/libpod/libpod/define"
@@ -120,20 +119,24 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams,
attachChan := make(chan error)
// We need to ensure that we don't return until start() fired in attach.
- // Use a WaitGroup to sync this.
- wg := new(sync.WaitGroup)
- wg.Add(1)
+ // Use a channel to sync
+ startedChan := make(chan bool)
// Attach to the container before starting it
go func() {
- if err := c.attach(streams, keys, resize, true, wg); err != nil {
+ if err := c.attach(streams, keys, resize, true, startedChan); err != nil {
attachChan <- err
}
close(attachChan)
}()
- wg.Wait()
- c.newContainerEvent(events.Attach)
+ select {
+ case err := <-attachChan:
+ return nil, err
+ case <-startedChan:
+ c.newContainerEvent(events.Attach)
+ }
+
return attachChan, nil
}