Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1247

connection leak with change stream

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.1.1
    • Affects Version/s: 1.1.0
    • Component/s: CRUD
    • None

      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

            Assignee:
            alice.thum@mongodb.com Alice Thum
            Reporter:
            mikeo@mongodb.com Michael O'Brien
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: