Replace an assert with a small hack.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 22 Jul 2015 00:12:37 +0000 (02:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 22 Jul 2015 00:12:37 +0000 (02:12 +0200)
Instead of checking that connect times are monotonic, explicitly make them
so if they're not. This seems safest, in case the monotonic clock goes
backwards a small bit (e.g. when changing CPUs). We don't need it for the
serialized case since we explicitly sort those by time; the assert can stay.

server.cpp

index 01ed0ff..079b629 100644 (file)
@@ -217,9 +217,15 @@ void Server::add_client(int sock)
        assert(ret.second == true);  // Should not already exist.
        Client *client_ptr = &ret.first->second;
 
-       // Connection timestamps must be nondecreasing.
-       assert(clients_ordered_by_connect_time.empty() ||
-              !is_earlier(client_ptr->connect_time, clients_ordered_by_connect_time.back().first));
+       // Connection timestamps must be nondecreasing. I can't find any guarantee
+       // that even the monotonic clock can't go backwards by a small amount
+       // (think switching between CPUs with non-synchronized TSCs), so if
+       // this actually should happen, we hack around it by fudging
+       // connect_time.
+       if (!clients_ordered_by_connect_time.empty() &&
+           is_earlier(client_ptr->connect_time, clients_ordered_by_connect_time.back().first)) {
+               client_ptr->connect_time = clients_ordered_by_connect_time.back().first;
+       }
        clients_ordered_by_connect_time.push(make_pair(client_ptr->connect_time, sock));
 
        // Start listening on data from this socket.