-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add a shutdown signal handler #7999
Merged
openshift-merge-robot
merged 3 commits into
containers:master
from
mheon:signal_handler
Oct 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package shutdown | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
stopped bool | ||
sigChan chan os.Signal | ||
cancelChan chan bool | ||
// Definitions of all on-shutdown handlers | ||
handlers map[string]func(os.Signal) error | ||
// Ordering that on-shutdown handlers will be invoked. | ||
handlerOrder []string | ||
shutdownInhibit sync.RWMutex | ||
) | ||
|
||
// Start begins handling SIGTERM and SIGINT and will run the given on-signal | ||
// handlers when one is called. This can be cancelled by calling Stop(). | ||
func Start() error { | ||
if sigChan != nil { | ||
// Already running, do nothing. | ||
return nil | ||
} | ||
|
||
sigChan = make(chan os.Signal, 1) | ||
cancelChan = make(chan bool, 1) | ||
stopped = false | ||
|
||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
select { | ||
case <-cancelChan: | ||
signal.Stop(sigChan) | ||
close(sigChan) | ||
close(cancelChan) | ||
stopped = true | ||
return | ||
case sig := <-sigChan: | ||
logrus.Infof("Received shutdown signal %v, terminating!", sig) | ||
shutdownInhibit.Lock() | ||
for _, name := range handlerOrder { | ||
handler, ok := handlers[name] | ||
if !ok { | ||
logrus.Errorf("Shutdown handler %s definition not found!", name) | ||
continue | ||
} | ||
logrus.Infof("Invoking shutdown handler %s", name) | ||
if err := handler(sig); err != nil { | ||
logrus.Errorf("Error running shutdown handler %s: %v", name, err) | ||
} | ||
} | ||
shutdownInhibit.Unlock() | ||
return | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// Stop the shutdown signal handler. | ||
func Stop() error { | ||
if cancelChan == nil { | ||
return errors.New("shutdown signal handler has not yet been started") | ||
} | ||
if stopped { | ||
return nil | ||
} | ||
|
||
cancelChan <- true | ||
|
||
return nil | ||
} | ||
|
||
// Temporarily inhibit signals from shutting down Libpod. | ||
func Inhibit() { | ||
shutdownInhibit.RLock() | ||
} | ||
|
||
// Stop inhibiting signals from shutting down Libpod. | ||
func Uninhibit() { | ||
shutdownInhibit.RUnlock() | ||
} | ||
|
||
// Register registers a function that will be executed when Podman is terminated | ||
// by a signal. Handlers are invoked LIFO - the last handler registered is the | ||
// first run. | ||
func Register(name string, handler func(os.Signal) error) error { | ||
if handlers == nil { | ||
handlers = make(map[string]func(os.Signal) error) | ||
} | ||
|
||
if _, ok := handlers[name]; ok { | ||
return errors.Errorf("handler with name %s already exists", name) | ||
} | ||
|
||
handlers[name] = handler | ||
handlerOrder = append([]string{name}, handlerOrder...) | ||
|
||
return nil | ||
} | ||
|
||
// Unregister un-registers a given shutdown handler. | ||
func Unregister(name string) error { | ||
if handlers == nil { | ||
return nil | ||
} | ||
|
||
if _, ok := handlers[name]; !ok { | ||
return nil | ||
} | ||
|
||
delete(handlers, name) | ||
|
||
newOrder := []string{} | ||
for _, checkName := range handlerOrder { | ||
if checkName != name { | ||
newOrder = append(newOrder, checkName) | ||
} | ||
} | ||
handlerOrder = newOrder | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just reset handler to new func?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to allow multiple handlers at the same time - theoretically the Libpod handler could run after the server handler and do any extra shutdown needed there.