From f7176d6d9b96311405c5f3b08b8f49820c064d8b Mon Sep 17 00:00:00 2001 From: Tomasz Adamski Date: Thu, 11 Jul 2024 20:51:46 +0200 Subject: [PATCH] [EJBCLIENT-536] RemotingEJBDiscoveryProvider: run cancel handlers outside the synchronized block --- .../remote/RemotingEJBDiscoveryProvider.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java index 25d181b7b..0ec255890 100644 --- a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java +++ b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java @@ -591,8 +591,9 @@ public void addMatch(final ServiceURL serviceURL) { public void cancel() { final List cancellers = this.cancellers; + final List cancellersCopy; synchronized (cancellers) { - /** + /* In scenario in which the last node performs a cancel operation and discovery fails a retry is attempted (see phase2 in countdown method above). This triggers opening new connections and, as a result, adding cancellers, which are being iterated over in this method leading to @@ -600,10 +601,14 @@ public void cancel() { See https://issues.redhat.com/browse/JBEAP-24568 */ - final List cancellersCopy = new ArrayList<>(cancellers); - for (Runnable canceller : cancellersCopy) { - canceller.run(); - } + cancellersCopy = new ArrayList<>(cancellers); + } + /* + * EJBCLIENT-536 - since we are iterating the copy we can safely do it outside synchronized block; running + * the cancellers synchronized may cause deadlocks + */ + for (Runnable canceller : cancellersCopy) { + canceller.run(); } }