From 4f2fc729ce99124a49581f7eda594406b6ec5206 Mon Sep 17 00:00:00 2001 From: Allo Date: Fri, 3 Feb 2023 14:12:52 +0800 Subject: [PATCH] fix(app/log): prevent close of closed channel The close of `done` channel may be called many times. And the handler will be blocked when the log client exists and the closure func is still not be called. So use context to resolve those two problems. --- app/log/command/command.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/log/command/command.go b/app/log/command/command.go index 2fe457c22..2524c08bf 100644 --- a/app/log/command/command.go +++ b/app/log/command/command.go @@ -43,18 +43,18 @@ func (s *LoggerServer) FollowLog(_ *FollowLogRequest, stream LoggerService_Follo if !ok { return newError("logger not support following") } - done := make(chan struct{}) + ctx, cancel := context.WithCancel(stream.Context()) f := func(msg cmlog.Message) { err := stream.Send(&FollowLogResponse{ Message: msg.String(), }) if err != nil { - close(done) + cancel() } } follower.AddFollower(f) defer follower.RemoveFollower(f) - <-done + <-ctx.Done() return nil }