|
50 | 50 | import io.rsocket.keepalive.KeepAliveSupport;
|
51 | 51 | import io.rsocket.lease.RequesterLeaseHandler;
|
52 | 52 | import java.nio.channels.ClosedChannelException;
|
| 53 | +import java.util.ArrayList; |
| 54 | +import java.util.Collection; |
53 | 55 | import java.util.concurrent.atomic.AtomicBoolean;
|
54 | 56 | import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
55 | 57 | import java.util.function.Consumer;
|
@@ -772,20 +774,26 @@ private void terminate(Throwable e) {
|
772 | 774 | leaseHandler.dispose();
|
773 | 775 |
|
774 | 776 | // Iterate explicitly to handle collisions with concurrent removals
|
775 |
| - for (IntObjectMap.PrimitiveEntry<Processor<Payload, Payload>> entry : receivers.entries()) { |
| 777 | + final IntObjectMap<Processor<Payload, Payload>> receivers = this.receivers; |
| 778 | + // copy to avoid collection modification from the foreach loop |
| 779 | + final Collection<Processor<Payload, Payload>> receiversCopy = |
| 780 | + new ArrayList<>(receivers.values()); |
| 781 | + for (Processor<Payload, Payload> handler : receiversCopy) { |
776 | 782 | try {
|
777 |
| - entry.value().onError(e); |
| 783 | + handler.onError(e); |
778 | 784 | } catch (Throwable ex) {
|
779 | 785 | if (LOGGER.isDebugEnabled()) {
|
780 | 786 | LOGGER.debug("Dropped exception", ex);
|
781 | 787 | }
|
782 | 788 | }
|
783 | 789 | }
|
784 |
| - |
785 | 790 | // Iterate explicitly to handle collisions with concurrent removals
|
786 |
| - for (IntObjectMap.PrimitiveEntry<Subscription> entry : senders.entries()) { |
| 791 | + final IntObjectMap<Subscription> senders = this.senders; |
| 792 | + // copy to avoid collection modification from the foreach loop |
| 793 | + final Collection<Subscription> sendersCopy = new ArrayList<>(senders.values()); |
| 794 | + for (Subscription subscription : sendersCopy) { |
787 | 795 | try {
|
788 |
| - entry.value().cancel(); |
| 796 | + subscription.cancel(); |
789 | 797 | } catch (Throwable ex) {
|
790 | 798 | if (LOGGER.isDebugEnabled()) {
|
791 | 799 | LOGGER.debug("Dropped exception", ex);
|
|
0 commit comments