Skip to content

Commit

Permalink
fix hang with podman events file logger
Browse files Browse the repository at this point in the history
podman --events-backend file events --stream=false should never hang. The
problem is that our tail library will wait for the file to be created
which makes sense when we do not run with --stream=false. To fix this we
can just always create the file when the logger is initialized. This
would also help to report errors early on in case the file is not
accessible.

Fixes part one from containers#15688

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 12, 2022
1 parent 5abc08d commit c5bdb6a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libpod/events/events_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewEventer(options EventerOptions) (Eventer, error) {
}
return eventer, nil
case strings.ToUpper(LogFile.String()):
return EventLogFile{options}, nil
return newLogFileEventer(options)
case strings.ToUpper(Null.String()):
return NewNullEventer(), nil
case strings.ToUpper(Memory.String()):
Expand Down
18 changes: 18 additions & 0 deletions libpod/events/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"time"

"github.com/containers/podman/v4/pkg/util"
Expand All @@ -27,6 +28,21 @@ type EventLogFile struct {
options EventerOptions
}

// newLogFileEventer creates a new EventLogFile eventer
func newLogFileEventer(options EventerOptions) (*EventLogFile, error) {
// Create events log dir
if err := os.MkdirAll(filepath.Dir(options.LogFilePath), 0700); err != nil {
return nil, fmt.Errorf("creating events dirs: %w", err)
}
// We have to make sure the file is created otherwise reading events will hang.
// https://github.com/containers/podman/issues/15688
fd, err := os.OpenFile(options.LogFilePath, os.O_RDONLY|os.O_CREATE, 0700)
if err != nil {
return nil, err
}
return &EventLogFile{options: options}, fd.Close()
}

// Writes to the log file
func (e EventLogFile) Write(ee Event) error {
// We need to lock events file
Expand Down Expand Up @@ -108,6 +124,8 @@ func (e EventLogFile) Read(ctx context.Context, options ReadOptions) error {
}
}()
}
logrus.Debugf("Reading events from file %q", e.options.LogFilePath)

var line *tail.Line
var ok bool
for {
Expand Down
8 changes: 0 additions & 8 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,6 @@ func makeRuntime(runtime *Runtime) (retErr error) {
}
}

// Create events log dir
if err := os.MkdirAll(filepath.Dir(runtime.config.Engine.EventsLogFilePath), 0700); err != nil {
// The directory is allowed to exist
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("creating events dirs: %w", err)
}
}

// Get us at least one working OCI runtime.
runtime.ociRuntimes = make(map[string]OCIRuntime)

Expand Down
6 changes: 5 additions & 1 deletion test/system/090-events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ function _populate_events_file() {

# Config without a limit
eventsFile=$PODMAN_TMPDIR/events.txt
_populate_events_file $eventsFile
containersConf=$PODMAN_TMPDIR/containers.conf
cat >$containersConf <<EOF
[engine]
events_logger="file"
events_logfile_path="$eventsFile"
EOF

# Check that a non existing event file does not cause a hang (#15688)
CONTAINERS_CONF=$containersConf run_podman events --stream=false

_populate_events_file $eventsFile

# Create events *without* a limit and make sure that it has not been
# rotated/truncated.
contentBefore=$(head -n100 $eventsFile)
Expand Down

0 comments on commit c5bdb6a

Please sign in to comment.