From 4cd4b60352bc8a572a0a9482c58564397c49446c Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Mon, 12 Nov 2018 11:11:47 -0800 Subject: [PATCH] runsc: generate exec pidfile after everything is ready. PiperOrigin-RevId: 221123160 Change-Id: Ia7061d60d114d69f49aba853fe6bae3c733522b5 --- runsc/cmd/exec.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/runsc/cmd/exec.go b/runsc/cmd/exec.go index 9a395e6f1..35aa5499e 100644 --- a/runsc/cmd/exec.go +++ b/runsc/cmd/exec.go @@ -146,12 +146,6 @@ func (ex *Exec) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) return ex.execAndWait(waitStatus) } - if ex.pidFile != "" { - if err := ioutil.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil { - Fatalf("error writing pid file: %v", err) - } - } - // Start the new process and get it pid. pid, err := c.Execute(e) if err != nil { @@ -173,6 +167,15 @@ func (ex *Exec) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) } } + // Generate the pid file after the internal pid file is generated, so that users + // can safely assume that the internal pid file is ready after `runsc exec -d` + // returns. + if ex.pidFile != "" { + if err := ioutil.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil { + Fatalf("error writing pid file: %v", err) + } + } + // Wait for the process to exit. ws, err := c.WaitPID(pid, ex.clearStatus) if err != nil {