Skip to content

QuickJS: added memory limit check for reuse queue. #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions nginx/ngx_http_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ static ngx_command_t ngx_http_js_commands[] = {
offsetof(ngx_http_js_loc_conf_t, reuse),
NULL },

{ ngx_string("js_context_reuse_max_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_js_loc_conf_t, reuse_max_size),
NULL },

{ ngx_string("js_import"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13,
ngx_js_import,
Expand Down
26 changes: 26 additions & 0 deletions nginx/ngx_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
JSRuntime *rt;
JSContext *cx;
JSClassID class_id;
JSMemoryUsage stats;
ngx_qjs_event_t *event;
ngx_js_opaque_t *opaque;
njs_rbtree_node_t *node;
Expand Down Expand Up @@ -1198,6 +1199,28 @@ ngx_engine_qjs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
cln->data = conf->reuse_queue;
}

/*
* After the request object is freed, the runtime's memory usage should
* be low. It can only remain high if the global scope was
* modified.
*
* To prevent unlimited memory consumption growth, check whether memory
* usage exceeds the configured limit. The check is performed rarely to
* avoid performance impact of JS_ComputeMemoryUsage() which is slow.
*/

if ((ngx_random() & 0xff) == 1) {
JS_ComputeMemoryUsage(JS_GetRuntime(cx), &stats);

if ((size_t) stats.malloc_size > conf->reuse_max_size) {
ngx_log_error(NGX_LOG_WARN, ctx->log, 0,
"js remaining memory usage of the context "
"exceeds \"js_context_reuse_max_size\" limit: %L"
", not reusing it", stats.malloc_size);
goto free_ctx;
}
}

if (ngx_js_queue_push(conf->reuse_queue, cx) != NGX_OK) {
goto free_ctx;
}
Expand Down Expand Up @@ -3950,6 +3973,7 @@ ngx_js_create_conf(ngx_conf_t *cf, size_t size)
conf->preload_objects = NGX_CONF_UNSET_PTR;

conf->reuse = NGX_CONF_UNSET_SIZE;
conf->reuse_max_size = NGX_CONF_UNSET_SIZE;
conf->buffer_size = NGX_CONF_UNSET_SIZE;
conf->max_response_body_size = NGX_CONF_UNSET_SIZE;
conf->timeout = NGX_CONF_UNSET_MSEC;
Expand Down Expand Up @@ -4059,6 +4083,8 @@ ngx_js_merge_conf(ngx_conf_t *cf, void *parent, void *child,

ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
ngx_conf_merge_size_value(conf->reuse, prev->reuse, 128);
ngx_conf_merge_size_value(conf->reuse_max_size, prev->reuse_max_size,
4 * 1024 * 1024);
ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, 16384);
ngx_conf_merge_size_value(conf->max_response_body_size,
prev->max_response_body_size, 1048576);
Expand Down
1 change: 1 addition & 0 deletions nginx/ngx_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ typedef struct {
ngx_uint_t type; \
ngx_engine_t *engine; \
ngx_uint_t reuse; \
size_t reuse_max_size; \
ngx_js_queue_t *reuse_queue; \
ngx_str_t cwd; \
ngx_array_t *imports; \
Expand Down
7 changes: 7 additions & 0 deletions nginx/ngx_stream_js_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ static ngx_command_t ngx_stream_js_commands[] = {
offsetof(ngx_stream_js_srv_conf_t, reuse),
NULL },

{ ngx_string("js_context_reuse_max_size"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, reuse_max_size),
NULL },

{ ngx_string("js_import"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE13,
ngx_js_import,
Expand Down