Skip to content

Commit 99246d6

Browse files
authored
Fix ClientChannelEvent.Timeout and ClientSessionEvent.TIMEOUT (#790)
Fix AbstractClientChannel.waitFor() and ClientSessionImpl.waitFor(); they could time-out much too early. On some hosts, on which waiting for the `futureLock.wait(remWait)` is faster than 1 millisecond, `remWait` gets reduced by 123 milliseconds although not even one passed. This causes a `ClientChannelEvent.Timeout` or `ClientSessionEvent.TIMEOUT` after a much smaller time passed than the configured timeout. With these changes, the `remWait` is calculated based on the milliseconds passed instead of transformed nanoseconds.
1 parent 2868de8 commit 99246d6

2 files changed

Lines changed: 2 additions & 18 deletions

File tree

sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.EnumSet;
2828
import java.util.Objects;
2929
import java.util.Set;
30-
import java.util.concurrent.TimeUnit;
3130
import java.util.concurrent.atomic.AtomicBoolean;
3231
import java.util.concurrent.atomic.AtomicReference;
3332

@@ -262,6 +261,7 @@ public Set<ClientChannelEvent> waitFor(Collection<ClientChannelEvent> mask, long
262261
if (timeout > 0L) {
263262
long now = System.currentTimeMillis();
264263
long usedTime = now - startTime;
264+
remWait = timeout - usedTime;
265265
if ((usedTime >= timeout) || (remWait <= 0L)) {
266266
if (traceEnabled) {
267267
log.trace("waitFor({}) call timeout {}/{} for mask={}: {}",
@@ -290,14 +290,6 @@ public Set<ClientChannelEvent> waitFor(Collection<ClientChannelEvent> mask, long
290290
if (traceEnabled) {
291291
log.trace("waitFor({}) lock notified on channel after {} nanos", this, nanoDuration);
292292
}
293-
294-
if (timeout > 0L) {
295-
long waitDuration = TimeUnit.MILLISECONDS.convert(nanoDuration, TimeUnit.NANOSECONDS);
296-
if (waitDuration <= 0L) {
297-
waitDuration = 123L;
298-
}
299-
remWait -= waitDuration;
300-
}
301293
} catch (InterruptedException e) {
302294
long nanoEnd = System.nanoTime();
303295
long nanoDuration = nanoEnd - nanoStart;

sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Map;
2929
import java.util.Objects;
3030
import java.util.Set;
31-
import java.util.concurrent.TimeUnit;
3231
import java.util.concurrent.atomic.AtomicBoolean;
3332
import java.util.concurrent.atomic.AtomicReference;
3433

@@ -266,6 +265,7 @@ public Set<ClientSessionEvent> waitFor(Collection<ClientSessionEvent> mask, long
266265
if (timeout > 0L) {
267266
long now = System.currentTimeMillis();
268267
long usedTime = now - startTime;
268+
remWait = timeout - usedTime;
269269
if ((usedTime >= timeout) || (remWait <= 0L)) {
270270
if (traceEnabled) {
271271
log.trace("waitFor({}) call timeout {}/{} for mask={}: {}",
@@ -294,14 +294,6 @@ public Set<ClientSessionEvent> waitFor(Collection<ClientSessionEvent> mask, long
294294
if (traceEnabled) {
295295
log.trace("waitFor({}) Lock notified after {} nanos", this, nanoDuration);
296296
}
297-
298-
if (timeout > 0L) {
299-
long waitDuration = TimeUnit.MILLISECONDS.convert(nanoDuration, TimeUnit.NANOSECONDS);
300-
if (waitDuration <= 0L) {
301-
waitDuration = 123L;
302-
}
303-
remWait -= waitDuration;
304-
}
305297
} catch (InterruptedException e) {
306298
long nanoEnd = System.nanoTime();
307299
long nanoDuration = nanoEnd - nanoStart;

0 commit comments

Comments
 (0)