Skip to content

Commit bde4e56

Browse files
author
Isabella Siu
committed
GODRIVER-910 don't panic when calling watch on a disconnected client
Change-Id: Ie665018d55c4bdcfa84f472a39d25a5e0a43a4a8
1 parent 9da818d commit bde4e56

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

mongo/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ func (c *Client) UseSessionWithOptions(ctx context.Context, opts *options.Sessio
531531
// The client must have read concern majority or no read concern for a change stream to be created successfully.
532532
func (c *Client) Watch(ctx context.Context, pipeline interface{},
533533
opts ...*options.ChangeStreamOptions) (*ChangeStream, error) {
534+
if c.topology.SessionPool == nil {
535+
return nil, ErrClientDisconnected
536+
}
534537

535538
return newClientChangeStream(ctx, c, pipeline, opts...)
536539
}

mongo/client_internal_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,12 @@ func TestClient_Disconnect_NilContext(t *testing.T) {
536536
err = c.Disconnect(nil)
537537
require.NoError(t, err)
538538
}
539+
540+
func TestClient_Watch_Disconnected(t *testing.T) {
541+
cs := testutil.ConnString(t)
542+
c, err := NewClient(options.Client().ApplyURI(cs.String()))
543+
require.NoError(t, err)
544+
change, err := c.Watch(context.Background(), []bson.D{})
545+
require.Nil(t, change)
546+
require.Equal(t, err, ErrClientDisconnected)
547+
}

0 commit comments

Comments
 (0)