Skip to content

Commit 6cfb37d

Browse files
committed
Don't pause processing when send_local_response fails
For context see the Envoy issue envoyproxy/envoy#28826. Here is a shorter summary: 1. A wasm plugin calls proxy_send_local_response from both onRequestHeaders and onResponseHeaders 2. When proxy_send_local_reply is called from onRequestHeaders it triggers a local reply and that reply goes through the filter chain in Envoy 3. The same plugin is called again as part of the filter chain processing but this time onResponseHeaders is called 4. onResponseHeaders calls proxy_send_local_response which ultimately does not generate a local reply, but it stops filter chain processing. As a result we end up with a stuck connection on Envoy - no local reply and processing is stopped. I think that proxy wasm plugins shouldn't use proxy_send_local_response this way, so ultimately whoever created such a plugin shot themselves in the foot. That being said, I think there are a few improvements that could be made here on Envoy/proxy-wasm side to handle this situation somewhat better: 1. We can avoid stopping processing in such cases to prevent stuck connections on Envoy 2. We can return errors from proxy_send_local_response instead of silently ignoring them. Currently Envoy implementation of sendLocalResponse can detect when a second local response is requested and returns an error in this case without actually trying to send a local response. However, even though Envoy reports an error, send_local_response ignores the result of the host specific sendLocalResponse implementation and stops processing and returns success to the wasm plugin. With this change, send_local_response will check the result of the host-specific implementation of the sendLocalResponse. In cases when sendLocalResponse fails it will just propagate the error to the caller and do nothing else (including stopping processing). I think this behavior makes sense in principle because on the one hand we don't ignore the failure from sendLocalResponse and on the other hand, when the failure happens we don't trigger any side-effects expected from the successful proxy_send_local_response call. NOTE: Even though I do think that this is a more resonable behavior, it's still a change from the previous behavior and it might break existing proxy-wasm plugins. Specifically: 1. C++ plugins that proactively check the result of proxy_send_local_response will change behavior (e.g., before proxy_send_local_response failed silently) 2. Rust plugins, due to the way Rust SDK handles errors from proxy_send_local_response will crash in runtime in this case. On the bright side of things, the plugins that are affected by this change currently just cause stuck connections in Envoy, so we are changing one undesirable behavior for another, but more explicit. Signed-off-by: Mikhail Krinkin <[email protected]>
1 parent 3212034 commit 6cfb37d

File tree

6 files changed

+164
-1
lines changed

6 files changed

+164
-1
lines changed

src/exports.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,25 @@ Word send_local_response(Word response_code, Word response_code_details_ptr,
153153
return WasmResult::InvalidMemoryAccess;
154154
}
155155
auto additional_headers = PairsUtil::toPairs(additional_response_header_pairs.value());
156-
context->sendLocalResponse(response_code, body.value(), std::move(additional_headers),
156+
auto status = context->sendLocalResponse(response_code, body.value(), std::move(additional_headers),
157157
grpc_status, details.value());
158+
// Only stop processing if we actually triggered local response.
159+
//
160+
// For context, Envoy sends local replies through the filter chain,
161+
// so wasm filter can be called to handle a local reply that the
162+
// filter itself triggered.
163+
//
164+
// Normally that is not an issue, unless wasm filter calls
165+
// proxy_send_local_response again (which they probably shouldn't).
166+
// In this case, no new local response will be generated and
167+
// sendLocalResponse will fail.
168+
//
169+
// If at this point we stop processing, we end up in a situation when
170+
// no response was sent, even though we tried twice, and the connection
171+
// is stuck, because processing is stopped.
172+
if (status != WasmResult::Ok) {
173+
return status;
174+
}
158175
context->wasm()->stopNextIteration(true);
159176
return WasmResult::Ok;
160177
}

