From 908dd96015d84d16639cb16dd627bfcdba7d1b49 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 27 Oct 2023 21:55:44 -0400 Subject: [PATCH] Make `EventTypeTracker` actually thread-safe Although I don't think anyone has actually triggered this bug in practice, we should still fix this. --- .../proxy/event/EventTypeTracker.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java b/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java index d4f65c5a0..45cfba70a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java @@ -17,29 +17,36 @@ package com.velocitypowered.proxy.event; -import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Multimap; import com.google.common.reflect.TypeToken; import java.util.Collection; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; class EventTypeTracker { - private final Multimap, Class> friends; + private final ConcurrentMap, ImmutableSet>> friends; public EventTypeTracker() { - this.friends = HashMultimap.create(); + this.friends = new ConcurrentHashMap<>(); } public Collection> getFriendsOf(final Class eventType) { if (friends.containsKey(eventType)) { - return ImmutableSet.copyOf(friends.get(eventType)); + return friends.get(eventType); } final Collection> types = getFriendTypes(eventType); for (Class type : types) { - friends.put(type, eventType); + this.friends.merge( + type, + ImmutableSet.of(eventType), + (oldVal, newSingleton) -> ImmutableSet.>builder() + .addAll(oldVal) + .addAll(newSingleton) + .build() + ); } return types; }