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 — Arm force-close timeout when an inbound CLOSE frame is echoed by normanmaurer · Pull Request #17574 · netty/netty

Arm force-close timeout when an inbound CLOSE frame is echoed #17574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
normanmaurer merged 6 commits into 4.2 from ws-close-1b430108
Oct 1, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Arm force-close timeout when an inbound CLOSE frame is echoed #17574

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
34b6a4b
Arm force-close timeout when an inbound CLOSE frame is echoed
normanmaurer Sep 21, 2026
308afa0
Close the channel directly from the force-close timeout
normanmaurer Sep 22, 2026
c4eb1c0
Cascade closeSent() instead of overwriting an in-flight promise
normanmaurer Sep 26, 2026
1b1e5fd
Merge branch '4.2' into ws-close-1b430108
normanmaurer Sep 28, 2026
7c04c3b
Use 10 seconds as default
normanmaurer Sep 29, 2026
dd650fb
Merge branch '4.2' into ws-close-1b430108
normanmaurer Oct 1, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Arm force-close timeout when an inbound CLOSE frame is echoed
Motivation:

WebSocketServerProtocolHandler.decode() writes the CLOSE echo directly
via WebSocketServerHandshaker.close(), bypassing this handler's own
write()/close() overrides where forceCloseTimeoutMillis was armed. If
the peer stops draining its socket, the echo write never completes and
the channel, its file descriptor and buffers are pinned forever.

Modifications:

Arm the timeout explicitly after the echo write is issued, and also
from a new flush() override so a bare write(CloseWebSocketFrame) not
followed by close() gets the same protection. The timeout is applied
after the write/flush attempt, not before, so a write that completes
synchronously never races the deadline.

Result:

Awalys close the channel even when the remote peer stops reading
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer committed Sep 21, 2026
commit 34b6a4b76d7ef90b69efe65ffe044f71b9b87dc6

There are no files selected for viewing

29 changes: 21 additions & 8 deletions 29 ...c-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class WebSocketProtocolHandler extends MessageToMessageDecoder<WebSocke
private final WebSocketCloseStatus closeStatus;
private final long forceCloseTimeoutMillis;
private ChannelPromise closeSent;
private Future<?> forceCloseTimeoutTask;

/**
* Creates a new {@link WebSocketProtocolHandler} that will <i>drop</i> {@link PongWebSocketFrame}s.
Expand Down Expand Up @@ -97,7 +98,6 @@ public void close(final ChannelHandlerContext ctx, final ChannelPromise promise)
write(ctx, new CloseWebSocketFrame(closeStatus, ctx.alloc()), ctx.newPromise());
}
flush(ctx);
applyCloseSentTimeout(ctx);
closeSent.addListener(future -> ctx.close(promise));
}
}
Expand All @@ -115,12 +115,29 @@ public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise pr
}
}

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
// Give the flush a chance to complete synchronously before arming the deadline, so a write that
// finishes immediately (e.g. EmbeddedChannel, or a fast socket) never races the force-close timer.
if (closeSent != null) {
applyCloseSentTimeout(ctx);
}
}

/**
* Records the {@link ChannelPromise} used to write the outgoing close frame. Every code path that initiates
* the close handshake must call this, and must subsequently give
* {@link #applyCloseSentTimeout(ChannelHandlerContext)} a chance to run (directly, or indirectly via
* {@link #flush(ChannelHandlerContext)}) so the channel is guaranteed to close even if the outbound write
* never completes (e.g. the peer stops reading and the socket send buffer fills up).
*/
void closeSent(ChannelPromise promise) {
closeSent = promise;
Comment thread
normanmaurer marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

}

private void applyCloseSentTimeout(ChannelHandlerContext ctx) {
if (closeSent.isDone() || forceCloseTimeoutMillis < 0) {
void applyCloseSentTimeout(ChannelHandlerContext ctx) {
if (forceCloseTimeoutTask != null || closeSent.isDone() || forceCloseTimeoutMillis < 0) {
return;
}

Expand All @@ -132,6 +149,7 @@ public void run() {
}
}
}, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
Comment thread
normanmaurer marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

forceCloseTimeoutTask = timeoutTask;

closeSent.addListener(future -> timeoutTask.cancel(false));
}
Expand Down Expand Up @@ -172,11 +190,6 @@ public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
Expand Down
5 changes: 5 additions & 0 deletions 5 .../src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Obje
ChannelPromise promise = ctx.newPromise();
closeSent(promise);
handshaker.close(ctx, (CloseWebSocketFrame) frame, promise);
// handshaker.close() bypasses this handler's own write()/flush() overrides (it writes
// directly on ctx), so the force-close timeout must be armed explicitly here. Doing so
// after the close0() write-and-flush attempt above mirrors close()'s ordering and avoids
// racing a write that completes synchronously.
applyCloseSentTimeout(ctx);
} else {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
Expand Down
61 changes: 61 additions & 0 deletions 61 .../test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.*;
Expand Down Expand Up @@ -458,6 +459,66 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
assertFalse(server.finishAndReleaseAll());
}

@Test
public void testForceCloseTimeoutAppliedToInboundCloseFrame() throws Exception {
final long forceCloseTimeoutMillis = 100;
final Queue<ChannelPromise> stalledWrites = new ArrayDeque<ChannelPromise>();
EmbeddedChannel client = createClient();
EmbeddedChannel server = createStallingServer(forceCloseTimeoutMillis, stalledWrites);

assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));
assertFalse(client.writeInbound(server.<ByteBuf>readOutbound()));

// The peer initiates the close handshake.
assertTrue(client.writeOutbound(new CloseWebSocketFrame(WebSocketCloseStatus.NORMAL_CLOSURE)));
assertFalse(server.writeInbound(client.<ByteBuf>readOutbound()));

// The server's echo write is stalled, simulating a peer that stopped draining its socket
// receive buffer. The channel must not stay open indefinitely waiting on that write.
assertEquals(1, stalledWrites.size());
assertTrue(server.isOpen());

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

assertFalse(server.isOpen());

client.close();
assertFalse(client.finishAndReleaseAll());
assertFalse(server.finishAndReleaseAll());
}

private static EmbeddedChannel createStallingServer(
long forceCloseTimeoutMillis, final Queue<ChannelPromise> stalledWrites) throws Exception {
WebSocketServerProtocolConfig serverConfig = WebSocketServerProtocolConfig.newBuilder()
.websocketPath("/test")
.dropPongFrames(false)
.forceCloseTimeoutMillis(forceCloseTimeoutMillis)
.build();
EmbeddedChannel ch = new EmbeddedChannel(false, false,
new ChannelOutboundHandlerAdapter() {
private boolean handshakeResponseSent;

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof ByteBuf && handshakeResponseSent) {
// Simulate a peer that stopped draining its socket receive buffer: the
// close-frame echo write never completes on its own.
ReferenceCountUtil.release(msg);
stalledWrites.add(promise);
return;
}
handshakeResponseSent = true;
ctx.write(msg, promise);
}
},
new HttpServerCodec(),
new HttpObjectAggregator(8192),
new WebSocketServerProtocolHandler(serverConfig));
ch.register();
return ch;
}

private EmbeddedChannel createClient(ChannelHandler... handlers) throws Exception {
WebSocketClientProtocolConfig clientConfig = WebSocketClientProtocolConfig.newBuilder()
.webSocketUri("http://test/test")
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.