Skip to content

test: Use a locked stats store for all fake upstream TLS contexts - #48960

Merged
phlax merged 3 commits into
envoyproxy:mainfrom
phlax:test-upstream-tls-stats-store
Nov 5, 2026
Merged

phlax merged 3 commits into
envoyproxy:mainfrom
phlax:test-upstream-tls-stats-store

Conversation

@phlax

@phlax phlax commented Nov 3, 2026

Copy link
Copy Markdown
Member

createUpstreamTlsContext built every fake upstream TLS context on server_factory_context_.serverScope(), a single unlocked mock IsolatedStoreImpl shared by all upstreams. ContextImpl creates ssl.ciphers., ssl.versions., ssl.curves.* and ssl.sigalgs.* lazily on the handshake, ie on each fake upstream dispatcher thread, so two TLS upstreams handshaking at once (AlpnIntegrationTest.Http1New: two connections, two upstreams) insert into the same flat_hash_map concurrently. The msan build hit absl's "constructed value does not match the lookup key" assertion in IsolatedStatsCache::get on a fake upstream listener connection.

The xDS upstream already used a TestIsolatedStoreImpl for exactly this. Create it lazily (upstreamStatsScope()) and use it for every upstream server TLS context, including the ones alpn_selection and sds_dynamic build themselves, so creating the xDS upstream no longer replaces a store other upstreams reference.

Copilot AI 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.

🟡 Changes recommended

The upstream stats store outlives its borrowed symbol table, causing TLS fixture teardown to trigger assertions or access destroyed state unless member declarations are reordered.

1 open issue
What changed in this PR

This PR addresses concurrent TLS statistics creation in fake upstreams by sharing a lazily initialized, thread-safe test stats store.

Changes:

  • Add upstreamStatsScope() to create and reuse the locked store.
  • Route standard, ALPN, SDS, and xDS upstream TLS contexts through that scope.
  • Avoid replacing the store when creating xDS upstreams.
File Description
test/​integration/​sds_dynamic_integration_test.cc Use the shared scope for custom SDS upstream TLS contexts.
test/​integration/​base_integration_test.h Declare the scope helper and document threading requirements.
test/​integration/​base_integration_test.cc Lazily initialize the store and reuse it for standard and xDS TLS contexts.
test/​integration/​alpn_selection_integration_test.cc Use the shared scope for ALPN upstream TLS contexts.
test/​integration/​ads_integration_test.cc Use the helper for replacement xDS upstreams.

Review effort: Balanced

Comment on lines +148 to +149
upstream_stats_store_ = std::make_unique<Stats::TestIsolatedStoreImpl>(
server_factory_context_.serverScope().symbolTable());

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.

Moved upstream_stats_store_ to after context_manager_ and before fake_upstreams_ in 40679d3, so it is destroyed after the upstreams and before the factory context/symbol table owners.

createUpstreamTlsContext built every fake upstream TLS context on
server_factory_context_.serverScope(), a single unlocked mock
IsolatedStoreImpl shared by all upstreams. ContextImpl creates
ssl.ciphers.*, ssl.versions.*, ssl.curves.* and ssl.sigalgs.* lazily on
the handshake, ie on each fake upstream dispatcher thread, so two TLS
upstreams handshaking at once (AlpnIntegrationTest.Http1New: two
connections, two upstreams) insert into the same flat_hash_map
concurrently. The msan build hit absl's "constructed value does not
match the lookup key" assertion in IsolatedStatsCache::get on a fake
upstream listener connection.

The xDS upstream already used a TestIsolatedStoreImpl for exactly this.
Create it lazily (upstreamStatsScope()) and use it for every upstream
server TLS context, including the ones alpn_selection and sds_dynamic
build themselves, so creating the xDS upstream no longer replaces a
store other upstreams reference.

Signed-off-by: Ryan Northey <[email protected]>
@phlax
phlax force-pushed the test-upstream-tls-stats-store branch from 57dc058 to 40679d3 Compare November 3, 2026 15:21
@phlax
phlax requested a balanced review from Copilot November 3, 2026 15:25

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the change introducing a shared, locked stats store for fake upstream TLS contexts.

1 open issue

Review effort: Balanced

@phlax

phlax commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

/retest flake addressed elsewhere

#endif
}

Stats::Scope& BaseIntegrationTest::upstreamStatsScope() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some fake-upstream server TLS contexts still use the shared, unlocked server_factory_context_.serverScope(), so the race this PR describes can still happen there:

  • test/extensions/clusters/dynamic_modules/integration_test.cc:223-224: DynamicModuleAsyncTlsIntegrationTest::createUpstreams() creates fake_upstreams_count_ TLS upstreams on it. That's the same "several TLS upstreams handshaking at once" pattern as the AlpnIntegrationTest failure.
  • Single-upstream cases on the same scope: auto_sni_integration_test.cc:76-77, xfcc_integration_test.cc:116-117, ssl_upstream_integration_test.cc:191-193, grpc_client_integration_test_harness.h:779-780. These still race with client TLS factories created on the test thread.

Could these switch to upstreamStatsScope() too (at least the dynamic_modules one)? If not, maybe adjust the title, which currently says "all".

