Skip to content

Commit 1984e2c

Browse files
authored
Merge pull request #4936 from cyphar/1.4-fix-prepare-cgroup-fd-close
[1.4] libct: close child fds on prepareCgroupFD error
2 parents bd6021d + 42b405d commit 1984e2c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

libcontainer/process_linux.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,31 +353,38 @@ func (p *setnsProcess) prepareCgroupFD() (*os.File, error) {
353353
return fd, nil
354354
}
355355

356-
func (p *setnsProcess) start() (retErr error) {
357-
defer p.comm.closeParent()
356+
// startWithCgroupFD starts a process via clone3 with CLONE_INTO_CGROUP,
357+
// with a fallback if it fails (e.g. not available).
358+
func (p *setnsProcess) startWithCgroupFD() error {
359+
// Close the child side of the pipes.
360+
defer p.comm.closeChild()
358361

359362
fd, err := p.prepareCgroupFD()
360363
if err != nil {
361364
return err
362365
}
363-
364-
// Get the "before" value of oom kill count.
365-
oom, _ := p.manager.OOMKillCount()
366-
367-
err = p.startWithCPUAffinity()
368366
if fd != nil {
369-
fd.Close()
367+
defer fd.Close()
370368
}
369+
370+
err = p.startWithCPUAffinity()
371371
if err != nil && p.cmd.SysProcAttr.UseCgroupFD {
372372
logrus.Debugf("exec with CLONE_INTO_CGROUP failed: %v; retrying without", err)
373373
// SysProcAttr.CgroupFD is never used when UseCgroupFD is unset.
374374
p.cmd.SysProcAttr.UseCgroupFD = false
375375
err = p.startWithCPUAffinity()
376376
}
377377

378-
// Close the child-side of the pipes (controlled by child).
379-
p.comm.closeChild()
380-
if err != nil {
378+
return err
379+
}
380+
381+
func (p *setnsProcess) start() (retErr error) {
382+
defer p.comm.closeParent()
383+
384+
// Get the "before" value of oom kill count.
385+
oom, _ := p.manager.OOMKillCount()
386+
387+
if err := p.startWithCgroupFD(); err != nil {
381388
return fmt.Errorf("error starting setns process: %w", err)
382389
}
383390

0 commit comments

Comments
 (0)