From 918d7229c2d134f146ae839503caa846b468a9db Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 3 Jun 2015 01:56:05 -0400 Subject: [PATCH] Fix Deadlock in EventBus post #1493 --- .../java/net/md_5/bungee/event/EventBus.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/event/src/main/java/net/md_5/bungee/event/EventBus.java b/event/src/main/java/net/md_5/bungee/event/EventBus.java index cd4057f9..00dc76d1 100644 --- a/event/src/main/java/net/md_5/bungee/event/EventBus.java +++ b/event/src/main/java/net/md_5/bungee/event/EventBus.java @@ -34,33 +34,35 @@ public class EventBus public void post(Object event) { + EventHandlerMethod[] handlers; lock.readLock().lock(); try { - EventHandlerMethod[] handlers = byEventBaked.get( event.getClass() ); - if ( handlers != null ) - { - for ( EventHandlerMethod method : handlers ) - { - try - { - method.invoke( event ); - } catch ( IllegalAccessException ex ) - { - throw new Error( "Method became inaccessible: " + event, ex ); - } catch ( IllegalArgumentException ex ) - { - throw new Error( "Method rejected target/argument: " + event, ex ); - } catch ( InvocationTargetException ex ) - { - logger.log( Level.WARNING, MessageFormat.format( "Error dispatching event {0} to listener {1}", event, method.getListener() ), ex.getCause() ); - } - } - } + handlers = byEventBaked.get( event.getClass() ); } finally { lock.readLock().unlock(); } + + if ( handlers != null ) + { + for ( EventHandlerMethod method : handlers ) + { + try + { + method.invoke( event ); + } catch ( IllegalAccessException ex ) + { + throw new Error( "Method became inaccessible: " + event, ex ); + } catch ( IllegalArgumentException ex ) + { + throw new Error( "Method rejected target/argument: " + event, ex ); + } catch ( InvocationTargetException ex ) + { + logger.log( Level.WARNING, MessageFormat.format( "Error dispatching event {0} to listener {1}", event, method.getListener() ), ex.getCause() ); + } + } + } } private Map, Map>> findHandlers(Object listener)