test/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ cc_test(
8989
data = [
9090
"//test/test_data:clock.wasm",
9191
"//test/test_data:env.wasm",
92+
"//test/test_data:local_response.wasm",
9293
"//test/test_data:random.wasm",
9394
],
9495
linkstatic = 1,

test/exports_test.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,42 @@ TEST_P(TestVm, RandomTooLarge) {
157157
EXPECT_TRUE(context->isLogged("random_get(66560) failed."));
158158
}
159159

160+
TEST_P(TestVm, SendLocalResponse) {
161+
auto source = readTestWasmFile("local_response.wasm");
162+
ASSERT_FALSE(source.empty());
163+
auto wasm = TestWasm(std::move(vm_));
164+
ASSERT_TRUE(wasm.load(source, false));
165+
ASSERT_TRUE(wasm.initialize());
166+
167+
auto *context = dynamic_cast<TestContext *>(wasm.vm_context());
168+
169+
170+
// We first try the negative case - proxy_send_local_response fails
171+
WasmCallVoid<0> run_fail;
172+
wasm.wasm_vm()->getFunction("run_fail", &run_fail);
173+
ASSERT_TRUE(run_fail != nullptr);
174+
run_fail(context);
175+
176+
// We expect application to log whatever status
177+
// proxy_send_local_response returns.
178+
EXPECT_TRUE(context->isLogged(
179+
stringify("proxy_send_local_response returned ",
180+
static_cast<uint32_t>(WasmResult::Unimplemented))));
181+
// When we fail to send local response we don't pause processing.
182+
EXPECT_FALSE(context->wasm()->isNextIterationStopped());
183+
184+
// Then we try the positive case - proxy_send_local_response succeeds
185+
WasmCallVoid<0> run_success;
186+
wasm.wasm_vm()->getFunction("run_success", &run_success);
187+
ASSERT_TRUE(run_success != nullptr);
188+
run_success(context);
189+
190+
EXPECT_TRUE(context->isLogged(
191+
stringify("proxy_send_local_response returned ",
192+
static_cast<uint32_t>(WasmResult::Ok))));
193+
// When we succeed to send local response we stop processing.
194+
EXPECT_TRUE(context->wasm()->isNextIterationStopped());
195+
}
196+
160197
} // namespace
161198
} // namespace proxy_wasm

test/test_data/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ wasm_rust_binary(
8080
wasi = True,
8181
)
8282

83+
wasm_rust_binary(
84+
name = "local_response.wasm",
85+
srcs = ["local_response.rs"],
86+
wasi = True,
87+
)
88+
8389
proxy_wasm_cc_binary(
8490
name = "canary_check.wasm",
8591
srcs = ["canary_check.cc"],

test/test_data/local_response.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#[no_mangle]
15+
pub extern "C" fn proxy_abi_version_0_2_0() {}
16+
17+
#[no_mangle]
18+
pub extern "C" fn proxy_on_memory_allocate(_: usize) -> *mut u8 {
19+
std::ptr::null_mut()
20+
}
21+
22+
fn send_http_response(status_code: u32) -> u32 {
23+
let headers = 0u32.to_le_bytes().to_vec();
24+
unsafe {
25+
proxy_send_local_response(
26+
status_code,
27+
std::ptr::null(),
28+
0,
29+
std::ptr::null(),
30+
0,
31+
headers.as_ptr(),
32+
headers.len(),
33+
-1)
34+
}
35+
}
36+
37+
#[no_mangle]
38+
pub extern "C" fn run_fail() {
39+
println!(
40+
"proxy_send_local_response returned {}",
41+
send_http_response(404));
42+
}
43+
44+
#[no_mangle]
45+
pub extern "C" fn run_success() {
46+
println!(
47+
"proxy_send_local_response returned {}",
48+
send_http_response(200));
49+
}
50+
51+
extern "C" {
52+
fn proxy_send_local_response(
53+
status_code: u32,
54+
status_code_details_data: *const u8,
55+
status_code_details_size: usize,
56+
body_data: *const u8,
57+
body_size: usize,
58+
headers_data: *const u8,
59+
headers_size: usize,
60+
grpc_status: i32,
61+
) -> u32;
62+
}

test/utility.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ namespace proxy_wasm {
4545
std::vector<std::string> getWasmEngines();
4646
std::string readTestWasmFile(const std::string &filename);
4747

48+
namespace internal {
49+
50+
template <typename... Args>
51+
struct Stringify {
52+
static void convert(std::ostream& out) {}
53+
};
54+
55+
template <typename... Args>
56+
void stringify_impl(std::ostream& out, Args... args) {
57+
Stringify<Args...>::convert(out, std::forward<Args>(args)...);
58+
}
59+
60+
template <typename A, typename... Args>
61+
struct Stringify<A, Args...> {
62+
static void convert(std::ostream& out, A arg, Args... args) {
63+
out << arg;
64+
stringify_impl(out, std::forward<Args>(args)...);
65+
}
66+
};
67+
68+
} // namespace internal
69+
70+
template <typename... Args>
71+
std::string stringify(Args... args) {
72+
std::ostringstream out(std::ostringstream::ate);
73+
internal::stringify_impl(out, std::forward<Args>(args)...);
74+
return out.str();
75+
}
76+
4877
class TestIntegration : public WasmVmIntegration {
4978
public:
5079
~TestIntegration() override = default;
@@ -133,6 +162,17 @@ class TestContext : public ContextBase {
133162
.count();
134163
}
135164

165+
WasmResult sendLocalResponse(uint32_t response_code,
166+
std::string_view body,
167+
Pairs headers,
168+
GrpcStatusCode grpc_status,
169+
std::string_view details) override {
170+
if (response_code >= 200 && response_code < 300) {
171+
return WasmResult::Ok;
172+
}
173+
return WasmResult::Unimplemented;
174+
}
175+
136176
private:
137177
std::string log_;
138178
static std::string global_log_;

0 commit comments

Comments
 (0)