Repository navigation
remove running containers in case of a sigterm - #452
Conversation
|
@rhcarvalho @smarterclayton ptal. part of openshift/origin#8266 (comment) |
24aa7aa to
8d83b0d
Compare
|
@bparees my bad, forget what I said above :) |
| } | ||
| go func() { | ||
| sigchan := make(chan os.Signal, 1) | ||
| signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) |
There was a problem hiding this comment.
What's the rationale behind the choice of signals to listen to here?
It seems to be in agreement with the code in https://github.com/kubernetes/kubernetes/tree/master/pkg/util/interrupt (used in openshift/origin#8306), that handles SIGINT, SIGTERM, SIGQUIT on Linux, and only SIGINT in other platforms.
By default, Go programs will panic on synchronous signals (triggered by errors in program execution), and deferred calls will be executed: example.
That means if our code panics for whatever reason, we should be covered.
Then, a SIGHUP, SIGINT, or SIGTERM signal causes the program to exit. In that case no deferred call is executed. Here we're handling SIGINT and SIGTERM, but not SIGHUP. Why?
SIGQUIT (and others), by default, cause the program to exit with a stack dump. By handling it here we're removing the default stack dump.
What is the signal that the platform (OpenShift/Kubernetes) send to terminate an on-going build?
There was a problem hiding this comment.
Yes I mirrored the k8s code, basically. monkey-see-monkey-do. I defer to @smarterclayton for why k8s picked that set of signals and did not include SIGHUP.
Removing the stack dump is an unfortunate side effect, i can add logic back in that performs the stack dump before invoking os.Exit, but we end up in a bit of a race....one the container is removed, the logic that was waiting on the container proceeds and actually ends up exiting the program before os.Exit is even called, so the same would be true for stack dumping. So it's going to require much more sophisticated synchronization/signaling to ensure we can execute the stack dump before proceeding.
There was a problem hiding this comment.
Let me know when you update this to use wait.
There was a problem hiding this comment.
this isn't in origin, are you suggesting i vendor your interrupt package from k8s into s2i? feels a little crazy.
There was a problem hiding this comment.
(platform will send sigterm, to answer your other question)
efbc12c to
7c3aeb7
Compare
|
@smarterclayton ok you're right, i added your interrupt library. it was the stack dump handling that pushed me over the edge. not sure about the licensing. |
| func (h *InterruptHandler) Run(fn func() error) error { | ||
| ch := make(chan os.Signal, 1) | ||
| signal.Notify(ch, childSignals...) | ||
| defer signal.Stop(ch) |
There was a problem hiding this comment.
@bparees you copied the version that leaks the goroutine :)
There was a problem hiding this comment.
sigh. and it was in my head as i was about to do it "don't forget to fix the leak" and then i totally forgot. thanks :)
There was a problem hiding this comment.
defer close(ch)
defer signal.Stop(ch)
is the right order right?
There was a problem hiding this comment.
team work working for us again!
Rodolfo Carvalho
OpenShift Developer Experience
On Fri, Apr 1, 2016 at 3:19 PM, Ben Parees [email protected] wrote:
In pkg/util/interrupt.go
#452 (comment)
:
if h.final == nil {os.Exit(0)}h.final(s)- })
+}
+// Run takes a single function argument which will be immediately invoked.
+// If a signal arrives while the function is running, it will be handled,
+// interrupting the function execution and executing any final and notify
+// functions immediately. The original function may or may not complete
+// depending on whether the final argument invoking os.Exit.
+func (h *InterruptHandler) Run(fn func() error) error {
- ch := make(chan os.Signal, 1)
- signal.Notify(ch, childSignals...)
- defer signal.Stop(ch)
sigh. and it was in my head as i was about to do it "don't forget to fix
the leak" and then i totally forgot. thanks :)—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/openshift/source-to-image/pull/452/files/7c3aeb752fb82b3002800315666fb9f2c039e56c#r58203776
There was a problem hiding this comment.
yes, correct
On Fri, Apr 1, 2016 at 3:20 PM, Ben Parees [email protected] wrote:
In pkg/util/interrupt.go
#452 (comment)
:
if h.final == nil {os.Exit(0)}h.final(s)- })
+}
+// Run takes a single function argument which will be immediately invoked.
+// If a signal arrives while the function is running, it will be handled,
+// interrupting the function execution and executing any final and notify
+// functions immediately. The original function may or may not complete
+// depending on whether the final argument invoking os.Exit.
+func (h *InterruptHandler) Run(fn func() error) error {
- ch := make(chan os.Signal, 1)
- signal.Notify(ch, childSignals...)
- defer signal.Stop(ch)
defer close(ch) defer signal.Stop(ch)is the right order right?
—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/openshift/source-to-image/pull/452/files/7c3aeb752fb82b3002800315666fb9f2c039e56c#r58203888
|
I was thinking.... the default behavior when the signals SIGBUS, SIGFPE, and SIGSEGV are caught is to The solution with the An alternative would be to Panicking would run all deferred calls all the way up to
|
| // this funtion simply abstracts out the running of the newly produced image when the --run=true option is provided | ||
| // runContainerDockerRun funtion simply abstracts out the running of the newly produced image when the --run=true option is provided | ||
| // This function runs forever, ensure a sig handler is installed elsewhere to handle cntrl-c. | ||
| func runContainerDockerRun(container *docker.Container, d *stiDocker, image string) { |
There was a problem hiding this comment.
This has historically been a hard to understand function, at least for me 😄
It looks like it does nothing apart from calling glog.Info. One has to read and spend some time on the caller to figure out how this can possibly work.
The function name and documentation comment is sort of misleading, or at least we need to talk about it 👌
The interrupt package doesn't interfere with the stack, the stack will look like: or so if a panic occurs in anonymous function, the defer stack is intact, as much so as it was before we introduced the interrupt handler logic. if a panic occurs inside removeContainer itself while we are handling a signal from within the go routine, i don't think there is any defer we'd expect to have executed outside the go routine, because there's no guarantee a defer runs once you've received a sigterm/etc anyway, so again, no change from existing behavior. |
|
@rhcarvalho comments addressed, aside from outstanding questions about exit codes and sighup. |
|
@rhcarvalho talked to @smarterclayton, will exit(1) and catch sighup. |
|
And open PRs to Kube to make those changes... On Fri, Apr 1, 2016 at 12:10 PM, Ben Parees [email protected]
|
|
PR opened: kubernetes/kubernetes#23752 |
3cd87b7 to
50b50bd
Compare
|
@rhcarvalho ready for a final review i think. note that i added os.Kill to the list of windows signals to handle. Not 100% confident on that, but it seemed reasonable to add. |
|
LGTM (I don't really like the fact that we now have to update the k8s sources manually... but I guess it is not possible to godep that k8s package). |
|
@mfojtik it would be possible, it was discussed, for now we went w/ this direction, but we can always change it. it doesn't seem like a library that should be frequently changing. |
|
Copying / adapting the package instead of adding a new dependency goes along the Go proverb
|
|
depends on what is "little"
|
|
Godeps doesn't manage independent subdirectories of an entire Git repository. source-to-image <<< kubernetes, we don't want to copy the whole kubernetes tree just to get a trivial (compared to source-to-image and k8s) package. Interesting links:
@mfojtik let's stop hijacking this PR for this discussion and get back to evaluating the code :) |
|
I am not saying to vendor entire kube, godeps allows to vendor jist the I was not hijacking this PR, I actually reviewed it yesterday night and
|
| ) | ||
|
|
||
| // childSignals are the allowed signals that can be sent to children in Windows to terminate | ||
| var childSignals = []os.Signal{os.Interrupt, os.Kill} |
There was a problem hiding this comment.
@bparees I don't understand this decision, why catch SIGKILL in !linux platforms, and not catch it on linux?!
According to os/signal docs:
The signals SIGKILL and SIGSTOP may not be caught by a program, and therefore can not be affected by this package.
I believe the existence of os.Kill is to send it to processes, not to catch it. See https://golang.org/pkg/os/#Process.Signal
There was a problem hiding this comment.
on Windows sigkill is an equivalent of window close afaik, which is not the same as kill on UNIX.
There was a problem hiding this comment.
Is that an argument to don't do clean up for SIGKILL on Windows? What about Darwin?
There was a problem hiding this comment.
(Amending my previous comment)
on Windows sigkill is an equivalent of window close afaik, which is not the same as kill on UNIX.
Is that an argument to handle SIGKILL on Windows AND Darwin, and not on Linux?
As the docs say, we cannot catch SIGKILL using signal.Notify. Short demo: https://gist.github.com/rhcarvalho/71bea7d3dd2b969bfac8e244f9810963
So, @bparees, addressing your concern, it makes no sense to "try to catch" SIGKILL.
There was a problem hiding this comment.
okey dokey, i'll remove it.
|
[merge] |
|
Source To Image Merge Results: SUCCESS (https://ci.openshift.redhat.com/jenkins/job/merge_pr_sti/60/) |
|
@bparees o-o |
|
@mfojtik indentation/nesting problem. wasn't running the post-exec hook unless there was an onstart hook. doh. |
|
Evaluated for source to image merge up to d235fb5 |
| // we're ignoring ErrClosedPipe, as this is information | ||
| // the docker container ended streaming logs | ||
| if glog.V(2) && err != io.ErrClosedPipe { | ||
| if glog.V(2) && err != io.ErrClosedPipe && err != io.EOF { |
There was a problem hiding this comment.
Why are we adding the new condition?
Do you know why, here and in layered.go, we have glog.V(2) in the if clause instead of just writing glog.V(2).Errorf(...)?
There was a problem hiding this comment.
no, not sure why it was done that way. the new condition is because EOF is what we normally get now, possibly due to your goroutine fixes, I didn't consider it important enough to dig into.
1c8b1bc to
d235fb5
Compare
No description provided.