Skip to content

reverse_tunnel: fix upstream acceptor TLS and extension teardown lifecycle - #48441

Merged
yanavlasov merged 8 commits into
envoyproxy:mainfrom
antoniovleonti:fix_bug_538559991
Nov 3, 2026
Merged

yanavlasov merged 8 commits into
envoyproxy:mainfrom
antoniovleonti:fix_bug_538559991

Conversation

@antoniovleonti

Copy link
Copy Markdown
Contributor

Commit Message: reverse_tunnel: fix upstream acceptor TLS and extension teardown lifecycle
Additional Description:

  • 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

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

6 similar comments
@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

Signed-off-by: antoniovleonti <[email protected]>
@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

/retest

@antoniovleonti

Copy link
Copy Markdown
Contributor Author

I synced extensions_metadata.yaml with main to fix the format, docs, and external CI failures caused by missing metadata for priority_load_shed and dynamic_modules.

socket_interface_->extension_ = this;
}

if (!tls_slot_) {

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.

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.

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.

this is already done in the constructor

if (socket_interface_) {
socket_interface_->extension_ = this;
}
void ReverseTunnelAcceptorExtension::onWorkerThreadInitialized() {

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.

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?

@basundhara-c

Copy link
Copy Markdown
Contributor

@agrawroh can you take a final look?

@yanavlasov
yanavlasov merged commit be15c6a into envoyproxy:main Nov 3, 2026
27 checks passed
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
`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]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…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]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
`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]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 16, 2026
…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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
`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]>
phlax pushed a commit that referenced this pull request Nov 17, 2026
…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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
`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]>
phlax added a commit that referenced this pull request Nov 17, 2026
`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]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 18, 2026
`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]>
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.

3 participants