[JAVA-2334] Negative server selection timeout doesn't work at all (long overflow) Created: 05/Oct/16  Updated: 31/Oct/16  Resolved: 31/Oct/16

Status: Closed
Project: Java Driver
Component/s: Cluster Management
Affects Version/s: 3.2.2
Fix Version/s: 3.4.0

Type: Bug Priority: Major - P3
Reporter: Andreas Kohn Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

From BaseCluster#selectServer():

            long startTimeNanos = System.nanoTime();
            long endTimeNanos = startTimeNanos + getUseableTimeoutInNanoseconds();
            long curTimeNanos = startTimeNanos;
 
            while (true) {
                throwIfIncompatible(curDescription);
 
                if (server != null) {
                    return server;
                }
 
                if (curTimeNanos > endTimeNanos) {
                    throw createTimeoutException(serverSelector, curDescription);
                }

    private long getUseableTimeoutInNanoseconds() {
        if (settings.getServerSelectionTimeout(NANOSECONDS) < 0) {
            return Long.MAX_VALUE;
        }
        return settings.getServerSelectionTimeout(NANOSECONDS);
    }

When the server selection timeout is less than 0 (to produce 'infinite' waiting), endTimeNanos will overflow, and curTimeNanos > endTimeNanos will be trivially true.

I guess the work-around is to use a reasonably big value instead of something less-than-0, and hope the issue is fixed before that value leads to overflows

Note that master has the same issue (determined by looking at the code), also note that the #getUseableTimeoutInNanoseconds() is called from multiple places, and at least #getDescription() has the same issue.



 Comments   
Comment by Githook User [ 31/Oct/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2334: Avoid overflow when serverSelectionTimeout is negative

A negative serverSelectionTimeout is documented to mean that the driver should wait indefinitely when selecting a server.
This commit fixes the implementation by avoiding numeric overflow.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/c0c7f52f91e3e6ede061a3cf6ee0dbe48b74e902

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