Skip to content

Commit baa75c5

Browse files
committed
Omit CSOT-calculated maxTimeMS if it's greater than max int32.
1 parent 832e502 commit baa75c5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

mongo/integration/csot_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ func TestCSOT(t *testing.T) {
234234
sendsMaxTimeMSWithTimeoutMS: true,
235235
sendsMaxTimeMSWithContextDeadline: true,
236236
preventsConnClosureWithTimeoutMS: true,
237-
topologies: []mtest.TopologyKind{mtest.ReplicaSet},
237+
// Change Streams aren't supported on standalone topologies.
238+
topologies: []mtest.TopologyKind{
239+
mtest.ReplicaSet,
240+
mtest.Sharded,
241+
},
238242
},
239243
{
240244
desc: "Cursor getMore",
@@ -436,6 +440,21 @@ func TestCSOT(t *testing.T) {
436440
}
437441
})
438442
}
443+
444+
csotOpts := mtest.NewOptions().ClientOptions(options.Client().SetTimeout(10 * time.Second))
445+
mt.RunOpts("maxTimeMS is omitted for values greater than 2147483647ms]", csotOpts, func(mt *mtest.T) {
446+
ctx, cancel := context.WithTimeout(context.Background(), (2147483647+1)*time.Millisecond)
447+
defer cancel()
448+
_, err := mt.Coll.InsertOne(ctx, bson.D{})
449+
require.NoError(t, err)
450+
451+
evt := mt.GetStartedEvent()
452+
_, err = evt.Command.LookupErr("maxTimeMS")
453+
assert.ErrorIs(mt,
454+
err,
455+
bsoncore.ErrElementNotFound,
456+
"expected maxTimeMS BSON value to be missing, but is present")
457+
})
439458
}
440459

441460
func TestCSOT_errors(t *testing.T) {

x/mongo/driver/operation.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,15 @@ func (op Operation) calculateMaxTimeMS(ctx context.Context, mon RTTMonitor) (uin
15881588
ErrDeadlineWouldBeExceeded)
15891589
}
15901590

1591+
// The server will return a "BadValue" error if maxTimeMS is greater
1592+
// than the maximum positive int32 value (about 24.9 days). If the
1593+
// user specified a timeout value greater than that, omit maxTimeMS
1594+
// and let the client-side timeout handle cancelling the op if the
1595+
// timeout is ever reached.
1596+
if maxTimeMS > math.MaxInt32 {
1597+
return 0, nil
1598+
}
1599+
15911600
return uint64(maxTimeMS), nil
15921601
}
15931602
} else if op.MaxTime != nil {

0 commit comments

Comments
 (0)