Skip to content

Allow timeout for Exec to avoid hanging #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rcowham opened this issue Mar 13, 2025 · 5 comments
Open

Allow timeout for Exec to avoid hanging #225

rcowham opened this issue Mar 13, 2025 · 5 comments

Comments

@rcowham
Copy link

rcowham commented Mar 13, 2025

It would be nice to be able to specify a timeout to avoid commands hanging during execution.

Maybe something like the existing WithEnv(), but instead WithTimeout() or WithWaitDelay() to support the new (Go 1.20) WaitDelay?

https://pkg.go.dev/os/exec#Cmd.WaitDelay

@bitfield
Copy link
Owner

Great suggestions, @rcowham! There's been some previous discussion on this, if you haven't already seen it: in #207 and #105, for example. Would you like to review those and see if you can propose a concrete example program to demonstrate your idea?

Good feature proposals tend to satisfy as many as possible of the following criteria:

  1. Show that the lack of such a feature is a significant problem for a significant number of users
  2. Propose a feature that addresses the problem in a satisfactory way
  3. Include a runnable example program that shows how the proposed syntax or API would be used to solve the problem for a realistic use case
  4. Show that this is significantly better than any exisitng solution without the feature

@rcowham
Copy link
Author

rcowham commented Mar 28, 2025

For my use cases, a timeout on the whole pipeline is sufficient - just want to avoid an indefinite hang. Clearly this can be done by me putting it on a thread, and killing the process if required - but if library can do it...

err := script.Exec(cmd).WithTimeout(3*time.Second)
if err == script.Timeout {
   // do something
} else if err != nil {
    // do something else
}

s, err := script.Exec(cmd).WithTimeout(3*time.Second).Slice()
// Similar error handling as above

errbuf := new(bytes.Buffer)
p := script.NewPipe().WithStderr(errbuf).WithTimeout(3*time.Second)
result, err := p.Exec(cmd).<other methods>
// Similar error handling as above

@bitfield
Copy link
Owner

A context would be the more idiomatic way to do this, wouldn't it?

@rcowham
Copy link
Author

rcowham commented Mar 28, 2025

A context would be fine - wasn't sure from reading other issues if that was the preferred way...

@bitfield
Copy link
Owner

bitfield commented Apr 5, 2025

Just a reminder that this is with you (or whoever wants to work on it) to complete the four steps I outlined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants