From 4add5f4402f17f178c6e8596d03835c3837a5994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Ja=C5=9Bkiewicz?= Date: Wed, 8 May 2024 22:39:45 +0200 Subject: [PATCH] fix: check if context is nil before calling ctx.Deadline (#1630) Signed-off-by: Krzysztof Jaskiewicz --- mongo/collection.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mongo/collection.go b/mongo/collection.go index 80a06f5b6d..4cf6fd1a1a 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -1200,6 +1200,11 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i // For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/. func (coll *Collection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (cur *Cursor, err error) { + + if ctx == nil { + ctx = context.Background() + } + // Omit "maxTimeMS" from operations that return a user-managed cursor to // prevent confusing "cursor not found" errors. To maintain existing // behavior for users who set "timeoutMS" with no context deadline, only @@ -1217,10 +1222,6 @@ func (coll *Collection) find( opts ...*options.FindOptions, ) (cur *Cursor, err error) { - if ctx == nil { - ctx = context.Background() - } - f, err := marshal(filter, coll.bsonOpts, coll.registry) if err != nil { return nil, err