gcpug/zagane

unclosetx: false positive detection with errgroup

Opened this issue · 2 comments

When I use errgroup, zagane reports transaction must be closed. I think that it's false positive detection.

sample code:

func f5(ctx context.Context, client *spanner.Client) error {
	tx := client.ReadOnlyTransaction() // OK
	defer tx.Close()

	var eg errgroup.Group

	eg.Go(func() error {
		_ = tx // use tx
		return nil
	})

	if err := eg.Wait(); err != nil {
		return err
	}
	return nil
}

more simple sample code:

func f5(ctx context.Context, client *spanner.Client) error {
	tx := client.ReadOnlyTransaction() // OK
	defer tx.Close()

	func() error {
		_ = tx // use tx
		return nil
	}()

	return nil
}

I think this issue has two bugs.
First bug is false positive of tx := client.ReadOnlyTransaction().
Second bug is false positive of _ = tx // use tx.
Actually the below code zagane reports false positive in only case (2).

func() error {
	func(tx *spanner.ReadOnlyTransaction) {}(tx) // (1) OK
	_ = tx // (2) NG
	return nil
}()