Skip to content

wasm: fix race condition on listener info in contexts. - #17243

Merged
lizan merged 3 commits into
envoyproxy:mainfrom
mathetake:listener-info-race
Jul 12, 2021
Merged

lizan merged 3 commits into
envoyproxy:mainfrom
mathetake:listener-info-race

Conversation

@mathetake

@mathetake mathetake commented Jul 6, 2021 •

Copy link
Copy Markdown
Member

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 Update Envoy SHA to 07-12. istio/proxy#3395 (comment)

Signed-off-by: Takeshi Yoneda [email protected]

@mathetake
mathetake marked this pull request as ready for review July 6, 2021 07:03
@mathetake mathetake changed the title wasm: fix race condition on listener info in stream contexts. wasm: fix race condition on listener info in contexts. Jul 6, 2021

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

Lifetime of listener metadata LGTM

Comment thread source/extensions/common/wasm/context.h Resolved
@kyessenov

Copy link
Copy Markdown
Contributor

@PiotrSikora What does Wasm spec say here? There are some significant repercussions on how we share roots since caches are generally stored there. Stats filter in istio, for example, keeps track of request reports in the root and (theoretically) benefits from sharing. I remember that the big change was making sure that listener reloads create new roots to avoid immediate config application. If we have multiple listeners reloading, how do we make sharing work?

@PiotrSikora

Copy link
Copy Markdown
Contributor

@mathetake thanks for the fast turnaround on the fix and detailed description of the changes in this PR, that's really helpful!

While this PR looks fine code-wise, I'd prefer if we didn't introduce changes breaking existing Wasm extensions. Making listener info unavailable in the root context also doesn't seem like a desireable outcome.

The original intent was always to create a separate root context for each unique configuration, with multiple Envoy filters configured exactly the same way being backed by a single root context inside the WasmVM. Currently, "unique configuration" is keyed only on root_id and plugin configuration, but it seems that the key should also include listener metadata and traffic direction. Such change should only slightly increase the number of root contexts without breaking anything, and it's technically more of a bugfix than a behavioral change.

Alternatively, we could change the mapping and create a separate root context for each configured Envoy filter, which I think could be easier to reason about to the users, but as @kyessenov pointed out, we'd lose the benefit of sharing root context across multiple (potentially thousands) identially configured Envoy filters, which might be quite costly at scale.

@mathetake

Copy link
Copy Markdown
Member Author

it seems that the key should also include listener metadata and traffic direction. Such change should only slightly increase the number of root contexts without breaking anything, and it's technically more of a bugfix than a behavioral change.

OK that would work and make sense to me. Let me rework this PR and create the corresponding one to C++ host. (Sorry in advance I won't be available until tomorrow morning in UTC+9 so don't expect me to turnaround in the next 24 hours)

@mathetake
mathetake force-pushed the listener-info-race branch from 00a45f4 to 16874aa Compare July 10, 2021 02:13
@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: #17243 was synchronize by mathetake.

see: more, trace.

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

Copy link
Copy Markdown
Member Author

OK reworked this PR, and created the necessary change in C++ host: proxy-wasm/proxy-wasm-cpp-host#178

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

LGTM, but could you verify that this fixes the issue in Istio Proxy before we merge it? Thanks!

Comment thread bazel/repository_locations.bzl Resolved
@mathetake

Copy link
Copy Markdown
Member Author

verified that the original issue Istio faced disappeared!

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake

Copy link
Copy Markdown
Member Author

ping @moderation for dep:)

@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!

@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 Jul 12, 2021
@lizan
lizan merged commit 56d92a5 into envoyproxy:main Jul 12, 2021
@mathetake
mathetake deleted the listener-info-race branch July 12, 2021 22:58
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]>
wbpcode added a commit that referenced this pull request Aug 28, 2026
Commit Message: wasm: fix multiple bug of vm key and plugin key
calculation
Additional Description:

This PR fixed tow bugs of wasm vm cache key and wasm plugin cache key:
- now, the whole PluginConfig will be used to calculate the cache key.
Before this change, the plugins with different
`allow_on_headers_stop_iteration` may hit same cached plugin handle and
make unexpected behavior.
- now, the `capability_restriction_config` and `runtime` will be used to
calculate the vm cache key. Before this change, the plugin VMs with
different `capability_restriction_config` and `runtime` may hit same
cache and result in unexpected behavior.

---

Now, we also removed the traffic direction from the plugin cache key.
It's safe because it's unnecessary now.

Long time ago, before the #31365, we cannot extract the listener info
from the connection info, so the WASM will get the traffic direction and
listener metadata from the FactoryContext and store the reference at the
cached plugin. Then when `get_property` ABI is used, the plugin could
serve the related attributes.

And to avoid the wasm plugins that be configured in different listeners
(with different direction/metadata) hit same plugin cache, we added the
listener metadata and traffic direction in to plugin cache key
calculation at #17243.


But at the #36619 , we have did a great refactoring and removed the
dependencies to the traffic direction and listener metadata from the
FactoryContext. The `get_property` ABI will get attributes from the
stream info and stream level context. So, after that, it's unnecessary
to use the listener metadata and traffic direction as cache key. And at
the #36941, we removed the traffic direction and listener metadata from
the cached plugin completely. After that, the traffic direction/metadata
make nonsense in the cache key.

Risk Level: low.
Testing: n/a.
Docs Changes: n/a.
Release Notes: added.
Platform Specific Features: n/a.

---------

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

6 participants