Repository navigation
reverse_tunnel: fix upstream acceptor TLS and extension teardown lifecycle - #48441
Conversation
…cycle Signed-off-by: antoniovleonti <[email protected]>
Signed-off-by: antoniovleonti <[email protected]>
Signed-off-by: antoniovleonti <[email protected]>
|
/retest |
6 similar comments
|
/retest |
|
/retest |
|
/retest |
|
/retest |
|
/retest |
|
/retest |
Signed-off-by: antoniovleonti <[email protected]>
|
/retest |
Signed-off-by: antoniovleonti <[email protected]>
|
/retest |
Signed-off-by: antoniovleonti <[email protected]>
|
I synced |
| socket_interface_->extension_ = this; | ||
| } | ||
|
|
||
| if (!tls_slot_) { |
There was a problem hiding this comment.
This fallback seems to be there so that old unit tests that only call onServerInitialized() still get a slot? Can we just have those tests call onWorkerThreadInitialized() instead?
| reporter_->onServerInitialized(); | ||
| } | ||
|
|
||
| // Set the extension reference in the socket interface. |
| if (socket_interface_) { | ||
| socket_interface_->extension_ = this; | ||
| } | ||
| void ReverseTunnelAcceptorExtension::onWorkerThreadInitialized() { |
There was a problem hiding this comment.
Since this slot is now created before the cluster manager's, it's also destroyed after it. That means the tunnel-closed logs written in ~UpstreamSocketManager now run after the cluster manager's per-thread data is gone on that worker.
With a file access log that's fine. With a gRPC access log, once the buffer fills, log() calls flush(), which opens a new stream and calls cm_.getThreadLocalCluster(). That dereferences the cluster manager's slot, which shutdownThread() has already cleared.
Before this change these logs were written while the cluster manager was still alive. Could we skip lifecycle log emission once context_.threadLocal().isShutdown() is true?
Signed-off-by: antoniovleonti <[email protected]>
Signed-off-by: antoniovleonti <[email protected]>
|
@agrawroh can you take a final look? |
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Signed-off-by: Ryan Northey <[email protected]>
…cycle (envoyproxy#48441) - Allocate `ReverseTunnelAcceptorExtension::tls_slot_` (`UpstreamSocketThreadLocal`) in `onWorkerThreadInitialized()` (matching `ReverseTunnelInitiatorExtension`), with a fallback in `onServerInitialized()` for unit tests. Since TLS slots are destroyed in reverse allocation order during `tls_.shutdownThread()`, allocating the slot in `onWorkerThreadInitialized()` (before `ClusterManagerImpl` allocates its TLS slot) ensures `ThreadLocalClusterManagerImpl` closes pooled `UpstreamReverseConnectionIOHandle` sockets before `UpstreamSocketThreadLocal` and `UpstreamSocketManager` are destroyed. - Clear `socket_interface_->extension_` in `~ReverseTunnelAcceptorExtension()` so the `ReverseTunnelAcceptor` singleton does not retain a dangling pointer after extension teardown. - Change `UpstreamSocketThreadLocal`'s constructor parameter from a nullable pointer (`ReverseTunnelAcceptorExtension* = nullptr`) to a reference (`ReverseTunnelAcceptorExtension&`) so `cx_post_upgrade_lifetime_` is guaranteed to be non-null when dereferenced by `UpstreamReverseConnectionIOHandle`. *(Note: AI assistance was used in drafting this PR.)* Risk Level: Low Testing: Unit and integration tests under `//test/extensions/bootstrap/reverse_tunnel/...`, `//test/extensions/clusters/reverse_connection/...`, `//test/extensions/filters/network/reverse_tunnel/...`, and `//test/extensions/upstreams/http/reverse_tunnel/...` Docs Changes: N/A Release Notes: N/A --------- Signed-off-by: antoniovleonti <[email protected]> (cherry picked from commit be15c6a) Signed-off-by: Ryan Northey <[email protected]>
…cycle (envoyproxy#48441) - Allocate `ReverseTunnelAcceptorExtension::tls_slot_` (`UpstreamSocketThreadLocal`) in `onWorkerThreadInitialized()` (matching `ReverseTunnelInitiatorExtension`), with a fallback in `onServerInitialized()` for unit tests. Since TLS slots are destroyed in reverse allocation order during `tls_.shutdownThread()`, allocating the slot in `onWorkerThreadInitialized()` (before `ClusterManagerImpl` allocates its TLS slot) ensures `ThreadLocalClusterManagerImpl` closes pooled `UpstreamReverseConnectionIOHandle` sockets before `UpstreamSocketThreadLocal` and `UpstreamSocketManager` are destroyed. - Clear `socket_interface_->extension_` in `~ReverseTunnelAcceptorExtension()` so the `ReverseTunnelAcceptor` singleton does not retain a dangling pointer after extension teardown. - Change `UpstreamSocketThreadLocal`'s constructor parameter from a nullable pointer (`ReverseTunnelAcceptorExtension* = nullptr`) to a reference (`ReverseTunnelAcceptorExtension&`) so `cx_post_upgrade_lifetime_` is guaranteed to be non-null when dereferenced by `UpstreamReverseConnectionIOHandle`. *(Note: AI assistance was used in drafting this PR.)* Risk Level: Low Testing: Unit and integration tests under `//test/extensions/bootstrap/reverse_tunnel/...`, `//test/extensions/clusters/reverse_connection/...`, `//test/extensions/filters/network/reverse_tunnel/...`, and `//test/extensions/upstreams/http/reverse_tunnel/...` Docs Changes: N/A Release Notes: N/A --------- Signed-off-by: antoniovleonti <[email protected]> (cherry picked from commit be15c6a) Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Signed-off-by: Ryan Northey <[email protected]>
…cycle (envoyproxy#48441) - Allocate `ReverseTunnelAcceptorExtension::tls_slot_` (`UpstreamSocketThreadLocal`) in `onWorkerThreadInitialized()` (matching `ReverseTunnelInitiatorExtension`), with a fallback in `onServerInitialized()` for unit tests. Since TLS slots are destroyed in reverse allocation order during `tls_.shutdownThread()`, allocating the slot in `onWorkerThreadInitialized()` (before `ClusterManagerImpl` allocates its TLS slot) ensures `ThreadLocalClusterManagerImpl` closes pooled `UpstreamReverseConnectionIOHandle` sockets before `UpstreamSocketThreadLocal` and `UpstreamSocketManager` are destroyed. - Clear `socket_interface_->extension_` in `~ReverseTunnelAcceptorExtension()` so the `ReverseTunnelAcceptor` singleton does not retain a dangling pointer after extension teardown. - Change `UpstreamSocketThreadLocal`'s constructor parameter from a nullable pointer (`ReverseTunnelAcceptorExtension* = nullptr`) to a reference (`ReverseTunnelAcceptorExtension&`) so `cx_post_upgrade_lifetime_` is guaranteed to be non-null when dereferenced by `UpstreamReverseConnectionIOHandle`. *(Note: AI assistance was used in drafting this PR.)* Risk Level: Low Testing: Unit and integration tests under `//test/extensions/bootstrap/reverse_tunnel/...`, `//test/extensions/clusters/reverse_connection/...`, `//test/extensions/filters/network/reverse_tunnel/...`, and `//test/extensions/upstreams/http/reverse_tunnel/...` Docs Changes: N/A Release Notes: N/A --------- Signed-off-by: antoniovleonti <[email protected]> (cherry picked from commit be15c6a) Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
…cycle (#48441) - Allocate `ReverseTunnelAcceptorExtension::tls_slot_` (`UpstreamSocketThreadLocal`) in `onWorkerThreadInitialized()` (matching `ReverseTunnelInitiatorExtension`), with a fallback in `onServerInitialized()` for unit tests. Since TLS slots are destroyed in reverse allocation order during `tls_.shutdownThread()`, allocating the slot in `onWorkerThreadInitialized()` (before `ClusterManagerImpl` allocates its TLS slot) ensures `ThreadLocalClusterManagerImpl` closes pooled `UpstreamReverseConnectionIOHandle` sockets before `UpstreamSocketThreadLocal` and `UpstreamSocketManager` are destroyed. - Clear `socket_interface_->extension_` in `~ReverseTunnelAcceptorExtension()` so the `ReverseTunnelAcceptor` singleton does not retain a dangling pointer after extension teardown. - Change `UpstreamSocketThreadLocal`'s constructor parameter from a nullable pointer (`ReverseTunnelAcceptorExtension* = nullptr`) to a reference (`ReverseTunnelAcceptorExtension&`) so `cx_post_upgrade_lifetime_` is guaranteed to be non-null when dereferenced by `UpstreamReverseConnectionIOHandle`. *(Note: AI assistance was used in drafting this PR.)* Risk Level: Low Testing: Unit and integration tests under `//test/extensions/bootstrap/reverse_tunnel/...`, `//test/extensions/clusters/reverse_connection/...`, `//test/extensions/filters/network/reverse_tunnel/...`, and `//test/extensions/upstreams/http/reverse_tunnel/...` Docs Changes: N/A Release Notes: N/A --------- Signed-off-by: antoniovleonti <[email protected]> (cherry picked from commit be15c6a) Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since #44298 and the pointer clear since #48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Signed-off-by: Ryan Northey <[email protected]>
`ReverseTunnelAcceptorExtension` had no destructor, so its members were destroyed in reverse declaration order: the reporter (and the rest of the extension's state) before `tls_slot_`. Destroying `tls_slot_` destroys each worker's `UpstreamSocketManager`, whose destructor marks its sockets dead and reports back through the extension, by then partly destroyed. Under GCC with optimization this segfaulted at every teardown of `ReverseTunnelFilterWithUpstreamTest`; at fastbuild it depends on how the freed memory is reused, so it shows up as a flake. Reset `tls_slot_` first in an explicit destructor, and clear the acceptor singleton's pointer to the extension so it does not outlive it. The reverse tunnel filter and reverse connection cluster tests replaced the acceptor before the extension built on it, and freed the TLS slot's registry while the extension still held the slot. As on `main` (envoyproxy#48441), they now detach the slot and destroy the extension first. Backport note: written for this branch. `main` has the destructor's ordering since envoyproxy#44298 and the pointer clear since envoyproxy#48441, which do not apply here. Deflaked `//test/extensions/filters/network/reverse_tunnel:filter_unit_test` with GCC `-c opt` on envoy-ci-staging: 1000/1000 runs segfault without this, 1000/1000 pass with it. Backport note (release/v1.37): with this branch's `onServerInitialized()`, and not the tenant isolation tests release/v1.38 has. Signed-off-by: Ryan Northey <[email protected]>
Commit Message: reverse_tunnel: fix upstream acceptor TLS and extension teardown lifecycle
Additional Description:
ReverseTunnelAcceptorExtension::tls_slot_(UpstreamSocketThreadLocal) inonWorkerThreadInitialized()(matchingReverseTunnelInitiatorExtension), with a fallback inonServerInitialized()for unit tests. Since TLS slots are destroyed in reverse allocation order duringtls_.shutdownThread(), allocating the slot inonWorkerThreadInitialized()(beforeClusterManagerImplallocates its TLS slot) ensuresThreadLocalClusterManagerImplcloses pooledUpstreamReverseConnectionIOHandlesockets beforeUpstreamSocketThreadLocalandUpstreamSocketManagerare destroyed.socket_interface_->extension_in~ReverseTunnelAcceptorExtension()so theReverseTunnelAcceptorsingleton does not retain a dangling pointer after extension teardown.UpstreamSocketThreadLocal's constructor parameter from a nullable pointer (ReverseTunnelAcceptorExtension* = nullptr) to a reference (ReverseTunnelAcceptorExtension&) socx_post_upgrade_lifetime_is guaranteed to be non-null when dereferenced byUpstreamReverseConnectionIOHandle.(Note: AI assistance was used in drafting this PR.)
Risk Level: Low
Testing: Unit and integration tests under
//test/extensions/bootstrap/reverse_tunnel/...,//test/extensions/clusters/reverse_connection/...,//test/extensions/filters/network/reverse_tunnel/..., and//test/extensions/upstreams/http/reverse_tunnel/...Docs Changes: N/A
Release Notes: N/A