diff --git a/changes.md b/changes.md index f6357e248a..d7c68296f9 100644 --- a/changes.md +++ b/changes.md @@ -163,6 +163,10 @@ Changes log proxy. - Bugs fixed + - The Netty server connector releases the response entity when the client is gone before the + response is written, or when writing the response fails before its head is sent: it left + the entity unreleased, so a stream wrapping another connection, such as a proxied + upstream answer, stayed open (#1774). - The MCP extension answers a `POST` without content with a `400` parse error: it answered `503` and logged a `NullPointerException`, as an entity without content has no stream to read (#1768); the MCP client likewise fails with an `IOException`, not a diff --git a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java index ff68170c58..5bfc7546fa 100644 --- a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java +++ b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java @@ -687,6 +687,7 @@ private void handleException(Response response, Exception exception) { setStatus( Status.SERVER_ERROR_INTERNAL.getCode(), "An exception occurred writing the response entity"); + releaseQuietly(response.getEntity()); response.setEntity(null); try { sendResponse(response); @@ -712,6 +713,9 @@ private void handleException(Response response, Exception exception) { */ private void sendResponse(Response response) throws IOException { if (!channel.isActive()) { + // Nothing will be written: release the entity, which may wrap another connection's + // stream. + releaseQuietly(response.getEntity()); throw new IOException("Connection closed by peer"); } @@ -739,6 +743,16 @@ private void sendResponse(Response response) throws IOException { } } + private void releaseQuietly(Representation entity) { + if (entity != null) { + try { + entity.release(); + } catch (RuntimeException e) { + getLogger().log(Level.FINE, "Unable to release the response entity", e); + } + } + } + private OutputStream getResponseEntityStream() { if (responseBody == null) { responseBody = diff --git a/org.restlet/src/test/java/org/restlet/engine/netty/NettyServerReleaseOnPeerGoneTestCase.java b/org.restlet/src/test/java/org/restlet/engine/netty/NettyServerReleaseOnPeerGoneTestCase.java new file mode 100644 index 0000000000..c08539d95d --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/engine/netty/NettyServerReleaseOnPeerGoneTestCase.java @@ -0,0 +1,92 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet.engine.netty; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.Restlet; +import org.restlet.Server; +import org.restlet.data.MediaType; +import org.restlet.data.Protocol; +import org.restlet.engine.Engine; +import org.restlet.engine.connector.HttpServerHelper; +import org.restlet.representation.InputRepresentation; + +/** + * Checks that the response entity is released when the client is gone before the server starts + * writing the response. + */ +class NettyServerReleaseOnPeerGoneTestCase { + + @Test + void releasesTheEntityWhenThePeerIsGoneBeforeTheWrite() throws Exception { + Engine.register(false); + Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null)); + + CountDownLatch handling = new CountDownLatch(1); + CountDownLatch proceed = new CountDownLatch(1); + CountDownLatch released = new CountDownLatch(1); + + Server server = + new Server( + Protocol.HTTP, + 0, + new Restlet() { + @Override + public void handle(Request request, Response response) { + handling.countDown(); + try { + // Simulates waiting for an upstream answer. + proceed.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + response.setEntity( + new InputRepresentation( + new ByteArrayInputStream(new byte[] {1, 2, 3}), + MediaType.APPLICATION_OCTET_STREAM) { + @Override + public void release() { + released.countDown(); + super.release(); + } + }); + } + }); + server.start(); + try { + try (Socket socket = new Socket("localhost", server.getActualPort())) { + OutputStream out = socket.getOutputStream(); + out.write( + "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" + .getBytes(StandardCharsets.US_ASCII)); + out.flush(); + assertTrue(handling.await(10, TimeUnit.SECONDS)); + } + // The client left while the application was still working. + Thread.sleep(500); + proceed.countDown(); + assertTrue( + released.await(10, TimeUnit.SECONDS), + "The response entity must be released when the peer is gone"); + } finally { + proceed.countDown(); + server.stop(); + } + } +}