GlowstoneMC/Glowstone — pull request #473 by coelho (2014-10-22 13:24:06+00:00)
Title: Fix race conditions

There are actually a lot of race conditions in Glowstone's network implementation. This PR fixes two notable ones that were reported.

#421
This is caused by the player entity being removed from an asynchronous thread while the synchronous thread is iterating over the map of entities.
This is resolved by removing the player entity from the synchronous thread.

#154
This is actually a very rare race condition, but it is nice to see that it was reproduced. This is where the channel is disconnected asynchronously at the exact same time that the GlowSession.pulse(); synchronous method is called. Because the EncryptionKeyResponse is handled on the synchronous thread, we would then handle it, and since we are not in the PlayProtocol state yet, we wouldn't acknowledge that the session has disconnected.

Due to the way that we fix #421 adding a "disconnected" variable instead of relying on it being in the PlayProtocol to be able to notice if it's disconnected, this is naively fixed or the window is actually narrowed for this issue to be a lot more slim, however it is not a proper fix, and there is still definitely a silent race condition.

The real solution to this issue is to handle the EncryptionKeyResponse asynchronously.