windows: Fix a few races

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-10-05 09:14:16 -07:00
parent b2e348231c
commit ad5266456c
No known key found for this signature in database
GPG Key ID: 40CF16616B361216
2 changed files with 23 additions and 12 deletions

View File

@ -5,6 +5,7 @@ package windows
import (
"context"
"io"
"sync"
"syscall"
"time"
@ -19,13 +20,14 @@ import (
// process implements containerd.Process and containerd.State
type process struct {
sync.Mutex
hcs hcsshim.Process
id string
pid uint32
io *pipeSet
status runtime.Status
task *task
id string
pid uint32
io *pipeSet
task *task
exitCh chan struct{}
exitCode uint32
@ -93,6 +95,13 @@ func (p *process) ExitCode() (uint32, time.Time, error) {
}
func (p *process) Start(ctx context.Context) (err error) {
p.Lock()
defer p.Unlock()
if p.hcs != nil {
return errors.Wrap(errdefs.ErrFailedPrecondition, "process already started")
}
// If we fail, close the io right now
defer func() {
if err != nil {
@ -132,6 +141,7 @@ func (p *process) Start(ctx context.Context) (err error) {
if p.io.stderr != nil {
go ioCopy("stderr", p.io.stderr, stderr)
}
p.hcs = hp
// Wait for the process to exit to get the exit status
go func() {
@ -174,8 +184,6 @@ func (p *process) Start(ctx context.Context) (err error) {
// Cleanup HCS resources
hp.Close()
}()
p.status = runtime.RunningStatus
p.hcs = hp
return nil
}

View File

@ -111,6 +111,10 @@ func (t *task) Info() runtime.TaskInfo {
}
func (t *task) Start(ctx context.Context) error {
if p := t.getProcess(t.id); p != nil {
return errors.Wrap(errdefs.ErrFailedPrecondition, "task already started")
}
conf := newWindowsProcessConfig(t.spec.Process, t.io)
p, err := t.newProcess(ctx, t.id, conf, t.io)
if err != nil {
@ -136,13 +140,13 @@ func (t *task) Pause(ctx context.Context) error {
t.Lock()
t.status = runtime.PausedStatus
t.Unlock()
}
if err == nil {
t.publisher.Publish(ctx,
runtime.TaskPausedEventTopic,
&eventsapi.TaskPaused{
ContainerID: t.id,
})
return nil
}
return errors.Wrap(err, "hcsshim failed to pause task")
}
@ -157,13 +161,13 @@ func (t *task) Resume(ctx context.Context) error {
t.Lock()
t.status = runtime.RunningStatus
t.Unlock()
}
if err == nil {
t.publisher.Publish(ctx,
runtime.TaskResumedEventTopic,
&eventsapi.TaskResumed{
ContainerID: t.id,
})
return nil
}
return errors.Wrap(err, "hcsshim failed to resume task")
}
@ -313,7 +317,6 @@ func (t *task) newProcess(ctx context.Context, id string, conf *hcsshim.ProcessC
id: id,
pid: pid,
io: pset,
status: runtime.CreatedStatus,
task: t,
exitCh: make(chan struct{}),
conf: conf,