From 0ea5303fe17ab10184341b7a35b4306e4db1e9d3 Mon Sep 17 00:00:00 2001 From: zulkhair <53172227+zulkhair@users.noreply.github.com> Date: Thu, 2 May 2024 16:45:23 +0000 Subject: [PATCH] fix: world cardinal dev not exit on compilation error (#63) Closes: WORLD-1079 ## Overview Fix `world cardinal dev` is not exited when there is a compilation error, user don't need to send sigint/sigterm to exit the process. ## Brief Changelog - add step to send signterm when `cmd.Exec` process is done / exited - skip killing the `cmd.Exec` if process is already exited. ## Testing and Verifying Manually tested by running `world cardinal dev` --- cmd/world/cardinal/dev.go | 15 +++++++++++++++ cmd/world/root/root_test.go | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index bb8c7e6..751e074 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -136,6 +136,17 @@ var devCmd = &cobra.Command{ return err } + // Start a goroutine to check cmd is stopped + go func() { + err := execCmd.Wait() + if err != nil { + logger.Error(eris.Wrap(err, "Cardinal process stopped")) + } + + // if process exited, send signal to StopChan + signalCh <- syscall.SIGTERM + }() + // Start a goroutine to listen for signals go func() { <-signalCh @@ -255,6 +266,10 @@ func stopCardinal(cmd *exec.Cmd, watch bool) error { return nil } + if cmd.ProcessState == nil || cmd.ProcessState.Exited() { + return nil + } + // stop the cardinal process if runtime.GOOS == "windows" { err := cmd.Process.Kill() diff --git a/cmd/world/root/root_test.go b/cmd/world/root/root_test.go index 7d793b4..7891ec3 100644 --- a/cmd/world/root/root_test.go +++ b/cmd/world/root/root_test.go @@ -214,7 +214,7 @@ func TestDev(t *testing.T) { assert.NilError(t, err) // Start cardinal dev - rootCmd.SetArgs([]string{"cardinal", "dev", "--watch"}) + rootCmd.SetArgs([]string{"cardinal", "dev"}) go func() { err := rootCmd.Execute() assert.NilError(t, err) @@ -238,7 +238,7 @@ func TestDev(t *testing.T) { assert.Assert(t, isGameLoopRunning) // Shutdown the program - close(cardinal.StopChan) + cardinal.StopChan <- struct{}{} // Check cardinal health (expected error), trying for 10 times count := 0