Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,20 @@ public void run() {
};
ServerConnection clientConnection = new ServerConnection(client, timeout);
Thread clientThread = factory.newThread(clientConnection);
synchronized (timeout) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're no longer synchronizing on the timeout here, but I didn't see anywhere else synchronizing on it either (including ServerConnection). Given it doesn't escape this method, I'm not sure how multiple threads could ever access timeout at once, so it makes sense to remove this synchronization

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was exactly my reasoning. I couldn't find any reason why the synchronisation was needed. The removal was only a means to make the code simpler removing the cognitive cost suggested by the presence of a synchronise keyword. The actual fix doesn't depend on it.

clientThread.start();
synchronized (clients) {
clients.add(clientConnection);
}
long timeoutMs = getConnectionTimeout();
// 0 is used for testing to avoid issues with clock resolution / thread scheduling,
// and force an immediate timeout.
if (timeoutMs > 0) {
timeoutTimer.schedule(timeout, getConnectionTimeout());
} else {
timeout.run();
}
synchronized (clients) {
clients.add(clientConnection);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are now adding to clients before starting the clientThread instead of after. What's the expected ordering for these two operations?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the connection to clients before starting the thread should be safer since otherwise the thread could have a chance of running and terminating before the connection is added to clients. This would cause the addition of an already terminated connection to the clients list which nobody would ever cleanup (i.e. memory leak).

This situation is really unlikely but still a possibility.

Changing the order of operation shouldn't affect any logic since the clients list is only used for cleanup in the close method.

}

long timeoutMs = getConnectionTimeout();
// 0 is used for testing to avoid issues with clock resolution / thread scheduling,
// and force an immediate timeout.
if (timeoutMs > 0) {
timeoutTimer.schedule(timeout, timeoutMs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on not calling getConnectionTimeout() multiple times

} else {
timeout.run();
}

clientThread.start();
}
} catch (IOException ioe) {
if (running) {
Expand Down