Skip to content

tls: fix connection close detection while handshake is waiting - #45755

Merged
ggreenway merged 4 commits into
envoyproxy:mainfrom
ggreenway:ssl-handshake-close
Jun 24, 2026
Merged

ggreenway merged 4 commits into
envoyproxy:mainfrom
ggreenway:ssl-handshake-close

Conversation

@ggreenway

Copy link
Copy Markdown
Member

When a TLS handshake is waiting on an async operation (such as certificate selection through some extensions, async certificate validation, or async private key provider operations), it was failing to detect if the downstream connection was closed, so that the async operation could be canceled and the TLS connection resources could be released.

Added connection.readDisable() while in this state, which changes which socket events ConnectionImpl is registered to receive so that connection close is handled immediately. Without this change, SslSocket::doRead() was discarding the read event which would signal an EOF.

Risk Level: Medium
Testing: Added new tests; new integration test fails without this fix
Docs Changes: none
Release Notes: added

When a TLS handshake is waiting on an async operation (such as
certificate selection through some extensions, async certificate
validation, or async private key provider operations), it was failing
to detect if the downstream connection was closed, so that the async
operation could be canceled and the TLS connection resources could be
released.

Added connection.readDisable() while in this state, which changes
which socket events ConnectionImpl is registered to receive so that
connection close is handled immediately. Without this change,
SslSocket::doRead() was discarding the read event which would signal
an EOF.

Signed-off-by: Greg Greenway <[email protected]>
Comment on lines +9 to +13
HandshakeWaitingForConnectionData,

// The handshake is in progress and waiting on Envoy to complete an operation before it can
// continue.
HandshakeBlockedOnAsyncOperation,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two were renamed for clarity, because now the state machine can go from the 2nd state back to the 1st

connection->close(Network::ConnectionCloseType::NoFlush);
connection.reset();

test_server_->waitForCounter("aysnc_cert_selection.cert_selection_cancelled", Eq(1),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: async

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was pre-existing. Want me to fix it in all the tests? It's ~15 more lines to change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, up to you. I didn't realize the scope prefix is wrongly named.

Comment thread source/common/tls/ssl_socket.cc Outdated
if (info_->state() == Ssl::SocketState::HandshakeBlockedOnAsyncOperation && !read_disabled_) {
// Connection close events can only be detected if the connection has reading disabled,
// because the handshaker won't actually read any data (such as EOF) when the handshake is
// blocked on an async operation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is a bit confusing. Would we leak async operations here if we read disable and fail to detect connection closure? I think the handshake timer should probably force close it, but want to double check. Apart from this, the rest of the change looks reasonable and safe to deploy since it's mostly debug asserts.

@ggreenway ggreenway Jun 22, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we won't leak async events; Envoy will not detect the downstream close, and so the async operation won't be cancelled when it should be.

Without the source change, the new integration test added will have the timer fire and ENVOY_BUG, instead of cancelling the operation.

Signed-off-by: Greg Greenway <[email protected]>
kyessenov
kyessenov previously approved these changes Jun 23, 2026
@ggreenway

Copy link
Copy Markdown
Member Author

ASAN test failure is real

/wait

@ggreenway

Copy link
Copy Markdown
Member Author

There's also a real failure in the mobile tests:

[2026-06-22 22:37:54.609][95605][critical][assert] [external/envoy/source/common/http/http1/codec_impl.cc:1501] assert failure: connection_.readEnabled().
Error: 6-22 22:37:54.609][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:38] stacktrace for envoy bug
Error: 6-22 22:37:54.659][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #0 Envoy::Http::Http1::ClientConnectionImpl::newStream() [0x564b6439f72c]
Error: 6-22 22:37:54.671][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #1 Envoy::Http::CodecClient::enlistAndCreateEncoder() [0x564b640532cb]
Error: 6-22 22:37:54.684][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #2 Envoy::Http::CodecClient::newStream() [0x564b640533c2]
Error: 6-22 22:37:54.696][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #3 Envoy::Http::Http1::ActiveClient::StreamWrapper::StreamWrapper() [0x564b63dd75d9]
Error: 6-22 22:37:54.709][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #4 std::__1::make_unique[abi:nn180100]<>() [0x564b63ddce6b]
Error: 6-22 22:37:54.721][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #5 Envoy::Http::Http1::ActiveClient::newStreamEncoder() [0x564b63dd9f37]
Error: 6-22 22:37:54.734][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #6 Envoy::Http::HttpConnPoolImplBase::onPoolReady() [0x564b63df707f]
Error: 6-22 22:37:54.746][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #7 Envoy::ConnectionPool::ConnPoolImplBase::attachStreamToClient() [0x564b63e0646b]
Error: 6-22 22:37:54.759][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #8 Envoy::ConnectionPool::ConnPoolImplBase::onUpstreamReady() [0x564b63e08a27]
Error: 6-22 22:37:54.771][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #9 Envoy::ConnectionPool::ConnPoolImplBase::onConnectionEvent() [0x564b63e0f87d]
Error: 6-22 22:37:54.783][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #10 Envoy::Http::ActiveClient::onEvent() [0x564b63dde74f]
Error: 6-22 22:37:54.796][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #11 Envoy::Network::ConnectionImplBase::raiseConnectionEvent() [0x564b64d4a238]
Error: 6-22 22:37:54.809][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #12 Envoy::Network::ConnectionImpl::raiseEvent() [0x564b64d2c7f4]
Error: 6-22 22:37:54.821][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #13 Envoy::Extensions::TransportSockets::Tls::SslSocket::onSuccess() [0x564b64128755]
Error: 6-22 22:37:54.833][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #14 Envoy::Extensions::TransportSockets::Tls::SslHandshakerImpl::doHandshake() [0x564b6415aaa1]
Error: 6-22 22:37:54.846][95605][error][envoy_bug] [external/envoy/source/common/common/assert.h:43] #15 Envoy::Extensions::TransportSockets::Tls::SslSocket::doHandshake() [0x564b64126739]

@ggreenway

Copy link
Copy Markdown
Member Author

/retest

@ggreenway
ggreenway enabled auto-merge (squash) June 23, 2026 17:56

@kyessenov kyessenov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@ggreenway
ggreenway merged commit df2c77d into envoyproxy:main Jun 24, 2026
28 checks passed
ggreenway pushed a commit that referenced this pull request Sep 3, 2026
#46676)

#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
envoyproxy#46676)

envoyproxy#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
(cherry picked from commit 70f0729)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
envoyproxy#46676)

envoyproxy#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
(cherry picked from commit 70f0729)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
envoyproxy#46676)

envoyproxy#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
(cherry picked from commit 70f0729)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
envoyproxy#46676)

envoyproxy#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
(cherry picked from commit 70f0729)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
envoyproxy#46676)

envoyproxy#45755 allowed the socket state
to transition to `ShutdownSent` before cert selection is complete (as
intended). However, this could cause `ENVOY_BUG` to be thrown when
`SslSocket::onAsynchronousCertificateSelectionComplete()` is invoked.
This patch updates
`SslSocket::onAsynchronousCertificateSelectionComplete` to allow this
socket state which is consistent with the implementation of
`SslSocket::onAsynchronousCertValidationComplete` and avoids the
`ENVOY_BUG` in the case of valid states such as `ShutdownSent`.

Signed-off-by: Ethan Truong <[email protected]>
(cherry picked from commit 70f0729)
Signed-off-by: Ryan Northey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants