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 — Auto-port 4.1: Call `handshakeFailure` for failed TLS completion events by netty-project-bot · Pull Request #18327 · netty/netty

Auto-port 4.1: Call handshakeFailure for failed TLS completion events #18327

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 3 commits into 4.1 from auto-port-pr-18323-to-4.1
Nov 11, 2026

Uh oh!

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

Merged

Auto-port 4.1: Call handshakeFailure for failed TLS completion events #18327

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
a535126
Call `handshakeFailure` for failed TLS completion events (#18323)
gavinbunney Nov 10, 2026
b7fdebb
Fix Java 6 compile issues in ApplicationProtocolNegotiationHandlerTest
normanmaurer Nov 11, 2026
e2c8f67
Avoid Arguments.of in ApplicationProtocolNegotiationHandlerTest
normanmaurer Nov 11, 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

There are no files selected for viewing

26 changes: 19 additions & 7 deletions 26 handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandler.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public abstract class ApplicationProtocolNegotiationHandler extends ChannelInbou
private final RecyclableArrayList bufferedMessages = RecyclableArrayList.newInstance();
private ChannelHandlerContext ctx;
private boolean sslHandlerChecked;
private boolean handshakeFailed;

/**
* Creates a new instance with the specified fallback protocol name.
Expand Down Expand Up @@ -138,16 +139,19 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
String protocol = sslHandler.applicationProtocol();
configurePipeline(ctx, protocol != null ? protocol : fallbackProtocol);
} else {
// if the event is not produced because of an successful handshake we will receive the same
// exception in exceptionCaught(...) and handle it there. This will allow us more fine-grained
// control over which exception we propagate down the ChannelPipeline.
//
// See https://github.com/netty/netty/issues/10342
try {
super.userEventTriggered(ctx, evt);
} finally {
this.notifyHandshakeFailure(ctx, handshakeEvent.cause());
}

return;
}
} catch (Throwable cause) {
exceptionCaught(ctx, cause);
} finally {
// Handshake failures are handled in exceptionCaught(...).
// consume later SSL decoder exceptions to avoid downstream warnings
// see https://github.com/netty/netty/issues/10342; timeouts and early closes may only emit this event
if (handshakeEvent.isSuccess()) {
removeSelfIfPresent(ctx);
}
Expand Down Expand Up @@ -192,17 +196,25 @@ protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) thro
ctx.close();
}

private void notifyHandshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (!this.handshakeFailed) {
this.handshakeFailed = true;
this.handshakeFailure(ctx, cause);
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
Throwable wrapped;
if (cause instanceof DecoderException && ((wrapped = cause.getCause()) instanceof SSLException)) {
try {
handshakeFailure(ctx, wrapped);
this.notifyHandshakeFailure(ctx, wrapped);
return;
} finally {
removeSelfIfPresent(ctx);
}
}

logger.warn("{} Failed to select the application-level protocol:", ctx.channel(), cause);
ctx.fireExceptionCaught(cause);
ctx.close();
Expand Down
192 changes: 187 additions & 5 deletions 192 handler/src/test/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandlerTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,40 @@
package io.netty.handler.ssl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.channel.socket.ChannelInputShutdownEvent;
import io.netty.handler.codec.DecoderException;
import io.netty.util.AsyncMapping;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLHandshakeException;
import java.nio.channels.ClosedChannelException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static io.netty.handler.ssl.CloseNotifyTest.assertCloseNotify;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -70,24 +83,193 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
assertFalse(channel.finishAndReleaseAll());
}

@Test
public void testHandshakeFailure() {
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void testHandshakeFailure(final boolean exceptionDuringEvent) {
final AtomicInteger failureCount = new AtomicInteger();
final AtomicInteger failureCountOnExceptionReturn = new AtomicInteger();
final AtomicInteger downstreamExceptionCount = new AtomicInteger();
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
fail();
}

@Override
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
failureCount.incrementAndGet();
failureCause.set(cause);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
failureCountOnExceptionReturn.set(failureCount.get());
}
};

EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
SSLHandshakeException exception = new SSLHandshakeException("error");
SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception);
final SSLHandshakeException exception = new SSLHandshakeException("error");
final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception);
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (exceptionDuringEvent && evt instanceof SslHandshakeCompletionEvent) {
ctx.pipeline().fireExceptionCaught(new DecoderException(exception));
}

ctx.fireUserEventTriggered(evt);
}
}, new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
downstreamExceptionCount.incrementAndGet();
}
});

channel.pipeline().fireUserEventTriggered(completionEvent);

if (!exceptionDuringEvent) {
channel.pipeline().fireExceptionCaught(new DecoderException(exception));
}

assertEquals(1, failureCount.get());
assertEquals(1, failureCountOnExceptionReturn.get());
assertEquals(0, downstreamExceptionCount.get());
assertSame(exception, failureCause.get());
assertNull(channel.pipeline().context(alpnHandler));
assertFalse(channel.finishAndReleaseAll());
}

@ParameterizedTest
@MethodSource("handshakeFailureCauses")
public void testHandshakeFailureEvent(final Throwable cause) {
final AtomicReference<SslHandshakeCompletionEvent> eventRef =
new AtomicReference<SslHandshakeCompletionEvent>();
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
fail();
}

@Override
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
failureCause.set(cause);
super.handshakeFailure(ctx, cause);
}
};
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof SslHandshakeCompletionEvent) {
eventRef.set((SslHandshakeCompletionEvent) evt);
}
}
});
final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(cause);

channel.pipeline().fireUserEventTriggered(completionEvent);

assertSame(cause, failureCause.get());
assertSame(completionEvent, eventRef.get());
assertFalse(channel.isOpen());
assertFalse(channel.finishAndReleaseAll());
}

private static List<Throwable> handshakeFailureCauses() {
return Arrays.<Throwable>asList(
new SslHandshakeTimeoutException("timed out"),
new ClosedChannelException());
}

@Test
public void testHandshakeFailureExceptionWithoutEvent() {
final AtomicInteger failureCount = new AtomicInteger();
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
fail();
}

@Override
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
failureCount.incrementAndGet();
failureCause.set(cause);
}
};
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
final SSLHandshakeException exception = new SSLHandshakeException("error");

channel.pipeline().fireExceptionCaught(new DecoderException(exception));

assertEquals(1, failureCount.get());
assertSame(exception, failureCause.get());
assertNull(channel.pipeline().context(alpnHandler));
assertFalse(channel.finishAndReleaseAll());
}

@Test
public void testSniLookupFailure() {
final SSLHandshakeException exception = new SSLHandshakeException("SNI lookup failed");
final AtomicInteger failureCount = new AtomicInteger();
final AtomicInteger handshakeEventCount = new AtomicInteger();
final AtomicInteger downstreamExceptionCount = new AtomicInteger();
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
final AtomicReference<SniCompletionEvent> sniEvent = new AtomicReference<SniCompletionEvent>();
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
fail();
}

@Override
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
failureCount.incrementAndGet();
failureCause.set(cause);
}
};
final SniHandler sniHandler = new SniHandler(new AsyncMapping<String, SslContext>() {
@Override
public Future<SslContext> map(String input, Promise<SslContext> promise) {
return promise.setFailure(exception);
}
});
EmbeddedChannel channel = new EmbeddedChannel(sniHandler, alpnHandler, new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof SniCompletionEvent) {
sniEvent.set((SniCompletionEvent) evt);
} else if (evt instanceof SslHandshakeCompletionEvent) {
handshakeEventCount.incrementAndGet();
}

ctx.fireUserEventTriggered(evt);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
downstreamExceptionCount.incrementAndGet();
}
});

try {
channel.writeInbound(Unpooled.buffer(5).writeByte(SslUtils.SSL_CONTENT_TYPE_APPLICATION_DATA)
.writeShort(0x0303).writeShort(0));

assertNotNull(sniEvent.get());
assertSame(exception, sniEvent.get().cause());
assertSame(exception, failureCause.get());
assertEquals(1, failureCount.get());
assertEquals(0, handshakeEventCount.get());
assertEquals(0, downstreamExceptionCount.get());
} finally {
channel.pipeline().remove(sniHandler);
channel.finishAndReleaseAll();
}
}

@Test
public void testHandshakeSuccess() throws NoSuchAlgorithmException {
testHandshakeSuccess0(false);
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.