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

Swap child and parent: prevent pplog immediate interrupt #13

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ jobs:
with:
files: ./coverage.txt
verbose: true
defaults:
run:
# NOTE: some of behavioral-tests requires tty... but
#
# GitHub Actions run without a TTY device. This is a workaround to get one,
# based on https://github.com/actions/runner/issues/241#issuecomment-2019042651
shell: 'script --return --quiet --log-out /dev/null --command "bash -e {0}"'
25 changes: 25 additions & 0 deletions behavioral-tests/ctrl-c-graceful/custom-fake-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

child_pid=

xtrap() {
echo "Getting SIGNAL $1. Exiting"
kill -9 $child_pid
exit
}

trap "xtrap SIGINT" 2 # graceful shutdown handler

echo "Arguments: $@"

(
# pretending someone kills process
for i in "$@"; do
echo "Working hard on ${i}..."
sleep 5
done
) &

child_pid=$!

wait
16 changes: 16 additions & 0 deletions behavioral-tests/ctrl-c-graceful/custom-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

# emulate terminal: run command in separate process group
set -m
(
go run ../../cmd/... ./custom-fake-server.sh ARG1 ARG2 ARG3 > output.log
) &
set +m
child_pgid=${!}

sleep 1 # allow to work a little
kill -2 -${child_pgid} # emulate user's ctrl-C

wait ${child_pgid}

echo "ok"
3 changes: 3 additions & 0 deletions behavioral-tests/ctrl-c-graceful/expected.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INVALID JSON: "Arguments: ARG1 ARG2 ARG3"
INVALID JSON: "Working hard on ARG1..."
INVALID JSON: "Getting SIGNAL SIGINT. Exiting"
28 changes: 24 additions & 4 deletions cmd/pplog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"os/exec"
"os/signal"
"path"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -178,10 +179,29 @@ func main() {
showBuildInfo()
return
}
if flag.NArg() >= 1 {
runSubprocessMode()
} else {
runPipeMode()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

done := make(chan struct{})
go func() {
if flag.NArg() >= 1 {
runSubprocessMode()
} else {
runPipeMode()
}
close(done)
}()

select {
case <-interrupt:
deb("interrupt, allow target to shutdown gracefully...")
case <-done:
}

select {
case <-interrupt:
deb("second interrupt, exit immediately")
case <-done:
}
}

Expand Down
Loading