GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix server-initiated WebSocket close handshake failing with "promise … · netty/netty@0358eeb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/websocketx
      • WebSocketProtocolHandler.java
      • WebSocketServerProtocolHandler.java
    • test/java/io/netty/handler/codec/http/websocketx
      • WebSocketServerProtocolHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ void closeSent(ChannelPromise promise) {
147147
closeSent = promise;
148148
}
149149

150+
/**
151+
* If a close frame was already sent (or is still being sent), closes the channel once that write completes
152+
* (or the force-close timeout fires) and returns {@code true}. Returns {@code false} otherwise.
153+
*/
154+
boolean closeIfCloseSent(final ChannelHandlerContext ctx) throws Exception {
155+
if (closeSent == null) {
156+
return false;
157+
}
158+
// Flush through our own override so the force-close timeout is guaranteed to be armed.
159+
flush(ctx);
160+
closeSent.addListener(new ChannelFutureListener() {
161+
@Override
162+
public void operationComplete(ChannelFuture future) {
163+
ctx.close();
164+
}
165+
});
166+
return true;
167+
}
168+
150169
void applyCloseSentTimeout(final ChannelHandlerContext ctx) {
151170
if (forceCloseTimeoutTask != null || closeSent.isDone() || forceCloseTimeoutMillis < 0) {
152171
return;
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Obje
237237
if (serverConfig.handleCloseFrames() && frame instanceof CloseWebSocketFrame) {
238238
WebSocketServerHandshaker handshaker = getHandshaker(ctx.channel());
239239
if (handshaker != null) {
240+
if (closeIfCloseSent(ctx)) {
241+
// We already sent our own close frame, so this is either the peer's response to it or a
242+
// simultaneous close. As per RFC 6455 we must not send another close frame, just close the
243+
// connection once our own close frame was written.
244+
return;
245+
}
240246
frame.retain();
241247
ChannelPromise promise = ctx.newPromise();
242248
closeSent(promise);
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java
+61-9Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.netty.channel.ChannelPromise;
2424
import io.netty.channel.embedded.EmbeddedChannel;
2525

26+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
2627
import io.netty.handler.codec.http.DefaultHttpContent;
2728
import io.netty.handler.codec.http.DefaultHttpRequest;
2829
import io.netty.handler.codec.http.HttpClientCodec;
@@ -43,7 +44,6 @@
4344
import org.junit.jupiter.api.Test;
4445

4546
import java.util.ArrayDeque;
46-
import java.util.ArrayList;
4747
import java.util.Queue;
4848
import java.util.concurrent.TimeUnit;
4949

@@ -509,21 +509,73 @@ public void testSimultaneousCloseDoesNotLeakOriginalPromise() throws Exception {
509509
assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
510510
assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));
511511

512-
// Both echo writes are stalled, but only a single deadline should govern the close handshake.
513-
assertEquals(2, stalledWrites.size());
514-
// The second echo (triggered by the peer's inbound frame) is the one whose promise was cascaded
515-
// from the original closeSent instead of overwriting it.
516-
ChannelPromise peerEchoWrite = new ArrayList<ChannelPromise>(stalledWrites).get(1);
512+
// We already sent a close frame, so no second close frame must be written in response to the peer's
513+
// one. A single deadline governs the close handshake.
514+
assertEquals(1, stalledWrites.size());
517515
assertTrue(server.isOpen());
518516

519517
server.advanceTimeBy(forceCloseTimeoutMillis * 2, TimeUnit.MILLISECONDS);
520518
server.runScheduledPendingTasks();
521519

522520
assertFalse(server.isOpen());
523-
// Neither promise is left hanging forever: the application's own close promise completes, and so
524-
// does the promise cascaded from it for the peer's own (redundant) close frame.
521+
// The application's own close promise is not left hanging forever.
525522
assertTrue(applicationClosePromise.isDone());
526-
assertTrue(peerEchoWrite.isDone());
523+
524+
client.close();
525+
assertFalse(client.finishAndReleaseAll());
526+
assertFalse(server.finishAndReleaseAll());
527+
}
528+
529+
@Test
530+
public void testServerInitiatedCloseHandshakeWithInboundCloseFrame() {
531+
// Reproducer from https://github.com/netty/netty/issues/17776
532+
EmbeddedChannel channel = new EmbeddedChannel(
533+
new HttpServerCodec(), new HttpObjectAggregator(65536), new WebSocketServerProtocolHandler("/ws"));
534+
535+
FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/ws");
536+
request.headers()
537+
.set(HttpHeaderNames.HOST, "localhost")
538+
.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET)
539+
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
540+
.set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==")
541+
.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13");
542+
assertFalse(channel.writeInbound(request));
543+
ReferenceCountUtil.release(channel.readOutbound());
544+
545+
assertTrue(channel.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
546+
ReferenceCountUtil.release(channel.readOutbound());
547+
assertTrue(channel.isOpen());
548+
549+
// The "client" responds with a close frame, which must not try to reuse the already completed
550+
// closeSent promise but just close the connection.
551+
assertFalse(channel.writeInbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
552+
assertFalse(channel.isOpen());
553+
assertNull(channel.readOutbound());
554+
555+
assertFalse(channel.finishAndReleaseAll());
556+
}
557+
558+
@Test
559+
public void testServerInitiatedCloseHandshake() throws Exception {
560+
EmbeddedChannel client = createClient();
561+
EmbeddedChannel server = createServer();
562+
563+
assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));
564+
assertFalse(client.writeInbound(server.<ByteBuf>readOutbound()));
565+
566+
// The server initiates the close handshake.
567+
assertTrue(server.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
568+
CloseWebSocketFrame closeMessage = decode(server.<ByteBuf>readOutbound(), CloseWebSocketFrame.class);
569+
assertEquals(WebSocketCloseStatus.NORMAL_CLOSURE.code(), closeMessage.statusCode());
570+
closeMessage.release();
571+
assertTrue(server.isOpen());
572+
573+
// The peer responds with its own close frame, which must complete the handshake by closing the
574+
// connection without sending another close frame.
575+
assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
576+
assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));
577+
assertFalse(server.isOpen());
578+
assertNull(server.readOutbound());
527579

528580
client.close();
529581
assertFalse(client.finishAndReleaseAll());

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.