-
Bug
-
Resolution: Fixed
-
Minecraft 1.8, Minecraft 1.9, Minecraft 1.10.2, Minecraft 16w41a, Minecraft 16w42a, Minecraft 1.11.2, Minecraft 1.12.2, Minecraft 17w50a
-
Confirmed
The bug
Whenever I start my game and go onto the Multiplayer server list, the servers all say "Can't connect to server" but when I refresh the page, it says the normal servers MOTD in the listing.
Code analysis (MCP names), comments, and additional note by MWisBest
I've analyzed the issue and found it's a race condition with the multi-threaded server pinger code. The server pinging code (net.minecraft.client.network.ServerPinger) is run in multiple threads, and each thread calls net.minecraft.network.NetworkManager.createNetworkManagerAndConnect(). This function uses "lazy loaders" (net.minecraft.util.LazyLoadBase) to initialize some static variables. The issue lies in net.minecraft.util.LazyLoadBase.getValue(). This function is not thread-safe. Below you'll find the code in question with comments explaining exactly what is happening and how to fix it.
package net.minecraft.util; public abstract class LazyLoadBase<T> { private T value; private boolean isLoaded; public T getValue() // NOT THREAD-SAFE. SIMPLE FIX: declare the method synchronized { if (!this.isLoaded) // Thread 1: Condition is true; Thread 2: Condition is false { this.isLoaded = true; // Thread 1: SETS isLoaded to true, BUT VALUE IS STILL NULL this.value = this.load(); } return this.value; // Thread 2: RETURNS NULL because this.load() is not finished in Thread 1 } protected abstract T load(); }
Note that depending on the speed of the computer, this bug might not occur. If the first thread finishes quickly enough everything will appear normal.
- is cloned by
-
MC-125762 Sometimes a few Minecraft servers display "Pinging..." followed by "Can't connect to server" in multiplayer server list
- Open
- is duplicated by
-
MC-73373 Server List is not loaded at start-up
- Resolved
-
MC-74260 Server list shows "Can't connect to server" right away even if server is up
- Resolved
-
MC-109066 multiplayer bug report! On first join minecraft, a few servers is currently offline.
- Resolved
-
MC-117129 on startup Minecraft, multiplayer servers is offline
- Resolved
-
MC-119126 Pinging servers randomly fails on first attempt (multithread race condition)
- Resolved