go-dexec icon indicating copy to clipboard operation
go-dexec copied to clipboard

Missing output from fast-running commands

Open waitingkuo opened this issue 9 years ago • 5 comments

I wanna run sh -c "echo $(pwd)".

cmd is executed by dexec, but it doesn't output anything. cmd2 is executed by go's built-in exec , it works.

package main

import (
  "log"

  "github.com/ahmetalpbalkan/dexec"
  "github.com/fsouza/go-dockerclient"
  "os/exec"
)

func main() {
  cl, _ := docker.NewClient("unix:///var/run/docker.sock")
  d := dexec.Docker{cl}

  m, _ := dexec.ByCreatingContainer(docker.CreateContainerOptions{
    Config: &docker.Config{Image: "busybox"}})

  cmd := d.Command(m, "sh", "-c", "echo $(pwd)")
  b, err := cmd.Output()
  if err != nil {
    log.Fatal(err)
  }
  log.Printf("%s", b)

  cmd2 := exec.Command("sh", "-c", "echo $(pwd)")
  bb, err := cmd2.Output()
  if err != nil {
    log.Fatal(err)
  }
  log.Printf("%s", bb) 

}


waitingkuo avatar Jul 11 '16 09:07 waitingkuo

@waitingkuo ah that's nasty. That sounds a bit like the known issue we got at https://godoc.org/github.com/ahmetalpbalkan/go-dexec#hdr-Known_issues

  • You may receive empty stdout/stderr from commands if the executed command does not end with a trailing new line or has a different flushing behavior.

Can you try appending an empty line echo after that? (echo $(pwd);echo)

ahmetb avatar Jul 11 '16 17:07 ahmetb

@ahmetalpbalkan it's still the same. Sometimes it can output correctly, sometimes empty.

waitingkuo avatar Jul 12 '16 12:07 waitingkuo

@waitingkuo yep that flakiness actually is my experience with running fast commands as well while I developed this. It appears like there is either a flushing bug in the go-dockerclient's hijack protocol implementation I am using or in the docker engine. (Former is more likely.)

When you run slower commands (or add sleeps it is actually working fine). What I can suggest is, try using https://godoc.org/github.com/fsouza/go-dockerclient directly (you can take a look at dexec source code and see how we call into that library) and repro it with the go-dockerclient only, and open an issue on its repository.

That would fix the downstream.

ahmetb avatar Jul 12 '16 17:07 ahmetb

@fsouza any ideas? Have you heard of any missing output cases from go-dockerclient attach/hijack functionality?

ahmetb avatar Jul 19 '16 16:07 ahmetb

@ahmetalpbalkan I haven't, but I can help debugging it. I'll try to reproduce it using only go-dockerclient.

fsouza avatar Jul 19 '16 19:07 fsouza