Fix panic on starting exec more than once
Issue was caused when exec is tarted, exits, then stated again. In this case, `Close` is called twice, which closes a channel twice. Changes execConfig.ExitCode to a pointer so we can test if the it has been set or not. This allows us to return early when the exec has already been run. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
parent
9b63019711
commit
81cd580d68
4 changed files with 65 additions and 17 deletions
|
@ -135,6 +135,11 @@ func (d *Daemon) ContainerExecStart(name string, stdin io.ReadCloser, stdout io.
|
||||||
}
|
}
|
||||||
|
|
||||||
ec.Lock()
|
ec.Lock()
|
||||||
|
if ec.ExitCode != nil {
|
||||||
|
ec.Unlock()
|
||||||
|
return derr.ErrorCodeExecExited.WithArgs(ec.ID)
|
||||||
|
}
|
||||||
|
|
||||||
if ec.Running {
|
if ec.Running {
|
||||||
ec.Unlock()
|
ec.Unlock()
|
||||||
return derr.ErrorCodeExecRunning.WithArgs(ec.ID)
|
return derr.ErrorCodeExecRunning.WithArgs(ec.ID)
|
||||||
|
@ -214,7 +219,7 @@ func (d *Daemon) Exec(c *container.Container, execConfig *exec.Config, pipes *ex
|
||||||
exitStatus = 128
|
exitStatus = 128
|
||||||
}
|
}
|
||||||
|
|
||||||
execConfig.ExitCode = exitStatus
|
execConfig.ExitCode = &exitStatus
|
||||||
execConfig.Running = false
|
execConfig.Running = false
|
||||||
|
|
||||||
return exitStatus, err
|
return exitStatus, err
|
||||||
|
|
|
@ -18,7 +18,7 @@ type Config struct {
|
||||||
*runconfig.StreamConfig
|
*runconfig.StreamConfig
|
||||||
ID string
|
ID string
|
||||||
Running bool
|
Running bool
|
||||||
ExitCode int
|
ExitCode *int
|
||||||
ProcessConfig *execdriver.ProcessConfig
|
ProcessConfig *execdriver.ProcessConfig
|
||||||
OpenStdin bool
|
OpenStdin bool
|
||||||
OpenStderr bool
|
OpenStderr bool
|
||||||
|
|
|
@ -742,6 +742,15 @@ var (
|
||||||
HTTPStatusCode: http.StatusInternalServerError,
|
HTTPStatusCode: http.StatusInternalServerError,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// ErrorCodeExecExited is generated when we try to start an exec
|
||||||
|
// but its already running.
|
||||||
|
ErrorCodeExecExited = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
||||||
|
Value: "EXECEXITED",
|
||||||
|
Message: "Error: Exec command %s has already run",
|
||||||
|
Description: "An attempt to start an 'exec' was made, but 'exec' was already run",
|
||||||
|
HTTPStatusCode: http.StatusConflict,
|
||||||
|
})
|
||||||
|
|
||||||
// ErrorCodeExecCantRun is generated when we try to start an exec
|
// ErrorCodeExecCantRun is generated when we try to start an exec
|
||||||
// but it failed for some reason.
|
// but it failed for some reason.
|
||||||
ErrorCodeExecCantRun = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
ErrorCodeExecCantRun = errcode.Register(errGroup, errcode.ErrorDescriptor{
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/integration/checker"
|
"github.com/docker/docker/pkg/integration/checker"
|
||||||
"github.com/go-check/check"
|
"github.com/go-check/check"
|
||||||
|
@ -66,33 +67,23 @@ func (s *DockerSuite) TestExecAPIStart(c *check.C) {
|
||||||
testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvagable to Windows to Windows CI
|
testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvagable to Windows to Windows CI
|
||||||
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
||||||
|
|
||||||
startExec := func(id string, code int) {
|
|
||||||
resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
|
|
||||||
c.Assert(err, checker.IsNil)
|
|
||||||
|
|
||||||
b, err := readBody(body)
|
|
||||||
comment := check.Commentf("response body: %s", b)
|
|
||||||
c.Assert(err, checker.IsNil, comment)
|
|
||||||
c.Assert(resp.StatusCode, checker.Equals, code, comment)
|
|
||||||
}
|
|
||||||
|
|
||||||
id := createExec(c, "test")
|
id := createExec(c, "test")
|
||||||
startExec(id, http.StatusOK)
|
startExec(c, id, http.StatusOK)
|
||||||
|
|
||||||
id = createExec(c, "test")
|
id = createExec(c, "test")
|
||||||
dockerCmd(c, "stop", "test")
|
dockerCmd(c, "stop", "test")
|
||||||
|
|
||||||
startExec(id, http.StatusNotFound)
|
startExec(c, id, http.StatusNotFound)
|
||||||
|
|
||||||
dockerCmd(c, "start", "test")
|
dockerCmd(c, "start", "test")
|
||||||
startExec(id, http.StatusNotFound)
|
startExec(c, id, http.StatusNotFound)
|
||||||
|
|
||||||
// make sure exec is created before pausing
|
// make sure exec is created before pausing
|
||||||
id = createExec(c, "test")
|
id = createExec(c, "test")
|
||||||
dockerCmd(c, "pause", "test")
|
dockerCmd(c, "pause", "test")
|
||||||
startExec(id, http.StatusConflict)
|
startExec(c, id, http.StatusConflict)
|
||||||
dockerCmd(c, "unpause", "test")
|
dockerCmd(c, "unpause", "test")
|
||||||
startExec(id, http.StatusOK)
|
startExec(c, id, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
||||||
|
@ -108,6 +99,30 @@ func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
||||||
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK, comment)
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK, comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #19362
|
||||||
|
func (s *DockerSuite) TestExecAPIStartMultipleTimesError(c *check.C) {
|
||||||
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
||||||
|
execID := createExec(c, "test")
|
||||||
|
startExec(c, execID, http.StatusOK)
|
||||||
|
|
||||||
|
timeout := time.After(10 * time.Second)
|
||||||
|
var execJSON struct{ Running bool }
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-timeout:
|
||||||
|
c.Fatal("timeout waiting for exec to start")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
inspectExec(c, execID, &execJSON)
|
||||||
|
if !execJSON.Running {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
startExec(c, execID, http.StatusConflict)
|
||||||
|
}
|
||||||
|
|
||||||
func createExec(c *check.C, name string) string {
|
func createExec(c *check.C, name string) string {
|
||||||
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
|
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
|
||||||
c.Assert(err, checker.IsNil, check.Commentf(string(b)))
|
c.Assert(err, checker.IsNil, check.Commentf(string(b)))
|
||||||
|
@ -118,3 +133,22 @@ func createExec(c *check.C, name string) string {
|
||||||
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
|
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
|
||||||
return createResp.ID
|
return createResp.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startExec(c *check.C, id string, code int) {
|
||||||
|
resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
|
b, err := readBody(body)
|
||||||
|
comment := check.Commentf("response body: %s", b)
|
||||||
|
c.Assert(err, checker.IsNil, comment)
|
||||||
|
c.Assert(resp.StatusCode, checker.Equals, code, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func inspectExec(c *check.C, id string, out interface{}) {
|
||||||
|
resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "")
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
defer body.Close()
|
||||||
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
||||||
|
err = json.NewDecoder(body).Decode(out)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue