EsotericSoftware / EsotericSoftware/kryonet

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

Open
#164 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.9k
Forks
411
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.