Small related cleanup: xds_failover_integration_test.cc:154-158 still does ASSERT(upstream_stats_store_ != nullptr); *upstream_stats_store_->rootScope(). Using upstreamStatsScope() there would remove the dependency on createXdsUpstream() running first.

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.

Done in 16b1622: dynamic_modules, auto_sni, xfcc, ssl_upstream (both subclasses), sds_static, starttls and the two dynamic forward proxy tests (via an explicit scope param on Ssl::createFakeUpstreamSslContext) now use upstreamStatsScope(), and xds_failover no longer depends on createXdsUpstream() having run first.

grpc_client_integration_test_harness.h is already fine: that harness ON_CALLs serverScope() to its own stats_store_, which is a Stats::TestIsolatedStoreImpl wrapping the mock's store (line 614), ie the locked wrapper.

std::move(cfg), context_manager_, server_factory_context_.serverScope());
std::move(cfg), context_manager_, upstreamStatsScope());
} else {
envoy::extensions::transport_sockets::quic::v3::QuicDownstreamTransport quic_config;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The QUIC branch still builds its context through factory_context_, which ends up on the mock's unlocked MockIsolatedStatsStore. Several HTTP/3 upstreams could race the same way. Is that worth routing through upstreamStatsScope() as well?

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.

The QUIC path doesn't have this problem. The lazy per-handshake stats (ssl.ciphers.*, ssl.versions.*, ssl.curves.*, ssl.sigalgs.*) are created in ContextImpl::logHandshake, whose only caller is SslSocket (ssl_socket.cc). The QUIC server handshaker doesn't go through it; the QUIC transport socket factory stats and the server ContextImpl stats are all created up front on the test thread in create()/initialize(), and per-connection QUIC stats go to each FakeUpstream's own stats_store_. So nothing touches that store from the fake upstream threads. (It also lands on BaseIntegrationTest::stats_store_ via the statsScope() ON_CALL, not the mock's store, but either way nothing races on it.)

…sScope()

Review follow-up: dynamic_modules' async TLS test creates
fake_upstreams_count_ TLS upstreams on server_factory_context_.serverScope(),
the same several-upstreams-handshaking-at-once pattern as AlpnIntegrationTest,
and the single-upstream tests (auto_sni, xfcc, ssl_upstream, sds_static,
starttls, the dynamic forward proxy tests via Ssl::createFakeUpstreamSslContext)
share that unlocked store with client TLS contexts handshaking on the test
thread. Switch all of them to upstreamStatsScope(), giving the ssl_utility
helper an explicit scope parameter, and drop xds_failover's dependency on
createXdsUpstream() having run first.

GrpcSslClientIntegrationTest's serverScope() already returns a locked
TestIsolatedStoreImpl wrapper, so it is left as is.

Signed-off-by: Ryan Northey <[email protected]>
Signed-off-by: Ryan Northey <[email protected]>
@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/assign @jwendell

@phlax
phlax merged commit a8d0083 into envoyproxy:main Nov 5, 2026
27 checks passed
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…voyproxy#48960)

createUpstreamTlsContext built every fake upstream TLS context on
server_factory_context_.serverScope(), a single unlocked mock
IsolatedStoreImpl shared by all upstreams. ContextImpl creates
ssl.ciphers.*, ssl.versions.*, ssl.curves.* and ssl.sigalgs.* lazily on
the handshake, ie on each fake upstream dispatcher thread, so two TLS
upstreams handshaking at once (AlpnIntegrationTest.Http1New: two
connections, two upstreams) insert into the same flat_hash_map
concurrently. The msan build hit absl's "constructed value does not
match the lookup key" assertion in IsolatedStatsCache::get on a fake
upstream listener connection.

The xDS upstream already used a TestIsolatedStoreImpl for exactly this.
Create it lazily (upstreamStatsScope()) and use it for every upstream
server TLS context, including the ones alpn_selection and sds_dynamic
build themselves, so creating the xDS upstream no longer replaces a
store other upstreams reference.

Backport note: v1.40 does not have the SslUpstreamClientCertRejectedIntegrationTest
class (envoyproxy#48570), so its server TLS context change is dropped.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 17, 2026
…8960)

createUpstreamTlsContext built every fake upstream TLS context on
server_factory_context_.serverScope(), a single unlocked mock
IsolatedStoreImpl shared by all upstreams. ContextImpl creates
ssl.ciphers.*, ssl.versions.*, ssl.curves.* and ssl.sigalgs.* lazily on
the handshake, ie on each fake upstream dispatcher thread, so two TLS
upstreams handshaking at once (AlpnIntegrationTest.Http1New: two
connections, two upstreams) insert into the same flat_hash_map
concurrently. The msan build hit absl's "constructed value does not
match the lookup key" assertion in IsolatedStatsCache::get on a fake
upstream listener connection.

The xDS upstream already used a TestIsolatedStoreImpl for exactly this.
Create it lazily (upstreamStatsScope()) and use it for every upstream
server TLS context, including the ones alpn_selection and sds_dynamic
build themselves, so creating the xDS upstream no longer replaces a
store other upstreams reference.

Backport note: v1.40 does not have the SslUpstreamClientCertRejectedIntegrationTest
class (#48570), so its server TLS context change is dropped.

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.

4 participants