Add option to exit on stdin EOF, standardize wait-for-interrupt, fix runwda exit #159
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit adds a --eof/-e flag to tell go-ios to read stdin and listen for EOF.
When run with this flag, EOF (^D) is treated identically to SIGINT (^C).
The primary purpose of this modification is to have go-ios exit automatically, with high reliability and in an OS-agnostic way, when a calling program is terminated non-gracefully. When a linux or MacOS process is terminated with SIGKILL, there is no opportunity to kill child processes, which more-than-likely end up as orphans with parent ID 1 (init). This can easily orphan tunnels, wda-instances, etc. However, if go-ios reads from a stdin provided by the parent process, when the parent is killed, the OS is guaranteed to deliver EOF.
This is added as a flag (--eof) rather than a default, because it is the default golang cmd.Exec behavior to immediately close STDIN (by piping /dev/null) when the user does not explicitly set up an input pipe. Thus, changing the default behavior would likely breaking existing scripts. (In golang, you can force stdin by calling cmd.StdinPipe()).
As part of this commit, it was observed that various command-line functions were inconsistent on which signals checked for termination (syscall.SIGINT, syscall.SIGTERM, os.Interrupt). A new common function consolidates these calls. This also yields a consistent log statement.
Commit 94f24f8 by wangchaoHZ and colerwang, which added a context.Context() option to the "runwda" function also inadvertently broke clean shutdown on ^C from runwda. The issue was in passing a context.Background() object for the new context parameter, when 'nil' should have been passed instead. Given how this function is implemented, I have attempted to include the appropriate fix, which passes in a new cancellable context that is sensitive to both signals and EOF, and deleted some now-extraneous code. In this instance, it would have been equally correct to simply replace context.Background() with 'nil', but the new approach is more general (since it doesn't require a dedicated cancel function like testmanagerd.CloseXCUITestRunner), and may server as a useful example if contexts are used this other way for functions in the future.