Skip to content

wasm: pass PluginHandle to stream contexts. - #16795

Merged
lizan merged 12 commits into
envoyproxy:mainfrom
mathetake:fix-lifetime-wasm
Jun 28, 2021
Merged

lizan merged 12 commits into
envoyproxy:mainfrom
mathetake:fix-lifetime-wasm

Conversation

@mathetake

@mathetake mathetake commented Jun 3, 2021 •

Copy link
Copy Markdown
Member

Previously, stream contexts (i.e. filter instances) are not holding shared ptrs to Wasm plugins which take ownership of Wasm VMs, and only holding raw pointers to Wasm VMs. If Wasm plugins (i.e. factory) die earlier than stream contexts during ECDS updates, then the left stream contexts will try using invalidated pointers to Wasm VM and this results in crashes.
So this commit passes the PluginHandle to all the stream contexts which the plugin generates, so the VM must outlive all stream contexts by which it is used.

Counter part in Proxy-Wasm C++ Host: proxy-wasm/proxy-wasm-cpp-host#167
Resolves istio/istio#33091
Signed-off-by: Takeshi Yoneda [email protected]

EDIT: I hope this commit 412081d explains the bug well.

@repokitteh-read-only repokitteh-read-only Bot added the deps Approval required for changes to Envoy's external dependencies label Jun 3, 2021
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/dependency-shepherds: Your approval is needed for changes made to (bazel/.*repos.*\.bzl)|(bazel/dependency_imports\.bzl)|(api/bazel/.*\.bzl)|(.*/requirements\.txt)|(.*\.patch).

🐱

Caused by: #16795 was opened by mathetake.

see: more, trace.

@mathetake

Copy link
Copy Markdown
Member Author

OK we need to pass the handle to ContexBase in C++ host so that VMs outlives not only Context (in Envoy) but also ContextBase (in Proxy-Wasm C++ host).

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake mathetake changed the title wip: wasm: pass PluginHandle to stream contexts. wasm: pass PluginHandle to stream contexts. Jun 3, 2021
@mathetake

Copy link
Copy Markdown
Member Author

tests will follow after #16430

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake
mathetake force-pushed the fix-lifetime-wasm branch from e132459 to 412081d Compare June 8, 2021 08:45
@mathetake
mathetake marked this pull request as ready for review June 8, 2021 08:46
@mathetake

Copy link
Copy Markdown
Member Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #16795 (comment) was created by @mathetake.

see: more, trace.

@alyssawilk

Copy link
Copy Markdown
Contributor

I think Lizan's out for a bit, so @PiotrSikora can you do first pass, and tag Matt or I when it's ready for merge?

lizan
lizan previously approved these changes Jun 22, 2021
@lizan

lizan commented Jun 22, 2021

Copy link
Copy Markdown
Member

@PiotrSikora still need a non-Tetrands review from you

@PiotrSikora

Copy link
Copy Markdown
Contributor

@PiotrSikora still need a non-Tetrands review from you

Sorry, I spent way too much time doing archeology work on this issue trying to find a regression, and then looking for an alternative solution. I should have timed out on this much sooner, I'll wrap this within next 24 hours.

PiotrSikora
PiotrSikora previously approved these changes Jun 24, 2021

@PiotrSikora PiotrSikora 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.

Thanks, and sorry for the delay!

Comment thread bazel/repository_locations.bzl Resolved
Comment thread source/extensions/common/wasm/wasm.h Resolved
@mathetake
mathetake dismissed stale reviews from PiotrSikora and lizan via 9d4c7e1 June 24, 2021 12:07
@mathetake

Copy link
Copy Markdown
Member Author

@moderation PTAL for dep!

@moderation

Copy link
Copy Markdown
Contributor

/lgtm deps

@repokitteh-read-only repokitteh-read-only Bot removed the deps Approval required for changes to Envoy's external dependencies label Jun 24, 2021
@mathetake

Copy link
Copy Markdown
Member Author

@lizan I believe we are good to go now

