diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go index 174dffeeb0..3edd6e6d47 100644 --- a/libpod/boltdb_state.go +++ b/libpod/boltdb_state.go @@ -1451,7 +1451,8 @@ func (s *BoltState) GetContainerExitCodeTimeStamp(id string) (*time.Time, error) }) } -// PruneExitCodes removes exit codes older than 5 minutes. +// PruneExitCodes removes exit codes older than 5 minutes unless the associated +// container still exists. func (s *BoltState) PruneContainerExitCodes() error { if !s.valid { return define.ErrDBClosed @@ -1472,7 +1473,17 @@ func (s *BoltState) PruneContainerExitCodes() error { return err } + ctrsBucket, err := getCtrBucket(tx) + if err != nil { + return err + } + return timeStampBucket.ForEach(func(rawID, rawTimeStamp []byte) error { + if ctrsBucket.Bucket(rawID) != nil { + // If the container still exists, don't prune + // its exit code since we may still need it. + return nil + } var timeStamp time.Time if err := timeStamp.UnmarshalText(rawTimeStamp); err != nil { return fmt.Errorf("converting raw time stamp %v of container %s from DB: %w", rawTimeStamp, string(rawID), err) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index 7c94c6f208..fd207acb36 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -943,7 +943,8 @@ func (s *SQLiteState) GetContainerExitCodeTimeStamp(id string) (*time.Time, erro return &result, nil } -// PruneExitCodes removes exit codes older than 5 minutes. +// PruneExitCodes removes exit codes older than 5 minutes unless the associated +// container still exists. func (s *SQLiteState) PruneContainerExitCodes() (defErr error) { if !s.valid { return define.ErrDBClosed @@ -963,7 +964,7 @@ func (s *SQLiteState) PruneContainerExitCodes() (defErr error) { } }() - if _, err := tx.Exec("DELETE FROM ContainerExitCode WHERE (Timestamp <= ?);", fiveMinsAgo); err != nil { + if _, err := tx.Exec("DELETE FROM ContainerExitCode WHERE (Timestamp <= ?) AND (ID NOT IN (SELECT ID FROM ContainerConfig))", fiveMinsAgo); err != nil { return fmt.Errorf("removing exit codes with timestamps older than 5 minutes: %w", err) }