]
Bela Ban updated JGRP-1877:
---------------------------
Priority: Major (was: Critical)
Downgraded; not as much impact as I thought, e.g.
{code:java}
long target_time=nanos() + timeout_ns;
while(...) {
wait_time=target_time - nanos();
if(wait_time <= 0)
break;
}
{code}
Var {{wait_time}} will always be positive !
What won't work is code like the following:
{code:java}
while(nanos() < target_time) {
...
}
{code}
Here, the current time could still be possitve while {{target_time}} could be negative.
The correct code would be
{code:java}
while(target_time - nanos() > 0) {...}
{code}
System.nanoTime() can be negative
---------------------------------
Key: JGRP-1877
URL:
https://issues.jboss.org/browse/JGRP-1877
Project: JGroups
Issue Type: Bug
Reporter: Bela Ban
Assignee: Bela Ban
Fix For: 3.5.1, 3.6
According to the javadoc, {{System.nanoTime()}} should only be used to measure _elapsed
time_, but not compute a _target time in the future_, as {{nanoTime()}} might return a a
time in the future.
Code like the one below might fail:
{code:title=Responses.waitFor()|borderStyle=solid}
public boolean waitFor(long timeout) {
long wait_time;
final long target_time=System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeout,
TimeUnit.MILLISECONDS); // ns
lock.lock();
try {
while(!done && (wait_time=target_time - System.nanoTime()) > 0) {
try {
cond.await(wait_time,TimeUnit.NANOSECONDS);
}
catch(InterruptedException e) {
}
}
return done;
}
finally {
lock.unlock();
}
}
{code}
When computing {{target_time}}, {{System.nanoTime()}} could return a negative value
(numeric overflow) or a value in the future. In the first case, {{target_time}} could be
negative, so the method would not block at all. In the latter case, {{target_time}} could
be huge, so the method would block for a long time.
Investigate all occurrences where we use {{nanoTime()}} to compute a time in the future,
and see what impact a future value value could have. Possibly replace with
{{System.currentTimeMillis()}} or the _time service_.