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>
This commit is contained in:
Marco Vedovati
2019-06-17 15:14:54 +02:00
parent 6e9b490f5e
commit 4f56964d55
3 changed files with 18 additions and 18 deletions

View File

@ -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
}