Using the latest, when creating change streams with Watch(), it looks like connections may be getting leaked.
Here's a minimal test case that illustrates the behavior:
package main import ( "context" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "log" "sync" "time" ) func main() { client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { log.Fatal("failed to build client", err) } err = client.Connect(context.Background()) if err != nil { log.Fatal("failed to connect", err) } coll := client.Database("test").Collection("test") wg := sync.WaitGroup{} for i := 0; i < 50; i++ { wg.Add(1) go func() { defer wg.Done() cursor, err := coll.Watch(context.Background(), bson.D{}) if err != nil { log.Fatal("failed to watch: ", err) } defer cursor.Close(context.Background()) // consume the cursor but bail out after 5 sec ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) _ = cursor.Next(ctx) fmt.Println("err", cursor.Err()) }() } wg.Wait() for { fmt.Println("now sleeping here") time.Sleep(time.Second) } }
When the code gets into the sleep loop at the end, there are lots of connections still open against the server. Running the same code using an older version of the driver results in the connection count going back down. Using git bisect, it looks like the change in behavior was introduced in df0f8c44059d04f60f45ebc8510065d4357cb408