Skip to content

Commit

Permalink
fix(*) ensure fake requests filter chains are freed
Browse files Browse the repository at this point in the history
Such as fake request passed to the FFI via the init_worker_by_lua fake
request.
  • Loading branch information
thibaultcha committed Sep 27, 2023
1 parent b10014c commit 0d63b5f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/common/lua/ngx_wasm_lua_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,22 @@ ngx_int_t
ngx_http_wasm_ffi_set_property(ngx_http_request_t *r,
ngx_str_t *key, ngx_str_t *value)
{
ngx_http_wasm_req_ctx_t *rctx;
ngx_proxy_wasm_ctx_t *pwctx;
ngx_http_wasm_req_ctx_t *rctx;
ngx_proxy_wasm_ctx_t *pwctx;
ngx_proxy_wasm_subsystem_t *subsystem = NULL;

#ifdef NGX_WASM_HTTP
subsystem = &ngx_http_proxy_wasm;
#endif

if (ngx_http_wasm_rctx(r, &rctx) != NGX_OK) {
return NGX_ERROR;
}

pwctx = ngx_http_proxy_wasm.get_context(rctx);
pwctx = ngx_proxy_wasm_ctx(NULL, 0,
NGX_PROXY_WASM_ISOLATION_STREAM,
subsystem,
rctx);
if (pwctx == NULL) {
return NGX_ERROR;
}
Expand All @@ -204,8 +212,13 @@ ngx_int_t
ngx_http_wasm_ffi_get_property(ngx_http_request_t *r,
ngx_str_t *key, ngx_str_t *value)
{
ngx_http_wasm_req_ctx_t *rctx;
ngx_proxy_wasm_ctx_t *pwctx;
ngx_http_wasm_req_ctx_t *rctx;
ngx_proxy_wasm_ctx_t *pwctx;
ngx_proxy_wasm_subsystem_t *subsystem = NULL;

#ifdef NGX_WASM_HTTP
subsystem = &ngx_http_proxy_wasm;
#endif

if (ngx_http_wasm_rctx(r, &rctx) != NGX_OK) {
/*
Expand All @@ -215,7 +228,10 @@ ngx_http_wasm_ffi_get_property(ngx_http_request_t *r,
return NGX_ERROR;
}

pwctx = ngx_http_proxy_wasm.get_context(rctx);
pwctx = ngx_proxy_wasm_ctx(NULL, 0,
NGX_PROXY_WASM_ISOLATION_STREAM,
subsystem,
rctx);
if (pwctx == NULL) {
return NGX_ERROR;
}
Expand Down
20 changes: 20 additions & 0 deletions src/http/proxy_wasm/ngx_http_proxy_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,23 @@ ngx_http_proxy_wasm_ecode(ngx_proxy_wasm_err_e ecode)
}


static void
ngx_http_wasm_ffi_cleanup_handler(void *data)
{
ngx_proxy_wasm_ctx_t *pwctx = data;

ngx_proxy_wasm_ctx_destroy(pwctx);
}


static ngx_proxy_wasm_ctx_t *
ngx_http_proxy_wasm_ctx(void *data)
{
ngx_proxy_wasm_ctx_t *pwctx;
ngx_http_wasm_req_ctx_t *rctx = data;
ngx_http_request_t *r = rctx->r;
ngx_http_wasm_loc_conf_t *loc;
ngx_pool_cleanup_t *cln;

loc = ngx_http_get_module_loc_conf(r, ngx_http_wasm_module);

Expand All @@ -374,6 +384,16 @@ ngx_http_proxy_wasm_ctx(void *data)

/* for on_request_body retrieval */
rctx->data = pwctx;

if (rctx->fake_request) {
cln = ngx_pool_cleanup_add(pwctx->parent_pool, 0);
if (cln == NULL) {
return NULL;
}

cln->handler = ngx_http_wasm_ffi_cleanup_handler;
cln->data = pwctx;
}
}

return pwctx;
Expand Down

0 comments on commit 0d63b5f

Please sign in to comment.