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

Internal context leak in client connection #5336

Closed
carzil opened this issue May 4, 2022 · 0 comments · Fixed by #5337
Closed

Internal context leak in client connection #5336

carzil opened this issue May 4, 2022 · 0 comments · Fixed by #5337

Comments

@carzil
Copy link
Contributor

carzil commented May 4, 2022

What version of gRPC are you using?

v1.46.0

What version of Go are you using (go version)?

go version go1.17 darwin/amd64

What operating system (Linux, Windows, …) and version?

MacOS

What did you do?

Run following code:

package main

import (
	"context"
	"log"
	"net/http"
	_ "net/http/pprof"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/backoff"
	"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

func main() {
	go func() {
		if err := http.ListenAndServe("127.0.0.1:9090", nil); err != nil {
			log.Fatalf("http err: %s", err)
		}
	}()

	del := 500 * time.Microsecond

	// Connection to this address should cause some error (connection refused, i/o timeout, etc)
	conn, err := grpc.Dial("127.0.0.1:12131", grpc.WithInsecure(), grpc.WithConnectParams(
		grpc.ConnectParams{
			Backoff: backoff.Config{
				BaseDelay:  del,
				Multiplier: 1,
				Jitter:     0,
				MaxDelay:   del,
			},
		},
	))
	if err != nil {
		log.Fatalf("err: %s", err)
	}

	c := grpc_reflection_v1alpha.NewServerReflectionClient(conn)

	for {
		_, _ = c.ServerReflectionInfo(context.Background())
		time.Sleep(del)
	}
}

What did you expect to see?

I did expect that this code doesn't cause memory leak.

What did you see instead?

This code leaks. Also, del could be decreased: this code will receive i/o timeout instead of connection refused, but will leak faster.
Heap profile:
profile001

I've found that:

  1. hctx is allocated here.
  2. Corresponding cancel function hcancel is called in onClose.
  3. But onClose is not called, if newHTTP2Client fails before transport is created (here, for example).
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant