From ee7ac3497b0287a076b025597db7b1f17897a10e Mon Sep 17 00:00:00 2001 From: Krzysztof Jaskiewicz Date: Mon, 6 May 2024 14:51:36 +0200 Subject: [PATCH] fix: check if context is nil before calling ctx.Deadline 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