Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* 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
*<p>
* 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) {

Check warning on line 56 in org.restlet/src/test/java/org/restlet/engine/netty/NettyServerReleaseOnPeerGoneTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace "e" with an unnamed pattern.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIuRG7nWZT8KVcmk6S8&open=AaIuRG7nWZT8KVcmk6S8&pullRequest=1776
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);

Check warning on line 82 in org.restlet/src/test/java/org/restlet/engine/netty/NettyServerReleaseOnPeerGoneTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "Thread.sleep()".

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIuRG7nWZT8KVcmk6S9&open=AaIuRG7nWZT8KVcmk6S9&pullRequest=1776
proceed.countDown();
assertTrue(
released.await(10, TimeUnit.SECONDS),
"The response entity must be released when the peer is gone");
} finally {
proceed.countDown();
server.stop();
}
}
}