Mojira Archive
MC-125762

Sometimes a few Minecraft servers display "Pinging..." followed by "Can't connect to server" in multiplayer server list

The bug

A few Minecraft servers display "Pinging..." followed by "Can't connect to server", occasionally. This appears to happen when opening the multiplayer/server list for the first time or after the minecraft client startup or when refreshed the multiplayer server lists.

Code analysis

It appears that most parts of the code analysis of MC-73207 apply again. There has been some refactoring (LazyLoadBase uses a Supplier now) which likely replaced the fix by accident.

The following class might be a better replacement for or addition to the LazyLoadBase class. The only restriction is that the factory should be thread-safe.

public class ThreadSafeLazySupplier<T> implements Supplier<T> {
    private final Supplier<T> factory;
    private final Object valueCreationMonitor;
    private volatile boolean hasValue;
    private volatile T value;
    
    public ThreadSafeLazySupplier(final Supplier<T> factory) {
        this.factory = factory;
        valueCreationMonitor = new Object();
        hasValue = false;
    }
    
    @Override
    public T get() {
        if (!hasValue) {
            synchronized(valueCreationMonitor) {
                if (!hasValue) {
                    value = factory.get();
                    hasValue = true;
                }
            }
        }
        
        return value;
    }
}