mirror of
https://github.com/containers/podman.git
synced 2025-10-18 03:33:32 +08:00
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:
@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user