Skip to content

Commit a244d57

Browse files
authored
Merge pull request opencontainers#3198 from Vanient/master
optimize log: move WriteJSON defer as early as possible
2 parents 963e014 + 64358c4 commit a244d57

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

libcontainer/factory_linux.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/opencontainers/runc/libcontainer/configs/validate"
2323
"github.com/opencontainers/runc/libcontainer/intelrdt"
2424
"github.com/opencontainers/runc/libcontainer/utils"
25+
"github.com/sirupsen/logrus"
2526
)
2627

2728
const (
@@ -346,11 +347,26 @@ func (l *LinuxFactory) StartInitialization() (err error) {
346347
envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE")
347348
pipefd, err := strconv.Atoi(envInitPipe)
348349
if err != nil {
349-
return fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
350+
err = fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
351+
logrus.Error(err)
352+
return err
350353
}
351354
pipe := os.NewFile(uintptr(pipefd), "pipe")
352355
defer pipe.Close()
353356

357+
defer func() {
358+
// We have an error during the initialization of the container's init,
359+
// send it back to the parent process in the form of an initError.
360+
if werr := utils.WriteJSON(pipe, syncT{procError}); werr != nil {
361+
fmt.Fprintln(os.Stderr, err)
362+
return
363+
}
364+
if werr := utils.WriteJSON(pipe, &initError{Message: err.Error()}); werr != nil {
365+
fmt.Fprintln(os.Stderr, err)
366+
return
367+
}
368+
}()
369+
354370
// Only init processes have FIFOFD.
355371
fifofd := -1
356372
envInitType := os.Getenv("_LIBCONTAINER_INITTYPE")
@@ -382,18 +398,6 @@ func (l *LinuxFactory) StartInitialization() (err error) {
382398
// specific env vars.
383399
os.Clearenv()
384400

385-
defer func() {
386-
// We have an error during the initialization of the container's init,
387-
// send it back to the parent process in the form of an initError.
388-
if werr := utils.WriteJSON(pipe, syncT{procError}); werr != nil {
389-
fmt.Fprintln(os.Stderr, err)
390-
return
391-
}
392-
if werr := utils.WriteJSON(pipe, &initError{Message: err.Error()}); werr != nil {
393-
fmt.Fprintln(os.Stderr, err)
394-
return
395-
}
396-
}()
397401
defer func() {
398402
if e := recover(); e != nil {
399403
err = fmt.Errorf("panic from initialization: %w, %v", e, string(debug.Stack()))

0 commit comments

Comments
 (0)