[GODRIVER-2998] Make ChangeStream.Close thread safe Created: 28/Sep/23  Updated: 02/Oct/23

Status: Backlog
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Preston Vasquez Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to GODRIVER-2959 Mongodb watch change stream will pani... Closed

 Description   

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.


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