@@ -14,6 +14,7 @@ import (
1414 "github.com/Sirupsen/logrus"
1515 "github.com/docker/containerd/specs"
1616 ocs "github.com/opencontainers/runtime-spec/specs-go"
17+ "golang.org/x/net/context"
1718 "golang.org/x/sys/unix"
1819)
1920
@@ -24,9 +25,9 @@ type Container interface {
2425 // Path returns the path to the bundle
2526 Path () string
2627 // Start starts the init process of the container
27- Start (checkpointPath string , s Stdio ) (Process , error )
28+ Start (ctx context. Context , checkpointPath string , s Stdio ) (Process , error )
2829 // Exec starts another process in an existing container
29- Exec (string , specs.ProcessSpec , Stdio ) (Process , error )
30+ Exec (context. Context , string , specs.ProcessSpec , Stdio ) (Process , error )
3031 // Delete removes the container's state and any resources
3132 Delete () error
3233 // Processes returns all the containers processes that have been added
@@ -186,7 +187,7 @@ func Load(root, id, shimName string, timeout time.Duration) (Container, error) {
186187 }
187188 p , err := loadProcess (filepath .Join (root , id , pid ), pid , c , s )
188189 if err != nil {
189- logrus .WithField ("id" , id ).WithField ("pid" , pid ).Debug ("containerd: error loading process %s" , err )
190+ logrus .WithField ("id" , id ).WithField ("pid" , pid ).Debugf ("containerd: error loading process %s" , err )
190191 continue
191192 }
192193 c .processes [pid ] = p
@@ -394,7 +395,7 @@ func (c *container) DeleteCheckpoint(name string, checkpointDir string) error {
394395 return os .RemoveAll (filepath .Join (checkpointDir , name ))
395396}
396397
397- func (c * container ) Start (checkpointPath string , s Stdio ) (Process , error ) {
398+ func (c * container ) Start (ctx context. Context , checkpointPath string , s Stdio ) (Process , error ) {
398399 processRoot := filepath .Join (c .root , c .id , InitProcessID )
399400 if err := os .Mkdir (processRoot , 0755 ); err != nil {
400401 return nil , err
@@ -423,13 +424,13 @@ func (c *container) Start(checkpointPath string, s Stdio) (Process, error) {
423424 if err != nil {
424425 return nil , err
425426 }
426- if err := c .createCmd (InitProcessID , cmd , p ); err != nil {
427+ if err := c .createCmd (ctx , InitProcessID , cmd , p ); err != nil {
427428 return nil , err
428429 }
429430 return p , nil
430431}
431432
432- func (c * container ) Exec (pid string , pspec specs.ProcessSpec , s Stdio ) (pp Process , err error ) {
433+ func (c * container ) Exec (ctx context. Context , pid string , pspec specs.ProcessSpec , s Stdio ) (pp Process , err error ) {
433434 processRoot := filepath .Join (c .root , c .id , pid )
434435 if err := os .Mkdir (processRoot , 0755 ); err != nil {
435436 return nil , err
@@ -463,13 +464,13 @@ func (c *container) Exec(pid string, pspec specs.ProcessSpec, s Stdio) (pp Proce
463464 if err != nil {
464465 return nil , err
465466 }
466- if err := c .createCmd (pid , cmd , p ); err != nil {
467+ if err := c .createCmd (ctx , pid , cmd , p ); err != nil {
467468 return nil , err
468469 }
469470 return p , nil
470471}
471472
472- func (c * container ) createCmd (pid string , cmd * exec.Cmd , p * process ) error {
473+ func (c * container ) createCmd (ctx context. Context , pid string , cmd * exec.Cmd , p * process ) error {
473474 p .cmd = cmd
474475 if err := cmd .Start (); err != nil {
475476 close (p .cmdDoneCh )
@@ -508,10 +509,25 @@ func (c *container) createCmd(pid string, cmd *exec.Cmd, p *process) error {
508509 close (p .cmdDoneCh )
509510 }()
510511 }()
511- if err := c .waitForCreate (p , cmd ); err != nil {
512+
513+ ch := make (chan error )
514+ go func () {
515+ if err := c .waitForCreate (p , cmd ); err != nil {
516+ ch <- err
517+ return
518+ }
519+ c .processes [pid ] = p
520+ ch <- nil
521+ }()
522+ select {
523+ case <- ctx .Done ():
524+ cmd .Process .Kill ()
525+ cmd .Wait ()
526+ <- ch
527+ return ctx .Err ()
528+ case err := <- ch :
512529 return err
513530 }
514- c .processes [pid ] = p
515531 return nil
516532}
517533
0 commit comments