Repository navigation
test: Use a locked stats store for all fake upstream TLS contexts - #48960
Conversation
There was a problem hiding this comment.
🟡 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
| upstream_stats_store_ = std::make_unique<Stats::TestIsolatedStoreImpl>( | ||
| server_factory_context_.serverScope().symbolTable()); |
There was a problem hiding this comment.
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]>
57dc058 to
40679d3
Compare
|
/retest flake addressed elsewhere |
| #endif | ||
| } | ||
|
|
||
| Stats::Scope& BaseIntegrationTest::upstreamStatsScope() { |
There was a problem hiding this comment.
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()createsfake_upstreams_count_TLS upstreams on it. That's the same "several TLS upstreams handshaking at once" pattern as theAlpnIntegrationTestfailure.- 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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]>
|
/assign @jwendell |
…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]>
…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]>

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.