Skip to content

Commit f569d7f

Browse files
committed
[grid] Closing input pipeline when 404 is returned
In some cases the resource is not found, and if we have not read completely the input stream, a read/write lock will happen since the client gets a response and won't stream the remaining content. Closing the pipe avoids a lock and leaves the RequestConverter ready for upcoming requests. Fixes SeleniumHQ/docker-selenium#1646 Fixes SeleniumHQ/docker-selenium#1605
1 parent 14b802c commit f569d7f

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

java/src/org/openqa/selenium/netty/server/RequestConverter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class RequestConverter extends SimpleChannelInboundHandler<HttpObject> {
7070
protected void channelRead0(
7171
ChannelHandlerContext ctx,
7272
HttpObject msg) throws Exception {
73-
LOG.fine("Incoming message: " + msg);
73+
LOG.log(Debug.getDebugLogLevel(), "Incoming message: " + msg);
7474

7575
if (msg instanceof io.netty.handler.codec.http.HttpRequest) {
7676
LOG.log(Debug.getDebugLogLevel(), "Start of http request: " + msg);
@@ -84,7 +84,7 @@ protected void channelRead0(
8484
}
8585

8686
if (nettyRequest.headers().contains("Sec-WebSocket-Version") &&
87-
"upgrade".equals(nettyRequest.headers().get("Connection"))) {
87+
"upgrade".equals(nettyRequest.headers().get("Connection"))) {
8888
// Pass this on to later in the pipeline.
8989
ReferenceCountUtil.retain(msg);
9090
ctx.fireChannelRead(msg);
@@ -124,7 +124,7 @@ protected void channelRead0(
124124
}
125125

126126
if (msg instanceof LastHttpContent) {
127-
LOG.fine("Closing input pipe.");
127+
LOG.log(Debug.getDebugLogLevel(), "Closing input pipe.");
128128
PipedOutputStream target = out;
129129

130130
EXECUTOR.submit(() -> {
@@ -139,17 +139,20 @@ protected void channelRead0(
139139

140140
@Override
141141
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
142-
LOG.fine("Closing input pipe, channel became inactive.");
143-
PipedOutputStream target = out;
142+
LOG.log(Debug.getDebugLogLevel(), "Closing input pipe, channel became inactive.");
143+
closeInputPipe();
144+
super.channelInactive(ctx);
145+
}
144146

147+
public void closeInputPipe() {
148+
PipedOutputStream target = out;
145149
SHUTDOWN_EXECUTOR.submit(() -> {
146150
try {
147151
target.close();
148152
} catch (IOException e) {
149153
throw new UncheckedIOException(e);
150154
}
151155
});
152-
super.channelInactive(ctx);
153156
}
154157

155158
private HttpRequest createRequest(

java/src/org/openqa/selenium/netty/server/ResponseConverter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package org.openqa.selenium.netty.server;
1919

2020
import com.google.common.io.ByteStreams;
21+
22+
import org.openqa.selenium.remote.http.HttpResponse;
23+
2124
import io.netty.buffer.Unpooled;
2225
import io.netty.channel.ChannelFuture;
2326
import io.netty.channel.ChannelHandlerContext;
@@ -29,7 +32,6 @@
2932
import io.netty.handler.codec.http.HttpChunkedInput;
3033
import io.netty.handler.codec.http.HttpResponseStatus;
3134
import io.netty.handler.stream.ChunkedStream;
32-
import org.openqa.selenium.remote.http.HttpResponse;
3335

3436
import java.io.InputStream;
3537

@@ -56,6 +58,13 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
5658
}
5759

5860
HttpResponse seResponse = (HttpResponse) msg;
61+
if (seResponse.getStatus() == 404) {
62+
// In some cases the resource is not found, and if we have not read completely the input
63+
// stream, a read/write lock will happen since the client gets a response and won't stream
64+
// the remaining content. Closing the pipe avoids a lock and leaves the RequestConverter
65+
// ready for upcoming requests.
66+
((RequestConverter) ctx.pipeline().context("se-request").handler()).closeInputPipe();
67+
}
5968

6069
// We may not know how large the response is, but figure it out if we can.
6170
byte[] ary = new byte[CHUNK_SIZE];

0 commit comments

Comments
 (0)