Skip to content

Commit

Permalink
fix(proxy-wasm) cancel dispatch when immediately producing a response
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 22, 2024
1 parent 5978747 commit c790268
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/common/proxy_wasm/ngx_proxy_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ typedef enum {
NGX_PROXY_WASM_ERR_START_FAILED = 5,
NGX_PROXY_WASM_ERR_VM_START_FAILED = 6,
NGX_PROXY_WASM_ERR_CONFIGURE_FAILED = 7,
NGX_PROXY_WASM_ERR_DISPATCH_FAILED = 8,
NGX_PROXY_WASM_ERR_INSTANCE_TRAPPED = 9,
NGX_PROXY_WASM_ERR_RETURN_ACTION = 10,
NGX_PROXY_WASM_ERR_UNKNOWN = 11,
NGX_PROXY_WASM_ERR_INSTANCE_TRAPPED = 8,
NGX_PROXY_WASM_ERR_RETURN_ACTION = 9,
NGX_PROXY_WASM_ERR_UNKNOWN = 10,
} ngx_proxy_wasm_err_e;


Expand Down
1 change: 0 additions & 1 deletion src/common/proxy_wasm/ngx_proxy_wasm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static ngx_str_t ngx_proxy_wasm_errlist[] = {
ngx_string("on_context_create internal failure"),
ngx_string("on_vm_start failure"),
ngx_string("on_configure failure"),
ngx_string("dispatch failure"),
ngx_string("instance trapped"),
ngx_string("invalid return action"),
ngx_string("unknown error")
Expand Down
10 changes: 8 additions & 2 deletions src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ ngx_http_proxy_wasm_dispatch_err(ngx_http_proxy_wasm_dispatch_t *call)
}
#endif

//pwexec->ecode = NGX_PROXY_WASM_ERR_DISPATCH_FAILED;

if (!pwexec->ictx->instance->hostcall || rctx->fake_request) {
ngx_wasm_log_error(NGX_LOG_ERR, pwexec->log, 0,
"%*s", p - (u_char *) &errbuf, &errbuf);
Expand Down Expand Up @@ -389,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 @@ -429,6 +429,11 @@ ngx_http_proxy_wasm_dispatch_destroy(ngx_http_proxy_wasm_dispatch_t *call)

dd("enter");

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

ngx_wasm_socket_tcp_destroy(sock);

if (call->host.data) {
Expand Down Expand Up @@ -463,6 +468,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
24 changes: 24 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,27 @@ 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 pending posted event for dispatch
--- 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
--- error_log
proxy_wasm http dispatch cancelled
--- no_error_log
[crit]
[emerg]
[alert]
6 changes: 6 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,12 @@ impl TestHttp {
return Action::Pause;
}

/* edge case: dispatch + local response */
"/t/dispatch_and_local_response" => {
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 c790268

Please sign in to comment.