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>
Upstream-commit: 1a60a805bfabee729dbc833515cd0be439adb95b
Component: engine
This commit is contained in:
Brian Goff
2016-01-15 11:57:23 -05:00
parent 89c24617af
commit addf165927
4 changed files with 65 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"strings"
"time"
"github.com/docker/docker/pkg/integration/checker"
"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
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")
startExec(id, http.StatusOK)
startExec(c, id, http.StatusOK)
id = createExec(c, "test")
dockerCmd(c, "stop", "test")
startExec(id, http.StatusNotFound)
startExec(c, id, http.StatusNotFound)
dockerCmd(c, "start", "test")
startExec(id, http.StatusNotFound)
startExec(c, id, http.StatusNotFound)
// make sure exec is created before pausing
id = createExec(c, "test")
dockerCmd(c, "pause", "test")
startExec(id, http.StatusConflict)
startExec(c, id, http.StatusConflict)
dockerCmd(c, "unpause", "test")
startExec(id, http.StatusOK)
startExec(c, id, http.StatusOK)
}
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)
}
// #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 {
_, 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)))
@ -118,3 +133,22 @@ func createExec(c *check.C, name string) string {
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
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)
}