Skip to content

Commit 45a880f

Browse files
committed
Write provisional state.json with PID -1 before cgroup Apply
1 parent 4ccedcb commit 45a880f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

libcontainer/process_linux.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,24 @@ func (p *initProcess) start() (retErr error) {
819819
}
820820
}()
821821

822+
// Write a provisional state.json before cgroup Apply. This ensures that
823+
// if the runc-create process is killed (e.g., SIGKILL from a higher-level
824+
// runtime due to timeout), runc-delete can still find and clean up the
825+
// container's cgroup and state directory.
826+
//
827+
// We temporarily nil out c.initProcess so that the saved state uses
828+
// PID -1 (the default when initProcess is nil). This prevents external
829+
// tools from seeing the container as "created" before the init process
830+
// is fully set up, avoiding a race where "runc start" could be called
831+
// with a stale STAGE_PARENT PID that will be reaped during creation.
832+
savedInit := p.container.initProcess
833+
p.container.initProcess = nil
834+
_, uerr := p.container.updateState(nil)
835+
p.container.initProcess = savedInit
836+
if uerr != nil {
837+
return fmt.Errorf("unable to store init state: %w", uerr)
838+
}
839+
822840
// Do this before syncing with child so that no children can escape the
823841
// cgroup. We don't need to worry about not doing this and not being root
824842
// because we'd be using the rootless cgroup manager in that case.

0 commit comments

Comments
 (0)