Release DataBuffers in FreeMarkerView

When template processing throws an exception, we must ensure that
`DataBuffers` are properly released to avoid memory leaks.

Issue: SPR-15966
This commit is contained in:
Jason Yu
2017-09-15 16:54:46 +08:00
committed by Brian Clozel
parent 7c18e4b115
commit 78434c8e20

View File

@@ -41,6 +41,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -199,10 +200,12 @@ public class FreeMarkerView extends AbstractUrlBasedView {
getTemplate(locale).process(freeMarkerModel, writer); getTemplate(locale).process(freeMarkerModel, writer);
} }
catch (IOException ex) { catch (IOException ex) {
DataBufferUtils.release(dataBuffer);
String message = "Could not load FreeMarker template for URL [" + getUrl() + "]"; String message = "Could not load FreeMarker template for URL [" + getUrl() + "]";
return Mono.error(new IllegalStateException(message, ex)); return Mono.error(new IllegalStateException(message, ex));
} }
catch (Throwable ex) { catch (Throwable ex) {
DataBufferUtils.release(dataBuffer);
return Mono.error(ex); return Mono.error(ex);
} }
return exchange.getResponse().writeWith(Flux.just(dataBuffer)); return exchange.getResponse().writeWith(Flux.just(dataBuffer));