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

False Positive on a Lambda function that takes a Mongo SessionContext #24

Open
blainenelson opened this issue Apr 2, 2024 · 1 comment

Comments

@blainenelson
Copy link

blainenelson commented Apr 2, 2024

In the following function

func (d *storage) WithTransaction(
	ctx context.Context,
	fn func(context.Context) error,
) error {
	sess, err := d.client.StartSession()
	if err != nil {
		return err
	}
	defer sess.EndSession(ctx)

	_, err = sess.WithTransaction(
		ctx,
		func(sessCtx mongo.SessionContext) (interface{}, error) {
			return nil, fn(sessCtx)
		},
	)
	return err
}

we are now seeing a False Positive

Function `WithTransaction$1` should pass the context parameter (contextcheck)
		func(sessCtx mongo.SessionContext) (interface{}, error) {

It appears there is an issue in creating a lambda function in this way, perhaps because the lambda function takes a mongo.SessionContext instead of a context.Context even though the former extends the latter: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#SessionContext

This issue appears to have arisen with the latest release.

@kkHAIKE
Copy link
Owner

kkHAIKE commented Apr 4, 2024

The essence of this issue lies in the inability to recognize embedded situations like mongo.SessionContext at present. In the future, it will be necessary to consider adding additional context type configurations. For now, you can consider making the following modifications.

// nolint: contextcheck
func TransactionCall(ctx SessionContext, f func(ctx context.Context, sessctx SessionContext) (interface{}, error)) {
   return f(ctx.Context, ctx)
}

sess.WithTransaction(
  ctx,
  func(sessCtx mongo.SessionContext) (interface{}, error) {
	return TransactionCall(sessCtx, fn)
  },
)

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

2 participants