Skip to content

server.update(int timeout) returning after incorrect timeout.  #164

@ent-moot

Description

@ent-moot

There's a "bug" whereby the server.update() method might take longer to return than specified.

E.g. a call of server.update(10) may take 25ms to return.

This issue is caused because of the way in which a workaround has been implemented for an issue with NIO.

The fix is very straight-forward:

Replace this code, in Server.java:

// NIO freaks and returns immediately with 0 sometimes, so try to keep from hogging the CPU.
long elapsedTime = System.currentTimeMillis() - startTime;
try {
if (elapsedTime < 25) Thread.sleep(25 - elapsedTime);
} catch (InterruptedException ex) {
}

with :

// NIO freaks and returns immediately with 0 sometimes, so try to keep from hogging the CPU.
long elapsedTime = System.currentTimeMillis() - startTime;
try {
int targetDuration = Math.min(timeout,25);
if (elapsedTime < targetDuration) Thread.sleep(targetDuration - elapsedTime);
} catch (InterruptedException ex) {
}

This ensures that, even if NIO "freaks out", we:

  1. Still avoid hogging the CPU
  2. BUT also avoid sleeping longer than the specified timeout

I have built and tested this locally, and it works, but not sure how to go about submitting it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions