title: Calling an executable with a timeout
body: <p>I am trying to use the <code>context</code> package to run a binary with a 10 second timeout, as such:</p>
  <pre><code>func RunQL(file db.File, flag string) string { 
      
      // 10-second timeout for the binary to run
      ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
      defer cancel()
  
      cmd := exec.CommandContext(ctx, &quot;qltool&quot;, &quot;run&quot;, &quot;-f&quot;, file.Path, &quot;--rootfs&quot;, file.Root, &quot;--args&quot;, flag)
      out, err := cmd.Output()
  
      // check to see if our timeout was executed
      if ctx.Err() == context.DeadlineExceeded {
          return &quot;&quot;
      }
  
      // no timeout (either completed successfully or errored)
      if err != nil {
          return &quot;&quot;
      }
  
      return string(out)
  }
  </code></pre>
  <p>But for some reason, it still hangs if the process lasts longer than 10 seconds. Not sure what would be causing this, I also noticed that the documentation for the <code>CommandContext()</code> function appears to be wrong/misleading? It shows the following code:</p>
  <pre><code>func main() {
      ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
      defer cancel()
  
      if err := exec.CommandContext(ctx, &quot;sleep&quot;, &quot;5&quot;).Run(); err != nil {
          // This will fail after 100 milliseconds. The 5 second sleep
          // will be interrupted.
      }
  }
  </code></pre>
  <p>But <code>CommandContext()</code> returns type <code>*Cmd</code> not <code>error</code> .</p>