Skip to content

Commit 51cc719

Browse files
committed
Remove UndertowDataBuffer
This commit removes the UndertowDataBuffer, in favor of using regular DataBuffers from the DataBufferFactory. During the development of the DefaultPartHttpMessageReader, it was determined that invoking various slicing and releasing operators on the UndertowDataBuffer resulted in memory leaks. This commit fixes that.
1 parent 973ee9b commit 51cc719

File tree

1 file changed

+4
-66
lines changed

1 file changed

+4
-66
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
2323
import java.nio.ByteBuffer;
24-
import java.util.concurrent.atomic.AtomicInteger;
2524

2625
import javax.net.ssl.SSLSession;
2726

@@ -34,9 +33,6 @@
3433

3534
import org.springframework.core.io.buffer.DataBuffer;
3635
import org.springframework.core.io.buffer.DataBufferFactory;
37-
import org.springframework.core.io.buffer.DataBufferUtils;
38-
import org.springframework.core.io.buffer.DataBufferWrapper;
39-
import org.springframework.core.io.buffer.PooledDataBuffer;
4036
import org.springframework.http.HttpCookie;
4137
import org.springframework.lang.Nullable;
4238
import org.springframework.util.Assert;
@@ -172,7 +168,6 @@ protected void readingPaused() {
172168
@Nullable
173169
protected DataBuffer read() throws IOException {
174170
PooledByteBuffer pooledByteBuffer = this.byteBufferPool.allocate();
175-
boolean release = true;
176171
try {
177172
ByteBuffer byteBuffer = pooledByteBuffer.getBuffer();
178173
int read = this.channel.read(byteBuffer);
@@ -183,19 +178,17 @@ protected DataBuffer read() throws IOException {
183178

184179
if (read > 0) {
185180
byteBuffer.flip();
186-
DataBuffer dataBuffer = this.bufferFactory.wrap(byteBuffer);
187-
release = false;
188-
return new UndertowDataBuffer(dataBuffer, pooledByteBuffer);
181+
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(read);
182+
dataBuffer.write(byteBuffer);
183+
return dataBuffer;
189184
}
190185
else if (read == -1) {
191186
onAllDataRead();
192187
}
193188
return null;
194189
}
195190
finally {
196-
if (release && pooledByteBuffer.isOpen()) {
197-
pooledByteBuffer.close();
198-
}
191+
pooledByteBuffer.close();
199192
}
200193
}
201194

@@ -205,59 +198,4 @@ protected void discardData() {
205198
}
206199
}
207200

208-
209-
private static class UndertowDataBuffer extends DataBufferWrapper implements PooledDataBuffer {
210-
211-
private final PooledByteBuffer pooledByteBuffer;
212-
213-
private final AtomicInteger refCount;
214-
215-
216-
public UndertowDataBuffer(DataBuffer dataBuffer, PooledByteBuffer pooledByteBuffer) {
217-
super(dataBuffer);
218-
this.pooledByteBuffer = pooledByteBuffer;
219-
this.refCount = new AtomicInteger(1);
220-
}
221-
222-
private UndertowDataBuffer(DataBuffer dataBuffer, PooledByteBuffer pooledByteBuffer,
223-
AtomicInteger refCount) {
224-
super(dataBuffer);
225-
this.refCount = refCount;
226-
this.pooledByteBuffer = pooledByteBuffer;
227-
}
228-
229-
@Override
230-
public boolean isAllocated() {
231-
return this.refCount.get() > 0;
232-
}
233-
234-
@Override
235-
public PooledDataBuffer retain() {
236-
this.refCount.incrementAndGet();
237-
DataBufferUtils.retain(dataBuffer());
238-
return this;
239-
}
240-
241-
@Override
242-
public boolean release() {
243-
int refCount = this.refCount.decrementAndGet();
244-
if (refCount == 0) {
245-
try {
246-
return DataBufferUtils.release(dataBuffer());
247-
}
248-
finally {
249-
this.pooledByteBuffer.close();
250-
}
251-
}
252-
return false;
253-
}
254-
255-
@Override
256-
public DataBuffer slice(int index, int length) {
257-
DataBuffer slice = dataBuffer().slice(index, length);
258-
return new UndertowDataBuffer(slice, this.pooledByteBuffer, this.refCount);
259-
}
260-
261-
}
262-
263201
}

0 commit comments

Comments
 (0)