Skip to content

DataBuffer Should Release Manually when Exception Thrown [SPR-15966] #20518

@spring-projects-issues

Description

@spring-projects-issues

Jason Yu opened SPR-15966 and commented

As this comment, I guess following implementation should manual release DataBuffer instance in catch block.

@Override
protected Mono<Void> renderInternal(Map<String, Object> renderAttributes,
		@Nullable MediaType contentType, ServerWebExchange exchange) {

	// Expose all standard FreeMarker hash models.
	SimpleHash freeMarkerModel = getTemplateModel(renderAttributes, exchange);
	if (logger.isDebugEnabled()) {
		logger.debug("Rendering FreeMarker template [" + getUrl() + "].");
	}

	Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());
	DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
	try {
		Charset charset = getCharset(contentType);
		Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset);
		getTemplate(locale).process(freeMarkerModel, writer);
	}
	catch (IOException ex) {
		String message = "Could not load FreeMarker template for URL [" + getUrl() + "]";
		return Mono.error(new IllegalStateException(message, ex));
	}
	catch (Throwable ex) {
		return Mono.error(ex);
	}
	return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}

Affects: 5.0 RC4

Referenced from: commits 78434c8

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions