Skip to content

Commit

Permalink
libimage: pull: increase timeout running under systemd
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vrothberg committed Aug 16, 2023
1 parent b70b0c4 commit b95e0be
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions libimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"io"
"net"
"os"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b95e0be

Please sign in to comment.