From fba5a1ec2358e2523fec6f5ecdd4514dbdf2666c Mon Sep 17 00:00:00 2001 From: lifubang Date: Sat, 22 Mar 2025 00:08:16 +0000 Subject: [PATCH] move process terminate operation to defer function Signed-off-by: lifubang --- utils_linux.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/utils_linux.go b/utils_linux.go index edf7a4cfd..33cd41dd0 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -286,22 +286,23 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) { if err != nil { return -1, err } + defer func() { + // We should terminate the process once we got an error. + if retErr != nil { + r.terminate(process) + } + }() if err = tty.waitConsole(); err != nil { - r.terminate(process) return -1, err } tty.ClosePostStart() if r.pidFile != "" { if err = createPidFile(r.pidFile, process); err != nil { - r.terminate(process) return -1, err } } handler := <-handlerCh status, err := handler.forward(process, tty, detach) - if err != nil { - r.terminate(process) - } if detach { return 0, nil }