Skip to content

Commit 00d1cb3

Browse files
author
OpenShift Bot
committed
Merge pull request #278 from bparees/waitgroup
Merged by openshift-bot
2 parents f8ae6d9 + cb22a48 commit 00d1cb3

File tree

2 files changed

+12
-45
lines changed

2 files changed

+12
-45
lines changed

pkg/docker/docker.go

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func runContainerDockerRun(container *docker.Container, d *stiDocker, image stri
413413
}
414414

415415
// this funtion simply abstracts out the first phase of attaching to the container that was originally in line with the RunContainer() method
416-
func runContainerAttachOne(attached chan struct{}, container *docker.Container, opts RunContainerOptions, d *stiDocker) sync.WaitGroup {
416+
func runContainerAttach(attached chan struct{}, container *docker.Container, opts RunContainerOptions, d *stiDocker) *sync.WaitGroup {
417417
attachOpts := docker.AttachToContainerOptions{
418418
Container: container.ID,
419419
Success: attached,
@@ -422,11 +422,11 @@ func runContainerAttachOne(attached chan struct{}, container *docker.Container,
422422
if opts.Stdin != nil {
423423
attachOpts.InputStream = opts.Stdin
424424
attachOpts.Stdin = true
425-
} else if opts.Stdout != nil {
425+
}
426+
if opts.Stdout != nil {
426427
attachOpts.OutputStream = opts.Stdout
427428
attachOpts.Stdout = true
428429
}
429-
430430
if opts.Stderr != nil {
431431
attachOpts.ErrorStream = opts.Stderr
432432
attachOpts.Stderr = true
@@ -440,33 +440,11 @@ func runContainerAttachOne(attached chan struct{}, container *docker.Container,
440440
glog.Errorf("Unable to attach container with %v", attachOpts)
441441
}
442442
}()
443-
return wg
444-
}
445-
446-
// this funtion simply abstracts out the second phase of attaching to the container that was originally in line with the RunContainer() method
447-
func runContainerAttachTwo(attached2 chan struct{}, container *docker.Container, opts RunContainerOptions, d *stiDocker, wg sync.WaitGroup) {
448-
attachOpts2 := docker.AttachToContainerOptions{
449-
Container: container.ID,
450-
Success: attached2,
451-
Stream: true,
452-
OutputStream: opts.Stdout,
453-
Stdout: true,
454-
}
455-
if opts.Stderr != nil {
456-
attachOpts2.Stderr = true
457-
attachOpts2.ErrorStream = opts.Stderr
458-
}
459-
go func() {
460-
wg.Add(1)
461-
defer wg.Done()
462-
if err := d.client.AttachToContainer(attachOpts2); err != nil {
463-
glog.Errorf("Unable to attach container with %v", attachOpts2)
464-
}
465-
}()
443+
return &wg
466444
}
467445

468446
// this funtion simply abstracts out the waiting on the container hosting the builder function that was originally in line with the RunContainer() method
469-
func runContainerWait(wg sync.WaitGroup, d *stiDocker, container *docker.Container) error {
447+
func runContainerWait(wg *sync.WaitGroup, d *stiDocker, container *docker.Container) error {
470448
glog.V(2).Infof("Waiting for container")
471449
exitCode, err := d.client.WaitContainer(container.ID)
472450
glog.V(2).Infof("Container wait returns with %d and %v\n", exitCode, err)
@@ -528,22 +506,11 @@ func (d *stiDocker) RunContainer(opts RunContainerOptions) (err error) {
528506
defer d.RemoveContainer(container.ID)
529507

530508
glog.V(2).Infof("Attaching to container")
531-
// creating / piping the channels in runContainerAttachOne lead to unintended hangs
509+
// creating / piping the channels in runContainerAttach lead to unintended hangs
532510
attached := make(chan struct{})
533-
wg := runContainerAttachOne(attached, container, opts, d)
511+
wg := runContainerAttach(attached, container, opts, d)
534512
attached <- <-attached
535513

536-
// If attaching both stdin and stdout or stderr, attach stdout and stderr in
537-
// a second goroutine
538-
// TODO remove this goroutine when docker 1.4 will be in broad usage,
539-
// see: https://github.com/docker/docker/commit/f936a10d8048f471d115978472006e1b58a7c67d
540-
if opts.Stdin != nil && opts.Stdout != nil {
541-
// creating / piping the channels in runContainerAttachTwo lead to unintended hangs
542-
attached2 := make(chan struct{})
543-
runContainerAttachTwo(attached2, container, opts, d, wg)
544-
attached2 <- <-attached2
545-
}
546-
547514
glog.V(2).Infof("Starting container")
548515
if err = d.client.StartContainer(container.ID, nil); err != nil {
549516
return err

pkg/docker/docker_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,16 @@ func TestRunContainer(t *testing.T) {
463463
}
464464

465465
// Verify that AttachToContainer was called twice (Stdin/Stdout)
466-
if len(fakeDocker.AttachToContainerOpts) != 2 {
466+
if len(fakeDocker.AttachToContainerOpts) != 1 {
467467
t.Errorf("%s: AttachToContainer was not called the expected number of times.", desc)
468468
}
469469
// Make sure AttachToContainer was not called with both Stdin & Stdout
470470
for _, opt := range fakeDocker.AttachToContainerOpts {
471-
if opt.InputStream != nil && (opt.OutputStream != nil) {
472-
t.Errorf("%s: AttachToContainer was called with both Stdin and Stdout: %#v", desc, opt)
471+
if opt.InputStream == nil || opt.OutputStream == nil {
472+
t.Errorf("%s: AttachToContainer was not called with both Stdin and Stdout: %#v", desc, opt)
473473
}
474-
if opt.Stdin && (opt.Stdout) {
475-
t.Errorf("%s: AttachToContainer was called with both Stdin and Stdout flags: %#v", desc, opt)
474+
if !opt.Stdin || !opt.Stdout {
475+
t.Errorf("%s: AttachToContainer was not called with both Stdin and Stdout flags: %#v", desc, opt)
476476
}
477477
}
478478
}

0 commit comments

Comments
 (0)