diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index e3e2cf2bdb8..f593533b777 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -225,8 +225,22 @@ func (c *Container) Run(process *Process) error { // Exec signals the container to exec the users process at the end of the init. func (c *Container) Exec() error { c.m.Lock() - defer c.m.Unlock() - return c.exec() + err := c.exec() + if err != nil { + c.m.Unlock() + return err + } + c.m.Unlock() + s, err := c.currentOCIState() + if err != nil { + return err + } + if c.config.Hooks != nil { + if err := c.config.Hooks.Run(configs.Poststart, s); err != nil { + return fmt.Errorf("run poststart hook %w", err) + } + } + return nil } func (c *Container) exec() error { @@ -353,19 +367,6 @@ func (c *Container) start(process *Process) (retErr error) { if process.Init { c.fifo.Close() - if c.config.Hooks != nil { - s, err := c.currentOCIState() - if err != nil { - return err - } - - if err := c.config.Hooks.Run(configs.Poststart, s); err != nil { - if err := ignoreTerminateErrors(parent.terminate()); err != nil { - logrus.Warn(fmt.Errorf("error running poststart hook: %w", err)) - } - return err - } - } } return nil }