From 773deca2f6507a22f66843a2a7c9fbafe6a06cb2 Mon Sep 17 00:00:00 2001 From: bin Date: Thu, 27 May 2021 14:57:29 +0800 Subject: [PATCH] virtiofsd: Fix file descriptors leak and return correct PID This commit will fix two problems: - Virtiofsd process ID returned to the caller will always be 0, the pid var is never being assigned a value. - Socket listen fd may leak in case of failure of starting virtiofsd process. This is a port of https://github.com/kata-containers/kata-containers/commit/be9ca0d58b2b777beda02f59e443c1dc67d0b5f7 Fixes: #1931 Signed-off-by: bin --- src/runtime/virtcontainers/virtiofsd.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/runtime/virtcontainers/virtiofsd.go b/src/runtime/virtcontainers/virtiofsd.go index 9b4bc27aac..be2c0e069c 100644 --- a/src/runtime/virtcontainers/virtiofsd.go +++ b/src/runtime/virtcontainers/virtiofsd.go @@ -75,6 +75,8 @@ func (v *virtiofsd) getSocketFD() (*os.File, error) { if err != nil { return nil, err } + + // no longer needed since fd is a dup defer listener.Close() listener.SetUnlinkOnClose(false) @@ -98,6 +100,7 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) { if err != nil { return 0, err } + defer socketFD.Close() cmd.ExtraFiles = append(cmd.ExtraFiles, socketFD) @@ -128,7 +131,7 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) { v.wait = waitVirtiofsReady } - return pid, socketFD.Close() + return cmd.Process.Pid, nil } func (v *virtiofsd) Stop(ctx context.Context) error {