Skip to content

Commit 5bbf307

Browse files
authored
fix: reset queue when destory hwp_ctx->pool (#113)
Signed-off-by: spacewander <[email protected]>
1 parent 4b3a1b3 commit 5bbf307

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/http/ngx_http_wasm_module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ ngx_http_wasm_free_plugin_ctx(ngx_http_wasm_plugin_ctx_t *hwp_ctx)
339339
if (hwp_ctx->pool != NULL) {
340340
ngx_destroy_pool(hwp_ctx->pool);
341341
hwp_ctx->pool = NULL;
342+
343+
/* http_ctx destroyed in pool should not be referred in the queue */
344+
ngx_queue_init(&hwp_ctx->occupied);
345+
ngx_queue_init(&hwp_ctx->free);
342346
}
343347

344348
ngx_log_error(NGX_LOG_INFO, log, 0, "free plugin context %d", ctx_id);

t/http_lifecycle.t

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,50 @@ location /t {
190190
--- error_log
191191
failed to call function
192192
failed to run proxy_on_http_response_headers
193+
194+
195+
196+
=== TEST 8: reuse plugin ctx id with freed http ctx
197+
--- config
198+
location /hit {
199+
content_by_lua_block {
200+
local wasm = require("resty.proxy-wasm")
201+
local ctx = package.loaded.ctx
202+
assert(wasm.on_http_request_headers(ctx))
203+
}
204+
}
205+
location /t {
206+
content_by_lua_block {
207+
local wasm = require("resty.proxy-wasm")
208+
local plugin = wasm.load("plugin", "t/testdata/http_lifecycle/main.go.wasm")
209+
local http = require "resty.http"
210+
local uri = "http://127.0.0.1:" .. ngx.var.server_port
211+
.. "/hit"
212+
for i = 1, 2 do
213+
do
214+
local ctx = assert(wasm.on_configure(plugin, '{"body":512}'))
215+
package.loaded.ctx = ctx
216+
local httpc = http.new()
217+
local res, err = httpc:request_uri(uri)
218+
if not res then
219+
ngx.log(ngx.ERR, err)
220+
return
221+
end
222+
package.loaded.ctx = nil
223+
end
224+
collectgarbage()
225+
end
226+
}
227+
}
228+
--- grep_error_log eval
229+
qr/(create|free) (plugin|http) context \d+/
230+
--- grep_error_log_out eval
231+
qr/create plugin context 1
232+
create http context 2
233+
free http context 2
234+
free plugin context 1
235+
create plugin context 1
236+
create http context 3
237+
free http context 3
238+
free plugin context 1
239+
/

0 commit comments

Comments
 (0)