Skip to content

remove running containers in case of a sigterm - #452

Merged
openshift-bot merged 1 commit into
openshift:masterfrom
bparees:sigterm
Apr 5, 2016
Merged

openshift-bot merged 1 commit into
openshift:masterfrom
bparees:sigterm

Conversation

@bparees

@bparees bparees commented Mar 30, 2016

Copy link
Copy Markdown
Contributor

No description provided.

@bparees

bparees commented Mar 30, 2016

Copy link
Copy Markdown
Contributor Author

@bparees
bparees force-pushed the sigterm branch 2 times, most recently from 24aa7aa to 8d83b0d Compare March 30, 2016 20:41
@rhcarvalho

Copy link
Copy Markdown
Contributor

@bparees IMHO this change goes in the wrong direction. What we should fix is #443.

With this commit we're just increasing the problem I explain in that issue; while trying to ensure we remove a container, we are, as a side-effect, ignoring whatever other deferred calls.

@rhcarvalho

Copy link
Copy Markdown
Contributor

@bparees my bad, forget what I said above :)

Comment thread pkg/docker/docker.go Outdated
}
go func() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know when you update this to use wait.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't in origin, are you suggesting i vendor your interrupt package from k8s into s2i? feels a little crazy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(platform will send sigterm, to answer your other question)

@bparees

bparees commented Apr 1, 2016

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread pkg/util/interrupt.go
func (h *InterruptHandler) Run(fn func() error) error {
ch := make(chan os.Signal, 1)
signal.Notify(ch, childSignals...)
defer signal.Stop(ch)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bparees you copied the version that leaks the goroutine :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    defer close(ch)
    defer signal.Stop(ch)

is the right order right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rhcarvalho

Copy link
Copy Markdown
Contributor

<dumping idea>

I was thinking.... the default behavior when the signals SIGBUS, SIGFPE, and SIGSEGV are caught is to panic... When a goroutine is panicking, it does execute all deferred calls up the call stack.

The solution with the interrupt package will only execute cleanup that we explicitly included in its usage, but will not honor any other deferred calls up the call stack.

An alternative would be to panic when we catch SIGHUP, SIGINT, SIGTERM (...).

Panicking would run all deferred calls all the way up to func main. It we desire, we could handle panics to omit a stack dump, or to include a more friendly message.

</dumping idea>

Comment thread pkg/docker/docker.go Outdated
// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👌

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update.

@bparees

bparees commented Apr 1, 2016

Copy link
Copy Markdown
Contributor Author

I was thinking.... the default behavior when the signals SIGBUS, SIGFPE, and SIGSEGV are caught is to panic... When a goroutine is panicking, it does execute all deferred calls up the call stack.
The solution with the interrupt package will only execute cleanup that we explicitly included in its usage, but will not honor any other deferred calls up the call stack.
An alternative would be to panic when we catch SIGHUP, SIGINT, SIGTERM (...).
Panicking would run all deferred calls all the way up to func main. It we desire, we could handle panics to omit a stack dump, or to include a more friendly message.

The interrupt package doesn't interfere with the stack, the stack will look like:

RunContainer()
interruptHandler.Run()
anonymous function(starting/waiting for the container)

or

RunContainer()
interruptHandler.Run()
interruptHandler.Close()
removeContainer()  // if anonymous finishes and we're in the deferred Close()

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.

@bparees

bparees commented Apr 1, 2016

Copy link
Copy Markdown
Contributor Author

@rhcarvalho comments addressed, aside from outstanding questions about exit codes and sighup.

@bparees

bparees commented Apr 1, 2016

Copy link
Copy Markdown
Contributor Author

@rhcarvalho talked to @smarterclayton, will exit(1) and catch sighup.

@smarterclayton

Copy link
Copy Markdown
Contributor

And open PRs to Kube to make those changes...

On Fri, Apr 1, 2016 at 12:10 PM, Ben Parees [email protected]
wrote:

@rhcarvalho https://github.com/rhcarvalho talked to @smarterclayton
https://github.com/smarterclayton, will exit(1) and catch sighup.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#452 (comment)

@bparees

bparees commented Apr 1, 2016

Copy link
Copy Markdown
Contributor Author

PR opened: kubernetes/kubernetes#23752

@openshift-bot openshift-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 4, 2016
@bparees
bparees force-pushed the sigterm branch 3 times, most recently from 3cd87b7 to 50b50bd Compare April 4, 2016 20:24
@bparees

bparees commented Apr 4, 2016

Copy link
Copy Markdown
Contributor Author

@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.

@openshift-bot openshift-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 4, 2016
@mfojtik

mfojtik commented Apr 4, 2016

Copy link
Copy Markdown
Contributor

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).

@bparees

bparees commented Apr 4, 2016

Copy link
Copy Markdown
Contributor Author

@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.

@rhcarvalho

Copy link
Copy Markdown
Contributor

Copying / adapting the package instead of adding a new dependency goes along the Go proverb

A little copying is better than a little dependency.

@mfojtik

mfojtik commented Apr 5, 2016

Copy link
Copy Markdown
Contributor

depends on what is "little"
On Apr 5, 2016 11:05 AM, "Rodolfo Carvalho" [email protected]
wrote:

Copying / adapting the package instead of adding a new dependency goes
along the Go proverb https://go-proverbs.github.io/

A little copying is better than a little dependency.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#452 (comment)

@rhcarvalho

Copy link
Copy Markdown
Contributor

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 :)

@mfojtik

mfojtik commented Apr 5, 2016

Copy link
Copy Markdown
Contributor

I am not saying to vendor entire kube, godeps allows to vendor jist the
package you need and track the kube commit the package was taken from.
Which allows to bump the package version if needed.

I was not hijacking this PR, I actually reviewed it yesterday night and
signed with LGTM. I expressed my concerns and Ben replied it was already
discussed, which was satisfying answer to me...
On Apr 5, 2016 11:39 AM, "Rodolfo Carvalho" [email protected]
wrote:

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 https://github.com/mfojtik let's stop hijacking this PR for
this discussion and get back to evaluating the code :)

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#452 (comment)

Comment thread pkg/util/child.go Outdated
)

// childSignals are the allowed signals that can be sent to children in Windows to terminate
var childSignals = []os.Signal{os.Interrupt, os.Kill}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on Windows sigkill is an equivalent of window close afaik, which is not the same as kill on UNIX.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that an argument to don't do clean up for SIGKILL on Windows? What about Darwin?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey dokey, i'll remove it.

@bparees

bparees commented Apr 5, 2016

Copy link
Copy Markdown
Contributor Author

[merge]

@openshift-bot

Copy link
Copy Markdown
Contributor

@mfojtik

mfojtik commented Apr 5, 2016

Copy link
Copy Markdown
Contributor

@bparees o-o

--- FAIL: TestCleanBuild (1.04s)
    integration_test.go:318: The build failed.
=== RUN TestCleanBuildUser

@bparees

bparees commented Apr 5, 2016

Copy link
Copy Markdown
Contributor Author

@mfojtik indentation/nesting problem. wasn't running the post-exec hook unless there was an onstart hook. doh.

@openshift-bot

Copy link
Copy Markdown
Contributor

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bparees
bparees force-pushed the sigterm branch 2 times, most recently from 1c8b1bc to d235fb5 Compare April 5, 2016 21:14
@openshift-bot
openshift-bot merged commit 3a1715b into openshift:master Apr 5, 2016
@bparees
bparees deleted the sigterm branch April 6, 2016 19:26
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.

6 participants