From de082e5627ada4672882001c28ee279f01386d5e Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Wed, 14 Oct 2020 09:32:11 +0300 Subject: [PATCH] ensure we cancel child context when reading grpc Signed-off-by: Avi Deitcher --- content/proxy/content_reader.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/content/proxy/content_reader.go b/content/proxy/content_reader.go index b06e48fa9..2947a7c82 100644 --- a/content/proxy/content_reader.go +++ b/content/proxy/content_reader.go @@ -40,7 +40,13 @@ func (ra *remoteReaderAt) ReadAt(p []byte, off int64) (n int, err error) { Offset: off, Size_: int64(len(p)), } - rc, err := ra.client.Read(ra.ctx, rr) + // we need a child context with cancel, or the eventually called + // grpc.NewStream will leak the goroutine until the whole thing is cleared. + // See comment at https://godoc.org/google.golang.org/grpc#ClientConn.NewStream + childCtx, cancel := context.WithCancel(ra.ctx) + // we MUST cancel the child context; see comment above + defer cancel() + rc, err := ra.client.Read(childCtx, rr) if err != nil { return 0, err }