Skip to content
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

[invoker] set the WaitDelay to avoid getting indefinitely blocking wait() #1614

Open
1 of 5 tasks
acabarbaye opened this issue Mar 21, 2024 · 1 comment
Open
1 of 5 tasks

Comments

@acabarbaye
Copy link
Contributor

Describe the bug

The problem lies in how the commands are spawned in the Invoker

cmd := exec.CommandContext(ctx, name, arg...)

if the spawned command opens I/Os, the wait() could block indefinitely. This bug has been reported in many issues with go : golang/go#24050 golang/go#20730 golang/go#23019 golang/go#56187 golang/go#57129 golang/go#60309 golang/go#22485

A solution is to set the WaitDelay to ensure wait() returns

// If WaitDelay is non-zero, the command's I/O pipes will be closed after
// WaitDelay has elapsed after either the command's process has exited or
// (if Context is non-nil) Context is done, whichever occurs first.
// If the command's process is still running after WaitDelay has elapsed,
// it will be terminated with os.Kill before the pipes are closed.
//
// If the command exits with a success code after pipes are closed due to
// WaitDelay and no Interrupt signal has been sent, Wait and similar methods
// will return ErrWaitDelay instead of nil.
//
// If WaitDelay is zero (the default), I/O pipes will be read until EOF,
// which might not occur until orphaned subprocesses of the command have
// also closed their descriptors for the pipes.
WaitDelay time.Duration

To Reproduce
the following should block indefinitely

common.Invoke{}.Command("bash", "-c", "watch date > date.txt 2>&1")

Expected behavior
The command above should be terminated if the Timeout is exceeded

Environment (please complete the following information):

  • Windows: [paste the result of ver]
  • Linux: [paste contents of /etc/os-release and the result of uname -a]
  • Mac OS: [paste the result of sw_vers and uname -a
  • FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
  • OpenBSD: [paste the result of uname -a]

Additional context
[Cross-compiling? Paste the command you are using to cross-compile and the result of the corresponding go env]

@shirou
Copy link
Owner

shirou commented Mar 23, 2024

That is why we introduced xxxWithContext. We can avoid waiting indefinitely by setting a timeout for a context . Further, as the author of the library, we cannot set the appropriate wait time. Therefore, we provide a way for library users to set a timeout.

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