From 7350df9fbfd3377f080511b8a9a60f2d33ff76ea Mon Sep 17 00:00:00 2001 From: Zhonghu Xu Date: Tue, 30 Jul 2024 14:28:12 +0800 Subject: [PATCH] Fix kmesh daemon graceful exit Signed-off-by: Zhonghu Xu --- build/docker/start_kmesh.sh | 9 ++++++++- pkg/status/status_server.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/build/docker/start_kmesh.sh b/build/docker/start_kmesh.sh index 035e6eb7b..180a2fe4a 100755 --- a/build/docker/start_kmesh.sh +++ b/build/docker/start_kmesh.sh @@ -31,6 +31,7 @@ pid=$! # pass SIGTERM to kmesh process function stop_kmesh() { + echo "received SIGTERM, stopping kmesh" kill $pid } @@ -43,8 +44,14 @@ function cleanup(){ umount -t cgroup2 /mnt/kmesh_cgroup2/ rm -rf /mnt/kmesh_cgroup2 rm -rf /sys/fs/bpf/bpf_kmesh + echo "kmesh exit" } trap 'stop_kmesh' SIGTERM -wait # wait child process exit + +# wait for kmesh process to exit, cannot use wait $pid here, because the script received SIGTERM, wait will return immediately +while kill -0 $pid 2>/dev/null; do + sleep 1 +done + cleanup diff --git a/pkg/status/status_server.go b/pkg/status/status_server.go index ffebe6b54..80d317e33 100644 --- a/pkg/status/status_server.go +++ b/pkg/status/status_server.go @@ -323,7 +323,7 @@ func (s *Server) setBpfLogLevel(w http.ResponseWriter, levelStr string) { func (s *Server) StartServer() { go func() { err := s.server.ListenAndServe() - if err != nil { + if err != nil && err != http.ErrServerClosed { log.Errorf("Failed to start status server: %v", err) } }()