[GODRIVER-1214] Cancelling the context associated with a query does not actually cancel the query Created: 02/Aug/19  Updated: 27/Oct/23  Resolved: 05/Aug/19

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

Type: Bug Priority: Major - P3
Reporter: Matthew Zimmerman Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux, Go



 Comments   
Comment by Kristofer Brandow (Inactive) [ 06/Aug/19 ]

Hi mzimmerman@gmail.com,

The MongoDB Wire Protocol does not support the functionality you mentioned, so it would be difficult and buggy to stop the work on the server. Timing out or cancelling operations is something the MongoDB drivers team is working on and you can follow the progress of that specifications via DRIVERS-555.

 

 

Comment by Matthew Zimmerman [ 06/Aug/19 ]

A major point of the context interface is that if the context is cancelled, the work should stop.

Since net.Connection.Read() doesn't accept a context, google wrote their httpDo() method like this: https://blog.golang.org/context#TOC_3.3.  With this implementation, the server would still be working, but at least the client could exit and continue good work.

I'm not familiar with the mongodb wire protocol, but I would imagine that it receives the opid from the server when a command is run such that when requests/responses are combined together over the same network stream that the client and server can determine which request goes with which response.  If that's the case, then the client would have an opid that it could cancel the work on the server when the context is cancelled.  Thus, the server would return an error on the net.Conn.Read() call and the original thread would be cancelled as well as the work on the server.

I found your response to be uninspiring and unhelpful and untrue.  The net.Dial methods do accept a context; but they only recognize it for the connection attempt; once the network socket is established.  You are correct in that the cancellation of the context is not recognized for the duration of the connection.

Comment by Jeffrey Yemin [ 05/Aug/19 ]

Hi mzimmerman@gmail.com

The driver can't really do what you're asking because Go's net library does not take a context in any of the functions that do I/O.  Therefore cancelling the context has no effect once we're waiting for I/O.

Comment by Matthew Zimmerman [ 02/Aug/19 ]

This quits at ~20 seconds.  I would expect it to quit at ~5.

Comment by Matthew Zimmerman [ 02/Aug/19 ]

package main
 
import (
     "context"
     "log"
     "runtime"
     "time"
 
    "go.mongodb.org/mongo-driver/mongo"
     "go.mongodb.org/mongo-driver/mongo/options"
     "gopkg.in/mgo.v2/bson"
 )
 
func main() {
     options := options.Client()
     options = options.ApplyURI("mongodb://localhost:27017")
     options = options.SetMaxPoolSize(uint16(runtime.NumCPU() * 2))
     mongoSession, err := mongo.NewClient(options)
     if err != nil
 
{         log.Fatalf("Error connecting to server - %v", err)     }
     ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
     defer cancel()
     err = mongoSession.Connect(ctx)
     if err != nil \{         log.Fatalf("Error connecting to server - %v", err)     }
 
    start := time.Now()
     go func()
 
{         time.Sleep(time.Second * 5)         cancel()         log.Printf("Cancelled in duration of %s", time.Now().Sub(start))     }
 
()
     cur, err := mongoSession.Database("ccid3").Collection("transactions").Find(ctx, bson.M{"$where": "function ()
 
{ sleep(100); return false}
 
"})
     if err != nil
 
{         log.Fatalf("Error with query - %s - %v", time.Now().Sub(start), err)     }
 
    log.Printf("Result came back in %s", time.Now().Sub(start))
     for cur.Next(ctx)
 
{     }
 
    log.Printf("Finished - %s - %v - %v", time.Now().Sub(start), err, ctx.Err())
 }

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