@PiotrSikora PiotrSikora 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.

Thanks!

@lizan
lizan merged commit 5a8f89a into envoyproxy:main Jun 28, 2021
@mathetake
mathetake deleted the fix-lifetime-wasm branch July 6, 2021 03:39
chrisxrepo pushed a commit to chrisxrepo/envoy that referenced this pull request Jul 8, 2021
Previously, stream contexts (i.e. filter instances) are not holding shared ptrs to Wasm plugins which take ownership of Wasm VMs, and only holding raw pointers to Wasm VMs. If Wasm plugins (i.e. factory) die earlier than stream contexts during ECDS updates, then the left stream contexts will try using invalidated pointers to Wasm VM and this results in crashes.
So this commit passes the PluginHandle to all the stream contexts which the plugin generates, so the VM must outlive all stream contexts by which it is used.

Counter part in Proxy-Wasm C++ Host: proxy-wasm/proxy-wasm-cpp-host#167
Resolves istio/istio#33091

Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: chris.xin <[email protected]>
lizan pushed a commit that referenced this pull request Jul 12, 2021
This is the follow up on #16795.

## Bug description

If the same plugin/rootcontext is shared across multiple listeners, then there is a race in listener information.

Before  #16795,  this was not a problem  for stream contexts since given listener information is from *where* it is created (`Plugin: plugin_` member created in the factory instance) https://github.com/envoyproxy/envoy/pull/16795/files#diff-90e133f83d91e5c22d1f7c7e8c0cf8017536a23765978bf81c7efd0482be0d81L43
But after that being merged, the `Plugin` passed to stream context constructor is the one from `PluginHandle::plugin()` which is potentially shared across multiple listeners.

For root contexts, this has been a problem since the beginning because the root context has been already potentially shared across multiple listeners, and pointing to the listener's info which is created first.


## Change in this PR

Combined with proxy-wasm/proxy-wasm-cpp-host#178, we explicitly include listener information into the plugin key so that if two plugins have different info to each other, different plugin instances will be created.

## Note

- Maybe we could drop the support for sharing 1 Plugin across multiple listeners (or filters), then this race condition would be resolved in a different way. I think it is worth discussion.
- This race is originally reported by Istio istio/proxy#3395 (comment)

Signed-off-by: Takeshi Yoneda <[email protected]>
leyao-daily pushed a commit to leyao-daily/envoy that referenced this pull request Sep 30, 2021
Previously, stream contexts (i.e. filter instances) are not holding shared ptrs to Wasm plugins which take ownership of Wasm VMs, and only holding raw pointers to Wasm VMs. If Wasm plugins (i.e. factory) die earlier than stream contexts during ECDS updates, then the left stream contexts will try using invalidated pointers to Wasm VM and this results in crashes.
So this commit passes the PluginHandle to all the stream contexts which the plugin generates, so the VM must outlive all stream contexts by which it is used.

Counter part in Proxy-Wasm C++ Host: proxy-wasm/proxy-wasm-cpp-host#167
Resolves istio/istio#33091

Signed-off-by: Takeshi Yoneda <[email protected]>
leyao-daily pushed a commit to leyao-daily/envoy that referenced this pull request Sep 30, 2021
)

This is the follow up on envoyproxy#16795.

## Bug description

If the same plugin/rootcontext is shared across multiple listeners, then there is a race in listener information.

Before  envoyproxy#16795,  this was not a problem  for stream contexts since given listener information is from *where* it is created (`Plugin: plugin_` member created in the factory instance) https://github.com/envoyproxy/envoy/pull/16795/files#diff-90e133f83d91e5c22d1f7c7e8c0cf8017536a23765978bf81c7efd0482be0d81L43
But after that being merged, the `Plugin` passed to stream context constructor is the one from `PluginHandle::plugin()` which is potentially shared across multiple listeners.

For root contexts, this has been a problem since the beginning because the root context has been already potentially shared across multiple listeners, and pointing to the listener's info which is created first.


## Change in this PR

