|
@@ -6,6 +6,7 @@ import (
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
+ "sync"
|
|
"syscall"
|
|
"syscall"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -93,22 +94,37 @@ func (ctr *container) start() error {
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
- createChan := make(chan struct{})
|
|
|
|
|
|
+
|
|
|
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
+ defer cancel()
|
|
|
|
+ ready := make(chan struct{})
|
|
|
|
+
|
|
iopipe, err := ctr.openFifos(spec.Process.Terminal)
|
|
iopipe, err := ctr.openFifos(spec.Process.Terminal)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ var stdinOnce sync.Once
|
|
|
|
+
|
|
// we need to delay stdin closure after container start or else "stdin close"
|
|
// we need to delay stdin closure after container start or else "stdin close"
|
|
// event will be rejected by containerd.
|
|
// event will be rejected by containerd.
|
|
// stdin closure happens in AttachStreams
|
|
// stdin closure happens in AttachStreams
|
|
stdin := iopipe.Stdin
|
|
stdin := iopipe.Stdin
|
|
iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
|
|
iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
|
|
- go func() {
|
|
|
|
- <-createChan
|
|
|
|
- stdin.Close()
|
|
|
|
- }()
|
|
|
|
- return nil
|
|
|
|
|
|
+ var err error
|
|
|
|
+ stdinOnce.Do(func() { // on error from attach we don't know if stdin was already closed
|
|
|
|
+ err = stdin.Close()
|
|
|
|
+ go func() {
|
|
|
|
+ select {
|
|
|
|
+ case <-ready:
|
|
|
|
+ if err := ctr.sendCloseStdin(); err != nil {
|
|
|
|
+ logrus.Warnf("failed to close stdin: %+v")
|
|
|
|
+ }
|
|
|
|
+ case <-ctx.Done():
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+ })
|
|
|
|
+ return err
|
|
})
|
|
})
|
|
|
|
|
|
r := &containerd.CreateContainerRequest{
|
|
r := &containerd.CreateContainerRequest{
|
|
@@ -125,20 +141,18 @@ func (ctr *container) start() error {
|
|
ctr.client.appendContainer(ctr)
|
|
ctr.client.appendContainer(ctr)
|
|
|
|
|
|
if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
|
|
if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
|
|
- close(createChan)
|
|
|
|
ctr.closeFifos(iopipe)
|
|
ctr.closeFifos(iopipe)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
|
|
resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
|
|
if err != nil {
|
|
if err != nil {
|
|
- close(createChan)
|
|
|
|
ctr.closeFifos(iopipe)
|
|
ctr.closeFifos(iopipe)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
ctr.startedAt = time.Now()
|
|
ctr.startedAt = time.Now()
|
|
ctr.systemPid = systemPid(resp.Container)
|
|
ctr.systemPid = systemPid(resp.Container)
|
|
- close(createChan)
|
|
|
|
|
|
+ close(ready)
|
|
|
|
|
|
return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
|
|
return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
|
|
CommonStateInfo: CommonStateInfo{
|
|
CommonStateInfo: CommonStateInfo{
|