Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExampleStreamDirector: context.WithCancel() and no cancel? #29

Open
pohly opened this issue Jun 13, 2018 · 0 comments
Open

ExampleStreamDirector: context.WithCancel() and no cancel? #29

pohly opened this issue Jun 13, 2018 · 0 comments

Comments

@pohly
Copy link

pohly commented Jun 13, 2018

go vet warns about ignoring the cancel function returned by context.WithCancel():

func ExampleStreamDirector() {
	director = func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
		// Make sure we never forward internal services.
		if strings.HasPrefix(fullMethodName, "/com.example.internal.") {
			return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
		}
		md, ok := metadata.FromIncomingContext(ctx)
		// Copy the inbound metadata explicitly.
		outCtx, _ := context.WithCancel(ctx)
                outCtx = metadata.NewOutgoingContext(outCtx, md.Copy())

https://stackoverflow.com/questions/44393995/what-happens-if-i-dont-cancel-a-context explains that this leads to a resource leak.

What is the purpose of creating two contexts (first with context.WithCancel, then with metadata.NewOutgoingContext)? To me it looks like the first one is simply redundant and the code can be simplified to:

		// Copy the inbound metadata explicitly.
		outCtx := metadata.NewOutgoingContext(ctx, md.Copy())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant