libpod: rework shutdown handler flow

Currently podman run -d can exit 0 if we send SIGTERM during startup
even though the contianer was never started. That just doesn't make any
sense is horribly confusing for a external job manager like systemd.

The original motivation was to exit 0 for the podman.service in commit
ca7376bb11. That does make sense but it should only do so for the
service and only if the server did indeed gracefully shutdown.

So we rework how the exit logic works, do not let the handler perform
the exit. Instead the shutdown package does the exit after all handlers
are run, this solves the issue of ordering. Then we default to exit code
1 like we did before and allow the service exit handler to overwrite the
exit code 0 in case of a graceful shutdown.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-09-26 16:14:30 +02:00
parent 2de82d523b
commit 0bbef4b830
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
3 changed files with 17 additions and 7 deletions

View File

@ -218,11 +218,6 @@ func newRuntimeFromConfig(ctx context.Context, conf *config.Config, options ...R
if runtime.store != nil { if runtime.store != nil {
_, _ = runtime.store.Shutdown(false) _, _ = runtime.store.Shutdown(false)
} }
// For `systemctl stop podman.service` support, exit code should be 0
if sig == syscall.SIGTERM {
os.Exit(0)
}
os.Exit(1)
return nil return nil
}); err != nil && !errors.Is(err, shutdown.ErrHandlerExists) { }); err != nil && !errors.Is(err, shutdown.ErrHandlerExists) {
logrus.Errorf("Registering shutdown handler for libpod: %v", err) logrus.Errorf("Registering shutdown handler for libpod: %v", err)

View File

@ -28,10 +28,18 @@ var (
shutdownInhibit sync.RWMutex shutdownInhibit sync.RWMutex
logrus = logrusImport.WithField("PID", os.Getpid()) logrus = logrusImport.WithField("PID", os.Getpid())
ErrNotStarted = errors.New("shutdown signal handler has not yet been started") ErrNotStarted = errors.New("shutdown signal handler has not yet been started")
// exitCode used to exit once we are done with all signal handlers, by default 1
exitCode = 1
) )
// SetExitCode when we exit after we ran all shutdown handlers, it should be positive.
func SetExitCode(i int) {
exitCode = i
}
// Start begins handling SIGTERM and SIGINT and will run the given on-signal // 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(). // handlers when one is called and then exit with the exit code of 1 if not
// overwritten with SetExitCode(). This can be cancelled by calling Stop().
func Start() error { func Start() error {
if sigChan != nil { if sigChan != nil {
// Already running, do nothing. // Already running, do nothing.
@ -75,6 +83,7 @@ func Start() error {
} }
handlerLock.Unlock() handlerLock.Unlock()
shutdownInhibit.Unlock() shutdownInhibit.Unlock()
os.Exit(exitCode)
return return
} }
}() }()

View File

@ -197,7 +197,13 @@ func (s *APIServer) Serve() error {
s.setupPprof() s.setupPprof()
if err := shutdown.Register("service", func(sig os.Signal) error { if err := shutdown.Register("service", func(sig os.Signal) error {
return s.Shutdown(true) err := s.Shutdown(true)
if err == nil {
// For `systemctl stop podman.service` support, exit code should be 0
// but only if we did indeed gracefully shutdown
shutdown.SetExitCode(0)
}
return err
}); err != nil { }); err != nil {
return err return err
} }