Skip to content

Commit

Permalink
fix(proxy-wasm) cancel dispatches when immediately producing a response
Browse files Browse the repository at this point in the history
Delete the posted event for dispatches that were sent in the latest
Proxy-Wasm step.
  • Loading branch information
thibaultcha committed May 30, 2024
1 parent 9003161 commit 1ff8835
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,

ngx_post_event(ev, &ngx_posted_events);

call->ev = ev;

ngx_queue_insert_head(&pwexec->calls, &call->q);

ngx_proxy_wasm_ctx_set_next_action(pwctx, NGX_PROXY_WASM_ACTION_PAUSE);
Expand Down Expand Up @@ -427,6 +429,12 @@ ngx_http_proxy_wasm_dispatch_destroy(ngx_http_proxy_wasm_dispatch_t *call)

dd("enter");

if (call->ev) {
ngx_delete_posted_event(call->ev);
ngx_free(call->ev);
call->ev = NULL;
}

ngx_wasm_socket_tcp_destroy(sock);

if (call->host.data) {
Expand Down Expand Up @@ -461,6 +469,7 @@ ngx_http_proxy_wasm_dispatch_handler(ngx_event_t *ev)
ngx_wasm_socket_tcp_t *sock = &call->sock;

ngx_free(ev);
call->ev = NULL;

sock->resume_handler = ngx_http_proxy_wasm_dispatch_resume_handler;
sock->data = call;
Expand Down
2 changes: 2 additions & 0 deletions src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct ngx_http_proxy_wasm_dispatch_s {
ngx_chain_t *req_body;
ngx_chain_t *req_out;

ngx_event_t *ev; /* initial posted event */

ngx_wasm_http_reader_ctx_t http_reader;
ngx_http_proxy_wasm_dispatch_state_e state;
ngx_http_request_t fake_r;
Expand Down
26 changes: 26 additions & 0 deletions t/03-proxy_wasm/hfuncs/133-proxy_dispatch_http_edge_cases.t
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,29 @@ qr/\A\[error] .*? dispatch failed: tcp socket - Connection refused
[crit]
[emerg]
[alert]



=== TEST 13: proxy_wasm - dispatch_http_call() followed by a local response
Cancels posted events of pending dispatches
--- skip_no_debug
--- valgrind
--- load_nginx_modules: ngx_http_echo_module
--- wasm_modules: hostcalls
--- config
location /t {
proxy_wasm hostcalls 'on=request_headers \
test=/t/dispatch_and_local_response \
host=127.0.0.1:1';
echo_status 200;
}
--- error_code: 201
--- response_body
--- grep_error_log eval: qr/proxy_wasm http dispatch cancelled/
--- grep_error_log_out
proxy_wasm http dispatch cancelled
proxy_wasm http dispatch cancelled
--- no_error_log
[crit]
[emerg]
[alert]
7 changes: 7 additions & 0 deletions t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ impl TestHttp {
return Action::Pause;
}

/* edge case: dispatch + local response */
"/t/dispatch_and_local_response" => {
self.send_http_dispatch(0);
self.send_http_dispatch(0);
test_send_status(self, 201)
}

/* shared memory */
"/t/shm/get_shared_data" => test_get_shared_data(self),
"/t/shm/set_shared_data" => test_set_shared_data(self),
Expand Down

0 comments on commit 1ff8835

Please sign in to comment.