Skip to content

Commit 1f37d81

Browse files
committed
NJS: adding the missing vm destruction.
This commit fixed the njs memory leak happened in the config validation, updating and http requests.
1 parent b2d1915 commit 1f37d81

File tree

8 files changed

+69
-4
lines changed

8 files changed

+69
-4
lines changed

docs/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ NGINX Unit updated to 1.29.1.
2525
</para>
2626
</change>
2727

28+
<change type="bugfix">
29+
<para>
30+
memory leak related to NJS.
31+
</para>
32+
</change>
33+
2834
</changes>
2935

3036

src/nxt_conf_validation.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,25 +1284,35 @@ nxt_conf_validate(nxt_conf_validation_t *vldt)
12841284

12851285
vldt->tstr_state = nxt_tstr_state_new(vldt->pool, 1);
12861286
if (nxt_slow_path(vldt->tstr_state == NULL)) {
1287-
return NXT_ERROR;
1287+
ret = NXT_ERROR;
1288+
goto fail;
12881289
}
12891290

12901291
ret = nxt_conf_vldt_type(vldt, NULL, vldt->conf, NXT_CONF_VLDT_OBJECT);
12911292
if (ret != NXT_OK) {
1292-
return ret;
1293+
goto fail;
12931294
}
12941295

12951296
ret = nxt_conf_vldt_object(vldt, vldt->conf, nxt_conf_vldt_root_members);
12961297
if (ret != NXT_OK) {
1297-
return ret;
1298+
goto fail;
12981299
}
12991300

13001301
ret = nxt_tstr_state_done(vldt->tstr_state, error);
13011302
if (ret != NXT_OK) {
1302-
return nxt_conf_vldt_error(vldt, "%s", error);
1303+
ret = nxt_conf_vldt_error(vldt, "%s", error);
1304+
goto fail;
13031305
}
13041306

1307+
nxt_tstr_state_release(vldt->tstr_state);
1308+
13051309
return NXT_OK;
1310+
1311+
fail:
1312+
1313+
nxt_tstr_state_release(vldt->tstr_state);
1314+
1315+
return ret;
13061316
}
13071317

13081318

src/nxt_http_request.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
833833
r->body->file->fd = -1;
834834
}
835835

836+
if (r->tstr_query != NULL) {
837+
nxt_tstr_query_release(r->tstr_query);
838+
}
839+
836840
if (nxt_fast_path(proto.any != NULL)) {
837841
protocol = r->protocol;
838842

src/nxt_js.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ nxt_js_conf_new(nxt_mp_t *mp)
4646

4747
jcf->funcs = nxt_array_create(mp, 4, sizeof(nxt_str_t));
4848
if (nxt_slow_path(jcf->funcs == NULL)) {
49+
njs_vm_destroy(jcf->vm);
4950
return NULL;
5051
}
5152

5253
return jcf;
5354
}
5455

5556

57+
void
58+
nxt_js_conf_release(nxt_js_conf_t *jcf)
59+
{
60+
njs_vm_destroy(jcf->vm);
61+
}
62+
63+
5664
void
5765
nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, njs_uint_t n)
5866
{
@@ -297,3 +305,12 @@ nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js,
297305

298306
return NXT_OK;
299307
}
308+
309+
310+
void
311+
nxt_js_release(nxt_js_cache_t *cache)
312+
{
313+
if (cache->vm != NULL) {
314+
njs_vm_destroy(cache->vm);
315+
}
316+
}

src/nxt_js.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ typedef struct {
2222

2323

2424
nxt_js_conf_t *nxt_js_conf_new(nxt_mp_t *mp);
25+
void nxt_js_conf_release(nxt_js_conf_t *jcf);
2526
void nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, nxt_uint_t n);
2627
nxt_js_t *nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz);
2728
nxt_int_t nxt_js_compile(nxt_js_conf_t *jcf);
2829
nxt_int_t nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error);
2930
nxt_int_t nxt_js_call(nxt_task_t *task, nxt_js_cache_t *cache, nxt_js_t *js,
3031
nxt_str_t *str, void *ctx);
32+
void nxt_js_release(nxt_js_cache_t *cache);
3133

3234

3335
extern njs_int_t nxt_js_proto_id;

src/nxt_router.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,10 @@ nxt_router_temp_conf(nxt_task_t *task)
11111111

11121112
fail:
11131113

1114+
if (rtcf->tstr_state != NULL) {
1115+
nxt_tstr_state_release(rtcf->tstr_state);
1116+
}
1117+
11141118
nxt_mp_destroy(mp);
11151119

11161120
return NULL;
@@ -3794,6 +3798,8 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
37943798

37953799
nxt_router_access_log_release(task, lock, rtcf->access_log);
37963800

3801+
nxt_tstr_state_release(rtcf->tstr_state);
3802+
37973803
nxt_mp_thread_adopt(rtcf->mem_pool);
37983804

37993805
nxt_mp_destroy(rtcf->mem_pool);

src/nxt_tstr.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error)
194194
}
195195

196196

197+
void
198+
nxt_tstr_state_release(nxt_tstr_state_t *state)
199+
{
200+
#if (NXT_HAVE_NJS)
201+
nxt_js_conf_release(state->jcf);
202+
#endif
203+
}
204+
205+
197206
nxt_bool_t
198207
nxt_tstr_is_const(nxt_tstr_t *tstr)
199208
{
@@ -315,3 +324,12 @@ nxt_tstr_query_handle(nxt_task_t *task, nxt_tstr_query_t *query,
315324
task, query->ctx, query->data);
316325
}
317326
}
327+
328+
329+
void
330+
nxt_tstr_query_release(nxt_tstr_query_t *query)
331+
{
332+
#if (NXT_HAVE_NJS)
333+
nxt_js_release(&query->cache->js);
334+
#endif
335+
}

src/nxt_tstr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ nxt_tstr_t *nxt_tstr_compile(nxt_tstr_state_t *state, nxt_str_t *str,
4242
nxt_tstr_flags_t flags);
4343
nxt_int_t nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error);
4444
nxt_int_t nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error);
45+
void nxt_tstr_state_release(nxt_tstr_state_t *state);
4546

4647
nxt_bool_t nxt_tstr_is_const(nxt_tstr_t *tstr);
4748
void nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str);
@@ -55,6 +56,7 @@ void nxt_tstr_query_resolve(nxt_task_t *task, nxt_tstr_query_t *query,
5556
void *data, nxt_work_handler_t ready, nxt_work_handler_t error);
5657
void nxt_tstr_query_handle(nxt_task_t *task, nxt_tstr_query_t *query,
5758
nxt_bool_t failed);
59+
void nxt_tstr_query_release(nxt_tstr_query_t *query);
5860

5961

6062
nxt_inline nxt_bool_t

0 commit comments

Comments
 (0)