Combined with proxy-wasm/proxy-wasm-cpp-host#178, we explicitly include listener information into the plugin key so that if two plugins have different info to each other, different plugin instances will be created.

## Note

- Maybe we could drop the support for sharing 1 Plugin across multiple listeners (or filters), then this race condition would be resolved in a different way. I think it is worth discussion.
- This race is originally reported by Istio istio/proxy#3395 (comment)

Signed-off-by: Takeshi Yoneda <[email protected]>
codefromthecrypt added a commit to tetratelabs/built-on-envoy that referenced this pull request May 13, 2026
Envoy requires node.id and node.cluster for any xDS config subscription,
including filesystem-based ones. Envoy's subscriptionFromConfigSource
checks node identity before examining the config source type, so
path_config_source crashes the same way gRPC does. This blocks any
extension that uses Extension Configuration Discovery Service (ECDS),
Secret Discovery Service (SDS) for TLS certs, or any other dynamic
config.

For example, Istio uses ECDS to hot-reload Wasm filters on running
sidecars (envoyproxy/envoy#16795). With nativeHttpFilters.after, a Go
dynamic module can do the same thing locally: generate ECDS config at
init and write it to disk for a native filter to consume via
path_config_source.

Signed-off-by: Adrian Cole <[email protected]>
codefromthecrypt added a commit to tetratelabs/built-on-envoy that referenced this pull request May 14, 2026
Istio uses ECDS to hot-reload Wasm filters on running sidecars
(envoyproxy/envoy#16795). With nativeHttpFilters.after, a Go dynamic
module can do the same thing locally: generate Extension Configuration
Discovery Service (ECDS) config at init and write it to disk for a
native filter to consume via path_config_source. After that point, the
module stays out of the request path entirely.

In short, dynamic modules could be used to augment, share code with,
or replace control plane (xDS) code, regardless of if that is about
ECDS, Secret Discovery Service (SDS) for TLS certs and any other
dynamic config.

If you try this with BOE now, Envoy crashes on startup. Envoy requires
node.id and node.cluster for any xDS config subscription. Set node to
"boe" in buildFullConfig. Static configs that don't create subscriptions
ignore node entirely, so there's no risk.

Signed-off-by: Adrian Cole <[email protected]>
wbpcode pushed a commit to tetratelabs/built-on-envoy that referenced this pull request May 14, 2026
Istio [uses ECDS to hot-reload Wasm filters][istio-ecds] on running
sidecars without restarting Envoy. In BOE, using
[`nativeHttpFilters.after`][native-after], a dynamic module can do the
same thing locally: generate [Extension Configuration Discovery
Service][ecds] (ECDS) config at init and write it to disk for a native
filter to consume (via [`path_config_source`][path-config]). After that
point, the module stays out of the request path entirely.

In short, dynamic modules could be used to augment, share code with, or
replace control plane ([xDS][xds]) code. This is regardless of if that
is about ECDS, [Secret Discovery Service][sds] (SDS) for TLS certs, or
any other dynamic config.

If you try this with BOE now, envoy crashes on startup. Envoy requires
`node.id` and `node.cluster` for any
xDS config subscription. This simple change to enable this is set `node:
{id: "boe", cluster: "boe"}` in `buildFullConfig`. Static configs that
don't create subscriptions ignore node entirely, so there's no risk I'm
aware of.

[xds]: https://www.envoyproxy.io/docs/envoy/latest/api-docs/xds_protocol
[path-config]:
https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/core/v3/config_source.proto#config-core-v3-pathconfigsource
[ecds]:
https://www.envoyproxy.io/docs/envoy/latest/configuration/overview/extension#discovery-service
[sds]:
https://www.envoyproxy.io/docs/envoy/latest/configuration/security/secret
[istio-ecds]: envoyproxy/envoy#16795
[native-after]:
https://github.com/tetratelabs/built-on-envoy/blob/main/extensions/manifest.schema.json

Signed-off-by: Adrian Cole <[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.

Envoy crashes when upgrading WASM modules under load

5 participants