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

Make ChangeStream.Close thread safe

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      The Go Documentation on ChangeStream.Close says

      Next and TryNext must not be called after Close has been called.

      If this is not honored by a user, they can get a fairly confusing panic:

      go.mongodb.org/mongo-driver/mongo.(*ChangeStream).loopNext(0xc000002140, {0x124ca00, 0x18e8040}, 0x0)
      	/go/pkg/mod/go.mongodb.org/mongo-driver@v1.12.1/mongo/change_stream.go:658 +0x6b
      go.mongodb.org/mongo-driver/mongo.(*ChangeStream).next(0xc000002140, {0x124ca00?, 0x18e8040?}, 0xd0?)
      	/go/pkg/mod/go.mongodb.org/mongo-driver@v1.12.1/mongo/change_stream.go:627 +0x5d
      go.mongodb.org/mongo-driver/mongo.(*ChangeStream).Next(...)
      	/go/pkg/mod/go.mongodb.org/mongo-driver@v1.12.1/mongo/change_stream.go:598
      draw-platform/storage/mongodb.(*StorageCollection).WatchRunnerResponse.func1()
      

      See here for an example: https://gist.github.com/prestonvasquez/30f15e9f84c76a63e6bf818663a18309

      The Go Driver could communicate to the Next / TryNext method from the Close method using a buffered channel, something like this:

      // This would be in the Next function:
      select {
      case <-close: // chan struct{}
          // Close the change stream.
          return
      default:
          // Run the next logic
      }
      

      Definition of done: determine if this change is worth doing or not, if it is worth doing then implement the change.

            Assignee:
            Unassigned Unassigned
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: