From b95e0be76ce42fa90abab81cf4e4c5b2d2496b27 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 16 Aug 2023 13:47:02 +0200 Subject: [PATCH] libimage: pull: increase timeout running under systemd Set the `EXTEND_TIMEOUT_USEC` over DBUS when pulling an image from a registry and when running under systemd. This will prevent a frequent issue when running Quadlets and exceeding the default systemd start timeout of 90 seconds when pulling the image takes too long. Fixes: containers/podman/issues/18353 Signed-off-by: Valentin Rothberg --- libimage/pull.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/libimage/pull.go b/libimage/pull.go index 296d003a8..a2a19b5c5 100644 --- a/libimage/pull.go +++ b/libimage/pull.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "io" + "net" + "os" "runtime" "strings" "time" @@ -379,6 +381,43 @@ func (r *Runtime) copyFromRegistry(ctx context.Context, ref types.ImageReference return nil, err } + // Avoid running out of time when running inside a systemd unit by + // regularly increasing the timeout. + if socketPath, ok := os.LookupEnv("NOTIFY_SOCKET"); ok { + socketAddr := &net.UnixAddr{ + Name: socketPath, + Net: "unixgram", + } + conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr) + if err != nil { + return nil, err + } + defer conn.Close() + + timeout := 20 * time.Second + increaseTimeout := func() { + // See upstream systemd change adding support for EXTEND_TIMEOUT_USEC: + // https://github.com/systemd/systemd/commit/a327431bd168b2f327f3cd422379e213c643f2a5 + if _, err := conn.Write([]byte("EXTEND_TIMEOUT_USEC=" + timeout.Microseconds())); err != nil { + logrus.Errorf("Increasing EXTEND_TIMEOUT_USEC failed: %v", err) + } + } + + increaseTimeout() + socketCtx, cancel := context.WithCancel(ctx) + defer cancel() + go func() { + for { + select { + case <-socketCtx.Done(): + return + case <-time.After(timeout): + increaseTimeout() + } + } + }() + } + if !options.AllTags { return r.copySingleImageFromRegistry(ctx, inputName, pullPolicy, options) }