diff --git a/rsocket-core/src/main/java/io/rsocket/core/RSocketRequester.java b/rsocket-core/src/main/java/io/rsocket/core/RSocketRequester.java index c10e86d56..bf298706a 100644 --- a/rsocket-core/src/main/java/io/rsocket/core/RSocketRequester.java +++ b/rsocket-core/src/main/java/io/rsocket/core/RSocketRequester.java @@ -345,15 +345,17 @@ private void terminate(Throwable e) { requesterLeaseTracker.dispose(e); } + final Collection activeStreamsCopy; synchronized (this) { final IntObjectMap activeStreams = this.activeStreams; - final Collection activeStreamsCopy = new ArrayList<>(activeStreams.values()); - for (FrameHandler handler : activeStreamsCopy) { - if (handler != null) { - try { - handler.handleError(e); - } catch (Throwable ignored) { - } + activeStreamsCopy = new ArrayList<>(activeStreams.values()); + } + + for (FrameHandler handler : activeStreamsCopy) { + if (handler != null) { + try { + handler.handleError(e); + } catch (Throwable ignored) { } } } diff --git a/rsocket-core/src/main/java/io/rsocket/core/RSocketResponder.java b/rsocket-core/src/main/java/io/rsocket/core/RSocketResponder.java index b2f084f51..ce4fe70a3 100644 --- a/rsocket-core/src/main/java/io/rsocket/core/RSocketResponder.java +++ b/rsocket-core/src/main/java/io/rsocket/core/RSocketResponder.java @@ -185,15 +185,18 @@ final void doOnDispose() { requestHandler.dispose(); } - private synchronized void cleanUpSendingSubscriptions() { - final IntObjectMap activeStreams = this.activeStreams; - final Collection activeStreamsCopy = new ArrayList<>(activeStreams.values()); + private void cleanUpSendingSubscriptions() { + final Collection activeStreamsCopy; + synchronized (this) { + final IntObjectMap activeStreams = this.activeStreams; + activeStreamsCopy = new ArrayList<>(activeStreams.values()); + } + for (FrameHandler handler : activeStreamsCopy) { if (handler != null) { handler.handleCancel(); } } - activeStreams.clear(); } final void handleFrame(ByteBuf frame) {