[GODRIVER-1247] connection leak with change stream Created: 22/Aug/19  Updated: 28/Oct/23  Resolved: 04/Sep/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 1.1.0
Fix Version/s: 1.1.1

Type: Bug Priority: Major - P3
Reporter: Michael O'Brien Assignee: Alice Thum
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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



 Comments   
Comment by Githook User [ 05/Sep/19 ]

Author:

{'email': 'alice.thum@10gen.com', 'name': 'Alice Thum'}

Message: Close connections in executeOperation.

GODRIVER-1247

Change-Id: Icf35cbb89050bec1b08833d57942cd08d4e9460e
Branch: release/1.1
https://github.com/mongodb/mongo-go-driver/commit/5cb43987dd7f92340b94f84aac91bc0c03c78f22

Comment by Alice Thum [ 04/Sep/19 ]

commit link: https://github.com/mongodb/mongo-go-driver/commit/7b15653fa692979af75c4d4ca512b7df58873d28

Comment by Githook User [ 04/Sep/19 ]

Author:

{'email': 'alice.thum@10gen.com', 'name': 'Alice Thum'}

Message: Close connections in executeOperation.

GODRIVER-1247

Change-Id: Icf35cbb89050bec1b08833d57942cd08d4e9460e
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/7b15653fa692979af75c4d4ca512b7df58873d28

Comment by Alice Thum [ 04/Sep/19 ]

gerrithub link: https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/467299

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