Skip to content

Conversation

@ehsundar
Copy link

Problem

Exec and ExecForEach commands can run indefinitely, causing resource leaks and hanging CI/CD pipelines with no way to set timeouts.

Solution

Adds WithTimeout(duration time.Duration) method for both Exec and ExecForEach commands.

// Basic usage
script.NewPipe().WithTimeout(5*time.Second).Exec("sleep 10").Wait()

// ExecForEach with timeout
script.Echo("file1\nfile2").WithTimeout(2*time.Second).ExecForEach("sleep 5").Wait()

Realistic Example: CI/CD Pipeline

// Build with timeout
script.Exec("go build ./...").WithTimeout(2*time.Minute).Wait()

// Test with timeout  
script.Exec("go test ./...").WithTimeout(5*time.Minute).Wait()

// Security scan with timeout
script.Exec("gosec ./...").WithTimeout(1*time.Minute).Wait()

Why This is Better

  • Simple API: WithTimeout() follows existing patterns
  • No external deps: Built into the library
  • Reliable: Uses context.WithTimeout and exec.CommandContext
  • Backward compatible: Zero timeout means no timeout

Fixes #225

@bitfield
Copy link
Owner

Thanks for the PR. What do you think about the comments on #235 225 about letting users inject a context, and also about using WaitDelay on the command? I don't think we've yet reached a consensus about how this feature should work, so I think we need to do that before attempting to implement anything.

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

Successfully merging this pull request may close these issues.

Allow timeout for Exec to avoid hanging

2 participants