Skip to content

Commit 313762f

Browse files
committed
ensure error respones remain the same, and ensure that deadline context trumps timeout value
1 parent 75c6790 commit 313762f

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

command.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,10 @@ func (c *Command) Execute() error {
203203
cmd.Stderr = c.StderrWriter
204204
cmd.Dir = c.WorkingDir
205205

206-
// Create timer only if timeout was set > 0
207-
if c.Timeout != 0 {
206+
// Create timer only if timeout was set > 0 and context does
207+
// not have a deadline
208+
_, hasDeadline := c.ctx.Deadline()
209+
if c.Timeout != 0 && !hasDeadline {
208210
ctx, cancel := context.WithTimeout(c.ctx, c.Timeout)
209211
defer cancel()
210212
c.ctx = ctx
@@ -228,12 +230,16 @@ func (c *Command) Execute() error {
228230
if err := cmd.Process.Kill(); err != nil {
229231
return fmt.Errorf("Timeout occurred and can not kill process with pid %v", cmd.Process.Pid)
230232
}
231-
return fmt.Errorf("Command timed out after %v", c.Timeout)
233+
if c.Timeout != 0 && !hasDeadline {
234+
return fmt.Errorf("Command timed out after %v", c.Timeout)
235+
}
236+
return c.ctx.Err()
232237
case err := <-done:
233238
if err != nil {
234239
c.getExitCode(err)
235240
}
236241
}
242+
237243
c.executed = true
238244
return nil
239245
}

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
55
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
77
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
89
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
910
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1011
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)