From 702dad6cec667f5e3f5632da70b86647e9faf08e Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 2 Sep 2019 12:42:14 +0200 Subject: [PATCH] Fix ConcurrentModificationException --- .../core/io/buffer/LeakAwareDataBufferFactory.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java index 7a65300e924..c9f79c26307 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java @@ -21,6 +21,7 @@ import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import io.netty.buffer.PooledByteBufAllocator; @@ -50,6 +51,8 @@ public class LeakAwareDataBufferFactory implements DataBufferFactory { private final List created = new ArrayList<>(); + private final AtomicBoolean trackCreated = new AtomicBoolean(true); + /** * Creates a new {@code LeakAwareDataBufferFactory} by wrapping a @@ -75,6 +78,7 @@ public class LeakAwareDataBufferFactory implements DataBufferFactory { * method. */ public void checkForLeaks() { + this.trackCreated.set(false); Instant start = Instant.now(); while (true) { if (this.created.stream().noneMatch(LeakAwareDataBuffer::isAllocated)) { @@ -112,7 +116,9 @@ public class LeakAwareDataBufferFactory implements DataBufferFactory { @NotNull private DataBuffer createLeakAwareDataBuffer(DataBuffer delegateBuffer) { LeakAwareDataBuffer dataBuffer = new LeakAwareDataBuffer(delegateBuffer, this); - this.created.add(dataBuffer); + if (this.trackCreated.get()) { + this.created.add(dataBuffer); + } return dataBuffer; }