Watching a ChangeStream doesn't handle Disconnect/Reconnect

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Gone away
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Using the following code

      ctx := context.Background()
      
      c := h.db.Collection("col")
      cs, err := c.Watch(ctx, []bson.M{})
      if err != nil {
      	log.Println("cannot watch for changes", err)
      }
      defer cs.Close(ctx)
      
      for cs.Next(ctx) {
      	doc := bson.M{}
      	if err := cs.Decode(&doc); err != nil {
      		continue
      	}
      
      	// do something with doc
      }
      
      if cs.Err() != nil {
      	log.Println("error reading changestream", cs.Err())
      }
      

       
      When the MongoDB server goes down or gets restarted, the for loop keeps hanging and doesn't receive any more data or error.
      Working with a time-constrained context seems wrong, since I actually want to listen endlessly.
      IMHO either the driver should silently handle the reconnect and setup the change stream again, or it should properly signal the lost session/connection by returning from the Next() call with an error, so I have a chance to reestablish the change stream myself.

            Assignee:
            Divjot Arora (Inactive)
            Reporter:
            Andreas Schneider
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: