From c6cc6098a6153777a406de6ad14a028328f29a10 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 94d94fa8a..808e79b92 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/event/EventTypeTracker.java @@ -17,24 +17,24 @@ 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 = getEventTypes(eventType); @@ -43,7 +43,14 @@ class EventTypeTracker { continue; } - friends.put(type, eventType); + this.friends.merge( + type, + ImmutableSet.of(eventType), + (oldVal, newSingleton) -> ImmutableSet.>builder() + .addAll(oldVal) + .addAll(newSingleton) + .build() + ); } return types; }