Skip to content

Commit

Permalink
Omit CSOT-calculated maxTimeMS if it's greater than max int32.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdale committed Apr 11, 2024
1 parent 832e502 commit baa75c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 20 additions & 1 deletion mongo/integration/csot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ func TestCSOT(t *testing.T) {
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
topologies: []mtest.TopologyKind{mtest.ReplicaSet},
// Change Streams aren't supported on standalone topologies.
topologies: []mtest.TopologyKind{
mtest.ReplicaSet,
mtest.Sharded,
},
},
{
desc: "Cursor getMore",
Expand Down Expand Up @@ -436,6 +440,21 @@ func TestCSOT(t *testing.T) {
}
})
}

csotOpts := mtest.NewOptions().ClientOptions(options.Client().SetTimeout(10 * time.Second))
mt.RunOpts("maxTimeMS is omitted for values greater than 2147483647ms]", csotOpts, func(mt *mtest.T) {
ctx, cancel := context.WithTimeout(context.Background(), (2147483647+1)*time.Millisecond)
defer cancel()
_, err := mt.Coll.InsertOne(ctx, bson.D{})
require.NoError(t, err)

evt := mt.GetStartedEvent()
_, err = evt.Command.LookupErr("maxTimeMS")
assert.ErrorIs(mt,
err,
bsoncore.ErrElementNotFound,
"expected maxTimeMS BSON value to be missing, but is present")
})
}

func TestCSOT_errors(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions x/mongo/driver/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,15 @@ func (op Operation) calculateMaxTimeMS(ctx context.Context, mon RTTMonitor) (uin
ErrDeadlineWouldBeExceeded)
}

// The server will return a "BadValue" error if maxTimeMS is greater
// than the maximum positive int32 value (about 24.9 days). If the
// user specified a timeout value greater than that, omit maxTimeMS
// and let the client-side timeout handle cancelling the op if the
// timeout is ever reached.
if maxTimeMS > math.MaxInt32 {
return 0, nil
}

return uint64(maxTimeMS), nil
}
} else if op.MaxTime != nil {
Expand Down

0 comments on commit baa75c5

Please sign in to comment.