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

View File

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