Expected behavior
SslHandler should release buffered outbound messages (removed from pendingUnencryptedWrites) and complete the corresponding ChannelPromises, even if wrap() at SslHandler.java#L869 throws an exception.
Actual behavior
SslHandler doesn't release buffered outbound messages (removed from pendingUnencryptedWrites) or complete the corresponding ChannelPromises if wrap() at SslHandler.java#L869 throws an exception.
Steps to reproduce
It's difficult to reproduce this issue automatically, as it depends on particular concurrency conditions. We used the following approach:
- In debug mode, start any HTTP (WebSocket in our case) server that uses OpenSSL for secure connections (specifically, ReferenceCountedOpenSslEngine).
- Add a breakpoint at ReferenceCountedOpenSslEngine.java#L928.
- Send an outbound message (in our case, a
TextWebSocketFrame)
- When the control flow hits the breakpoint, set
pendingException = new RuntimeException. if you are using IDEA, you can set the variable's value during debug. In our environment, for example, we see the following exception instead: OpenSslException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
As a result, you should get a stack trace similar to the following one:
This pending inbound exception ReferenceCountedOpenSslEngine.java#L213:
i.n.h.s.ReferenceCountedOpenSslEngine$OpenSslException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at i.n.h.s.ReferenceCountedOpenSslEngine.newSSLExceptionForError(ReferenceCountedOpenSslEngine.java:1389)
at i.n.h.s.ReferenceCountedOpenSslEngine.needWrapAgain(ReferenceCountedOpenSslEngine.java:1375)
at i.n.h.s.ReferenceCountedOpenSslEngine.sslReadErrorResult(ReferenceCountedOpenSslEngine.java:1406)
at i.n.h.s.ReferenceCountedOpenSslEngine.unwrap(ReferenceCountedOpenSslEngine.java:1337)
at i.n.h.s.ReferenceCountedOpenSslEngine.unwrap(ReferenceCountedOpenSslEngine.java:1438)
at i.n.h.s.ReferenceCountedOpenSslEngine.unwrap(ReferenceCountedOpenSslEngine.java:1481)
at i.n.h.s.SslHandler$SslEngineType$1.unwrap(SslHandler.java:224)
at i.n.h.ssl.SslHandler.unwrap(SslHandler.java:1445)
at i.n.h.ssl.SslHandler.decodeNonJdkCompatible(SslHandler.java:1349)
at i.n.h.ssl.SslHandler.decode(SslHandler.java:1389)
at i.n.h.c.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530)
at i.n.h.c.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469)
at i.n.h.c.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
at i.n.c.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at i.n.c.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at i.n.c.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at i.n.c.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at i.n.c.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
at i.n.c.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at i.n.c.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at i.n.c.e.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:801)
at i.n.c.e.EpollEventLoop.processReady(EpollEventLoop.java:501)
at i.n.c.e.EpollEventLoop.run(EpollEventLoop.java:399)
... 4 common frames omitted
caused the following outbound exception ReferenceCountedOpenSslEngine.java#L934:
Wrapped by: j.n.ssl.SSLException: io.netty.handler.ssl.ReferenceCountedOpenSslEngine$OpenSslException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at i.n.h.s.ReferenceCountedOpenSslEngine.wrap(ReferenceCountedOpenSslEngine.java:934)
at j.net.ssl.SSLEngine.wrap(SSLEngine.java:522)
at i.n.h.ssl.SslHandler.wrap(SslHandler.java:1131)
at i.n.h.ssl.SslHandler.wrap(SslHandler.java:869)
at i.n.h.ssl.SslHandler.wrapAndFlush(SslHandler.java:822)
at i.n.h.ssl.SslHandler.flush(SslHandler.java:803)
at i.n.c.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:941)
... 91 common frames omitted
Notice that ReferenceCountedOpenSslEngine.java#L934 leads to exiting the control flow at SslHandler.java#L869.
The problem is that this exit happens after the messages (and their promises) have already been removed from pendingUnencryptedWrites at SslHandler.java#L840 but before they are actually written to the next handler in the pipeline (i.e., before the next handler takes responsibility for releasing them and completing the promises) at SslHandler.java#L887. As a result, these messages are never released, and their corresponding promises are never completed.
Netty version
4.1.109.Final
JVM version (e.g. java -version)
openjdk version "11.0.20.1" 2023-08-22 LTS
OpenJDK Runtime Environment Corretto-11.0.20.9.1 (build 11.0.20.1+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.20.9.1 (build 11.0.20.1+9-LTS, mixed mode)
OS version (e.g. uname -a)
22.6.0 Darwin Kernel Version 22.6.0: Sun Dec 17 22:12:45 PST 2023; RELEASE_ARM64_T6000 arm64