[GODRIVER-1697] Cursor.Close accepts context which could result in cancelling the close operation Created: 20/Jul/20  Updated: 27/Oct/23  Resolved: 23/Jul/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: 1.3.5
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Shay Katz Assignee: Divjot Arora (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

mongodb-go-driver accepts a context for the Cursor.CloseSession.EndSession, transaction, etc. This looks like a problem to me, as context is used for cancellation and cancelling a close operation is not really desired (Leaking the cursor).

I can try just passing context.Background() specifically to such close methods, but that seems very error prone.

You would often use context by taking the HTTP request context, which gets cancelled if the connection is severed for any reason, and possibly add a Mongo session on top of it if desired (UseSession). If I then just pass context.Background(), it won't use the session for those close operations which I'm not sure is correct or even going to work.



 Comments   
Comment by Divjot Arora (Inactive) [ 23/Jul/20 ]

Hi shay@codeocean.com,

The context.Context type in Go is an overloaded concept. As you mention, it is used for timeouts/cancellation, but it can also be used to store a map of values associated with an operation via the context.WithValue function. This is an extremely useful functionality and we've heard of some users combining it with our command monitoring API (https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo/options?tab=doc#ClientOptions.SetMonitor and https://pkg.go.dev/go.mongodb.org/mongo-driver/event?tab=doc#CommandMonitor) to do operation tracing. Removing contexts from the functions you mention would be a backwards-breaking API change and could break things for users who are doing this.

You can use context.Background() for these operations. Cursor.Close, Session.EndSession and Session.AbortTransaction all get the session from elsewhere rather than extracting it from the context. Version 1.4.0 of the driver will also include a non-callback based sessions API, so you would be able to create a new SessionContext based on context.Background() if needed.

Given all of this, I'm going to close out this ticket. Feel free to leave another comment or open a new ticket if you have any suggestions!

– Divjot

Generated at Thu Feb 08 08:36:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.