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@21bad5d

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
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ void closeSent(Promise<Void> promise) {
144144
closeSent = promise;
145145
}
146146

147+
/**
148+
* If a close frame was already sent (or is still being sent), closes the channel once that write completes
149+
* (or the force-close timeout fires) and returns {@code true}. Returns {@code false} otherwise.
150+
*/
151+
boolean closeIfCloseSent(final ChannelHandlerContext ctx) throws Exception {
152+
if (closeSent == null) {
153+
return false;
154+
}
155+
// Flush through our own override so the force-close timeout is guaranteed to be armed.
156+
flush(ctx);
157+
closeSent.addListener(future -> ctx.close());
158+
return true;
159+
}
160+
147161
void applyCloseSentTimeout(ChannelHandlerContext ctx) {
148162
if (forceCloseTimeoutTask != null || closeSent.isDone() || forceCloseTimeoutMillis < 0) {
149163
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
@@ -236,6 +236,12 @@ protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Obje
236236
if (serverConfig.handleCloseFrames() && frame instanceof CloseWebSocketFrame) {
237237
WebSocketServerHandshaker handshaker = getHandshaker(ctx.channel());
238238
if (handshaker != null) {
239+
if (closeIfCloseSent(ctx)) {
240+
// We already sent our own close frame, so this is either the peer's response to it or a
241+
// simultaneous close. As per RFC 6455 we must not send another close frame, just close the
242+
// connection once our own close frame was written.
243+
return;
244+
}
239245
frame.retain();
240246
Promise<Void> promise = ctx.newPromise();
241247
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
@@ -22,6 +22,7 @@
2222
import io.netty.channel.ChannelOutboundHandler;
2323
import io.netty.channel.embedded.EmbeddedChannel;
2424

25+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
2526
import io.netty.handler.codec.http.DefaultHttpContent;
2627
import io.netty.handler.codec.http.DefaultHttpRequest;
2728
import io.netty.handler.codec.http.HttpClientCodec;
@@ -44,7 +45,6 @@
4445
import org.junit.jupiter.api.Test;
4546

4647
import java.util.ArrayDeque;
47-
import java.util.ArrayList;
4848
import java.util.Queue;
4949
import java.util.concurrent.TimeUnit;
5050

@@ -512,21 +512,73 @@ public void testSimultaneousCloseDoesNotLeakOriginalPromise() throws Exception {
512512
assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
513513
assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));
514514

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

522520
server.advanceTimeBy(forceCloseTimeoutMillis * 2, TimeUnit.MILLISECONDS);
523521
server.runScheduledPendingTasks();
524522

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

531583
client.close();
532584
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.