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 … · lltopk/netty@7a6f630

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
@@ -142,6 +142,20 @@ void closeSent(ChannelPromise promise) {
142142
closeSent = promise;
143143
}
144144

145+
/**
146+
* If a close frame was already sent (or is still being sent), closes the channel once that write completes
147+
* (or the force-close timeout fires) and returns {@code true}. Returns {@code false} otherwise.
148+
*/
149+
boolean closeIfCloseSent(final ChannelHandlerContext ctx) throws Exception {
150+
if (closeSent == null) {
151+
return false;
152+
}
153+
// Flush through our own override so the force-close timeout is guaranteed to be armed.
154+
flush(ctx);
155+
closeSent.addListener(future -> ctx.close());
156+
return true;
157+
}
158+
145159
void applyCloseSentTimeout(ChannelHandlerContext ctx) {
146160
if (forceCloseTimeoutTask != null || closeSent.isDone() || forceCloseTimeoutMillis < 0) {
147161
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

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

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

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

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

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