Skip to content

Commit e3ab8bd

Browse files
committed
Avoid allocating slice for finding Process
Signed-off-by: zyu <[email protected]>
1 parent aac6a51 commit e3ab8bd

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

runtime/v1/shim/service.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -503,42 +503,37 @@ func (s *Service) processExits() {
503503
}
504504
}
505505

506-
func (s *Service) allProcesses() []process.Process {
507-
s.mu.Lock()
508-
defer s.mu.Unlock()
509-
510-
res := make([]process.Process, 0, len(s.processes))
511-
for _, p := range s.processes {
512-
res = append(res, p)
513-
}
514-
return res
515-
}
516-
517506
func (s *Service) checkProcesses(e runc.Exit) {
518-
for _, p := range s.allProcesses() {
519-
if p.Pid() != e.Pid {
520-
continue
507+
var p process.Process
508+
s.mu.Lock()
509+
for _, proc := range s.processes {
510+
if proc.Pid() == e.Pid {
511+
p = proc
512+
break
521513
}
522-
523-
if ip, ok := p.(*process.Init); ok {
524-
// Ensure all children are killed
525-
if shouldKillAllOnExit(s.context, s.bundle) {
526-
if err := ip.KillAll(s.context); err != nil {
527-
log.G(s.context).WithError(err).WithField("id", ip.ID()).
528-
Error("failed to kill init's children")
529-
}
514+
}
515+
s.mu.Unlock()
516+
if p == nil {
517+
log.G(s.context).Infof("process with id:%d wasn't found", e.Pid)
518+
return
519+
}
520+
if ip, ok := p.(*process.Init); ok {
521+
// Ensure all children are killed
522+
if shouldKillAllOnExit(s.context, s.bundle) {
523+
if err := ip.KillAll(s.context); err != nil {
524+
log.G(s.context).WithError(err).WithField("id", ip.ID()).
525+
Error("failed to kill init's children")
530526
}
531527
}
528+
}
532529

533-
p.SetExited(e.Status)
534-
s.events <- &eventstypes.TaskExit{
535-
ContainerID: s.id,
536-
ID: p.ID(),
537-
Pid: uint32(e.Pid),
538-
ExitStatus: uint32(e.Status),
539-
ExitedAt: p.ExitedAt(),
540-
}
541-
return
530+
p.SetExited(e.Status)
531+
s.events <- &eventstypes.TaskExit{
532+
ContainerID: s.id,
533+
ID: p.ID(),
534+
Pid: uint32(e.Pid),
535+
ExitStatus: uint32(e.Status),
536+
ExitedAt: p.ExitedAt(),
542537
}
543538
}
544539

0 commit comments

Comments
 (0)