Skip to content

Commit b954c7a

Browse files
committed
Prevent presumably bogus reentrancy onPostStop when onCreateRuntime errored
Signed-off-by: apostasie <[email protected]>
1 parent 6296bb3 commit b954c7a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

pkg/ocihook/ocihook.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,24 +505,37 @@ func onCreateRuntime(opts *handlerOpts) error {
505505
log.L.WithError(err).Error("failed re-acquiring name - see https://github.com/containerd/nerdctl/issues/2992")
506506
}
507507

508+
var netError error
508509
if opts.cni != nil {
509-
if err = applyNetworkSettings(opts); err != nil {
510-
return err
511-
}
510+
netError = applyNetworkSettings(opts)
512511
}
513512

514-
// Set StartedAt
515513
lf := state.NewLifecycleState(opts.state.Annotations[labels.StateDir])
516-
return lf.WithLock(func() error {
514+
515+
return errors.Join(netError, lf.WithLock(func() error {
517516
// Errors are voluntarily ignored here, as they should not be fatal.
518517
// The lifecycle struct is also already warning about the issue.
519518
_ = lf.Load()
520519
lf.StartedAt = time.Now()
520+
lf.CreateError = netError != nil
521521
return lf.Save()
522-
})
522+
}))
523523
}
524524

525525
func onPostStop(opts *handlerOpts) error {
526+
// See https://github.com/containerd/nerdctl/issues/3357
527+
// Check if we actually errored during runtimeCreate
528+
// If that is the case, CreateError is set, and we are in postStop while the container will NOT be deleted (see ticket).
529+
// In that case, do NOT treat this as a deletion, as the container is still there.
530+
// Reset CreateError, and return.
531+
lf := state.NewLifecycleState(opts.state.Annotations[labels.StateDir])
532+
if lf.WithLock(lf.Load) == nil {
533+
if lf.CreateError {
534+
lf.CreateError = false
535+
return lf.WithLock(lf.Save)
536+
}
537+
}
538+
526539
ctx := context.Background()
527540
ns := opts.state.Annotations[labels.Namespace]
528541
if opts.cni != nil {

pkg/ocihook/state/state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ func NewLifecycleState(stateDir string) *LifecycleState {
5151
}
5252

5353
type LifecycleState struct {
54-
stateDir string
55-
StartedAt time.Time `json:"started_at"`
54+
stateDir string
55+
StartedAt time.Time `json:"started_at"`
56+
CreateError bool `json:"create_error"`
5657
}
5758

5859
func (lf *LifecycleState) WithLock(fun func() error) error {

0 commit comments

Comments
 (0)