1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-12 02:40:42 +00:00

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.
This commit is contained in:
Allo 2023-02-03 14:12:52 +08:00 committed by Shelikhoo
parent 75ece5b6f6
commit 4f2fc729ce
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -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
}